From 82f14f0d07c54cd04b9ec61d9b296305772f5e9d Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Thu, 30 May 2019 14:05:41 +0800 Subject: [PATCH 1/7] show total speed as notification title --- .../kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index 91ff3860a..d0c127ac4 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -182,11 +182,19 @@ class V2RayVpnService : VpnService() { sendFd() if (defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) { + val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") + var last_zero_speed = false mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS) .subscribe { val uplink = v2rayPoint.queryStats("socks", "uplink") val downlink = v2rayPoint.queryStats("socks", "downlink") - updateNotification("${(uplink / 3).toSpeedString()} ↑ ${(downlink / 3).toSpeedString()} ↓") + val total = uplink + downlink + if (total > 0 || !last_zero_speed) { + updateNotification( + "${cf_name} [${(total / 3).toSpeedString()}]", + "${(uplink / 3).toSpeedString()} ↑ ${(downlink / 3).toSpeedString()} ↓") + } + last_zero_speed = (total == 0L) } } } @@ -357,9 +365,10 @@ class V2RayVpnService : VpnService() { mSubscription = null } - private fun updateNotification(contentText: String) { + private fun updateNotification(titleText: String, contentText: String) { if (mBuilder != null) { mBuilder?.setContentText(contentText) + mBuilder?.setContentTitle(titleText) getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build()) } } From 5aca55cb8f5fe2344f4fde06459a2e36ba3e832b Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Fri, 31 May 2019 09:06:14 +0800 Subject: [PATCH 2/7] only use title to show speed --- .../com/v2ray/ang/service/V2RayVpnService.kt | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index d0c127ac4..a584a825b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -188,13 +188,11 @@ class V2RayVpnService : VpnService() { .subscribe { val uplink = v2rayPoint.queryStats("socks", "uplink") val downlink = v2rayPoint.queryStats("socks", "downlink") - val total = uplink + downlink - if (total > 0 || !last_zero_speed) { - updateNotification( - "${cf_name} [${(total / 3).toSpeedString()}]", - "${(uplink / 3).toSpeedString()} ↑ ${(downlink / 3).toSpeedString()} ↓") + val zero_speed = (uplink == 0L && downlink == 0L) + if (!zero_speed || !last_zero_speed) { + updateNotification("${cf_name} · ${(uplink / 3).toSpeedString()} ↑ ${(downlink / 3).toSpeedString()} ↓") } - last_zero_speed = (total == 0L) + last_zero_speed = zero_speed } } } @@ -329,7 +327,6 @@ class V2RayVpnService : VpnService() { mBuilder = NotificationCompat.Builder(applicationContext, channelId) .setSmallIcon(R.drawable.ic_v) .setContentTitle(defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "")) - .setContentText(getString(R.string.notification_action_more)) .setPriority(NotificationCompat.PRIORITY_MIN) .setOngoing(true) .setShowWhen(false) @@ -365,10 +362,9 @@ class V2RayVpnService : VpnService() { mSubscription = null } - private fun updateNotification(titleText: String, contentText: String) { + private fun updateNotification(contentText: String) { if (mBuilder != null) { - mBuilder?.setContentText(contentText) - mBuilder?.setContentTitle(titleText) + mBuilder?.setContentTitle(contentText) getNotificationManager().notify(NOTIFICATION_ID, mBuilder?.build()) } } From 4dda36673a349c6134334044788789ac9e006d9c Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Tue, 9 Jul 2019 11:43:31 +0800 Subject: [PATCH 3/7] allow go to shutdown VpnService --- .../main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index a584a825b..843e29b57 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -198,10 +198,7 @@ class V2RayVpnService : VpnService() { } fun shutdown() { - try { - mInterface.close() - } catch (ignored: Exception) { - } + stopV2Ray(true) } fun sendFd() { @@ -378,6 +375,8 @@ class V2RayVpnService : VpnService() { private inner class V2RayCallback : V2RayVPNServiceSupportsSet { override fun shutdown(): Long { + // called by go + // shutdown the whole vpn service try { this@V2RayVpnService.shutdown() return 0 From 5d760382d75c4fa4219214923c7de6380198eb19 Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Tue, 9 Jul 2019 13:06:28 +0800 Subject: [PATCH 4/7] pass callback as V2RayPoint arg; stop on LowMemory --- .../kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index 843e29b57..96ffc5149 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -55,8 +55,7 @@ class V2RayVpnService : VpnService() { } } - private val v2rayPoint = Libv2ray.newV2RayPoint() - private val v2rayCallback = V2RayCallback() + private val v2rayPoint = Libv2ray.newV2RayPoint(V2RayCallback()) private lateinit var configContent: String private lateinit var mInterface: ParcelFileDescriptor val fd: Int get() = mInterface.fd @@ -110,9 +109,13 @@ class V2RayVpnService : VpnService() { stopV2Ray() } + override fun onLowMemory() { + stopV2Ray() + super.onLowMemory() + } + override fun onDestroy() { super.onDestroy() - cancelNotification() } @@ -239,7 +242,6 @@ class V2RayVpnService : VpnService() { } configContent = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG, "") - v2rayPoint.supportSet = v2rayCallback v2rayPoint.configureFileContent = configContent v2rayPoint.enableLocalDNS = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_LOCAL_DNS_ENABLED, false) v2rayPoint.forwardIpv6 = defaultDPreference.getPrefBoolean(SettingsActivity.PREF_FORWARD_IPV6, false) From 71a65318350fc0edae0174b157f82216962c7839 Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Tue, 9 Jul 2019 14:32:47 +0800 Subject: [PATCH 5/7] stop speed query when screen off; resume when screen on --- V2rayNG/app/src/main/AndroidManifest.xml | 1 + .../com/v2ray/ang/service/V2RayVpnService.kt | 67 ++++++++++++++----- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/V2rayNG/app/src/main/AndroidManifest.xml b/V2rayNG/app/src/main/AndroidManifest.xml index d83230080..a5d804c3c 100644 --- a/V2rayNG/app/src/main/AndroidManifest.xml +++ b/V2rayNG/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index 96ffc5149..ac8e16d19 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -183,21 +183,7 @@ class V2RayVpnService : VpnService() { // Create a new interface using the builder and save the parameters. mInterface = builder.establish() sendFd() - - if (defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) { - val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") - var last_zero_speed = false - mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS) - .subscribe { - val uplink = v2rayPoint.queryStats("socks", "uplink") - val downlink = v2rayPoint.queryStats("socks", "downlink") - val zero_speed = (uplink == 0L && downlink == 0L) - if (!zero_speed || !last_zero_speed) { - updateNotification("${cf_name} · ${(uplink / 3).toSpeedString()} ↑ ${(downlink / 3).toSpeedString()} ↓") - } - last_zero_speed = zero_speed - } - } + startSpeedNotification() } fun shutdown() { @@ -237,7 +223,11 @@ class V2RayVpnService : VpnService() { if (!v2rayPoint.isRunning) { try { - registerReceiver(mMsgReceive, IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)) + val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE) + mFilter.addAction(Intent.ACTION_SCREEN_ON) + mFilter.addAction(Intent.ACTION_SCREEN_OFF) + mFilter.addAction(Intent.ACTION_USER_PRESENT) + registerReceiver(mMsgReceive, mFilter) } catch (e: Exception) { } @@ -375,6 +365,36 @@ class V2RayVpnService : VpnService() { return mNotificationManager!! } + fun startSpeedNotification() { + if (mSubscription == null && + defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) { + val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") + var last_zero_speed = false + + mSubscription = Observable.interval(3, java.util.concurrent.TimeUnit.SECONDS) + .subscribe { + val uplink = v2rayPoint.queryStats("socks", "uplink") + val downlink = v2rayPoint.queryStats("socks", "downlink") + val zero_speed = (uplink == 0L && downlink == 0L) + if (!zero_speed || !last_zero_speed) { + updateNotification("${cf_name} • ${(uplink / 3).toSpeedString()}↑ ${(downlink / 3).toSpeedString()}↓") + } + last_zero_speed = zero_speed + } + } + } + + + fun stopSpeedNotification() { + if (mSubscription != null) { + mSubscription?.unsubscribe() //stop queryStats + mSubscription = null + + val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") + updateNotification(cf_name) + } + } + private inner class V2RayCallback : V2RayVPNServiceSupportsSet { override fun shutdown(): Long { // called by go @@ -453,6 +473,21 @@ class V2RayVpnService : VpnService() { vpnService?.startV2ray() } } + + when (intent?.action) { + Intent.ACTION_SCREEN_OFF -> { + Log.d(AppConfig.ANG_PACKAGE, "SCREEN_OFF, stop querying stats") + vpnService?.stopSpeedNotification() + } + Intent.ACTION_SCREEN_ON -> { + Log.d(AppConfig.ANG_PACKAGE, "SCREEN_ON, start querying stats") + vpnService?.startSpeedNotification() + } + Intent.ACTION_USER_PRESENT -> { + Log.d(AppConfig.ANG_PACKAGE, "USER_PRESENT, start querying stats") + vpnService?.startSpeedNotification() + } + } } } } From 17c858d24c02e09166febf5718a1e42513524a66 Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Tue, 9 Jul 2019 15:58:36 +0800 Subject: [PATCH 6/7] only show speed if core isrunning --- .../app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index ac8e16d19..bc06dd5a3 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -367,6 +367,7 @@ class V2RayVpnService : VpnService() { fun startSpeedNotification() { if (mSubscription == null && + v2rayPoint.isRunning && defaultDPreference.getPrefBoolean(SettingsActivity.PREF_SPEED_ENABLED, false)) { val cf_name = defaultDPreference.getPrefString(AppConfig.PREF_CURR_CONFIG_NAME, "") var last_zero_speed = false From ba30697309f86871aaeb7fa6c21c8b1753e1e2b0 Mon Sep 17 00:00:00 2001 From: cutelua <33216131+cutelua@users.noreply.github.com> Date: Thu, 11 Jul 2019 09:19:30 +0800 Subject: [PATCH 7/7] no need for USER_PRESENT --- V2rayNG/app/src/main/AndroidManifest.xml | 1 - .../src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 4 ---- 2 files changed, 5 deletions(-) diff --git a/V2rayNG/app/src/main/AndroidManifest.xml b/V2rayNG/app/src/main/AndroidManifest.xml index a5d804c3c..d83230080 100644 --- a/V2rayNG/app/src/main/AndroidManifest.xml +++ b/V2rayNG/app/src/main/AndroidManifest.xml @@ -6,7 +6,6 @@ - diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index bc06dd5a3..6bc795ac5 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -484,10 +484,6 @@ class V2RayVpnService : VpnService() { Log.d(AppConfig.ANG_PACKAGE, "SCREEN_ON, start querying stats") vpnService?.startSpeedNotification() } - Intent.ACTION_USER_PRESENT -> { - Log.d(AppConfig.ANG_PACKAGE, "USER_PRESENT, start querying stats") - vpnService?.startSpeedNotification() - } } } }