diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f06850e..51d32d3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,9 +38,6 @@ android:required="false" /> - - - @@ -294,18 +291,6 @@ android:resource="@xml/cache_paths" /> - - - - - - - - throw UnknownError() }.java - val CONNECTION_ID_SHORTCUT = 0 - val CONNECTION_ID_TILE = 1 - val CONNECTION_ID_MAINACTIVITY = 2 + const val CONNECTION_ID_SHORTCUT = 0 + const val CONNECTION_ID_TILE = 1 + const val CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND = 2 + const val CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND = 3 } interface Callback { @@ -102,6 +103,11 @@ class SagerConnection( var service: ISagerNetService? = null + fun updateConnectionId(id: Int) { + connectionId = id + service?.registerCallback(serviceCallback, id) + } + override fun onServiceConnected(name: ComponentName?, binder: IBinder) { this.binder = binder val service = ISagerNetService.Stub.asInterface(binder)!! diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt index f9f3e79..98bfbdd 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt @@ -56,10 +56,16 @@ class TrafficLooper Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId") val oldData = items[selectorNowId] val data = items[id] ?: return - oldData?.tag = selectorNowFakeTag + oldData?.apply { + tag = selectorNowFakeTag + ignore = true + } selectorNowFakeTag = data.tag - data.tag = TAG_PROXY selectorNowId = id + data.apply { + tag = TAG_PROXY + ignore = false + } } private suspend fun loop() { @@ -94,6 +100,7 @@ class TrafficLooper tag = tag, rx = ent.rx, tx = ent.tx, + ignore = proxy.config.selectorGroupId >= 0L, ) if (tag == TAG_PROXY && itemMain == null) { itemMain = item @@ -135,14 +142,18 @@ class TrafficLooper ) // broadcast (MainActivity) - data.binder.broadcast { b -> - if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAINACTIVITY) { - b.cbSpeedUpdate(speed) - if (profileTrafficStatistics) { - items.forEach { (id, item) -> - b.cbTrafficUpdate( - TrafficData(id = id, rx = item.rx, tx = item.tx) // display - ) + if (data.state == BaseService.State.Connected + && data.binder.callbackIdMap.containsValue(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) + ) { + data.binder.broadcast { b -> + if (data.binder.callbackIdMap[b] == SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) { + b.cbSpeedUpdate(speed) + if (profileTrafficStatistics) { + items.forEach { (id, item) -> + b.cbTrafficUpdate( + TrafficData(id = id, rx = item.rx, tx = item.tx) // display + ) + } } } } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficUpdater.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficUpdater.kt index d18adb3..3081e2b 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficUpdater.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficUpdater.kt @@ -13,9 +13,10 @@ class TrafficUpdater( var txRate: Long = 0, var rxRate: Long = 0, var lastUpdate: Long = 0, + var ignore: Boolean = false, ) - private suspend fun updateOne(item: TrafficLooperData): TrafficLooperData { + private fun updateOne(item: TrafficLooperData): TrafficLooperData { // last update val now = System.currentTimeMillis() val interval = now - item.lastUpdate @@ -48,6 +49,7 @@ class TrafficUpdater( suspend fun updateAll() { val updated = mutableMapOf() // diffs items.forEach { item -> + if (item.ignore) return@forEach var diff = updated[item.tag] // query a tag only once if (diff == null) { diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/MainActivity.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/MainActivity.kt index 92bd6cd..1954d15 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/MainActivity.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/MainActivity.kt @@ -381,7 +381,7 @@ class MainActivity : ThemedActivity(), } } - val connection = SagerConnection(SagerConnection.CONNECTION_ID_MAINACTIVITY, true) + val connection = SagerConnection(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND, true) override fun onServiceConnected(service: ISagerNetService) = changeState( try { BaseService.State.values()[service.state] @@ -426,10 +426,12 @@ class MainActivity : ThemedActivity(), } override fun onStart() { + connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_FOREGROUND) super.onStart() } override fun onStop() { + connection.updateConnectionId(SagerConnection.CONNECTION_ID_MAIN_ACTIVITY_BACKGROUND) super.onStop() }