From 490f6c5115387bd334485bcfc0733de7cc19615a Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 20 Jun 2020 23:43:20 -0400 Subject: [PATCH] Lazily start IpNeighbourMonitor in qs tiles --- .../manage/IpNeighbourMonitoringTileService.kt | 18 ++++++++++++++++-- .../manage/LocalOnlyHotspotTileService.kt | 1 + .../vpnhotspot/manage/TetheringTileService.kt | 2 ++ .../vpnhotspot/util/KillableTileService.kt | 2 ++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/IpNeighbourMonitoringTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/IpNeighbourMonitoringTileService.kt index 31308799..c00ebc06 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/IpNeighbourMonitoringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/IpNeighbourMonitoringTileService.kt @@ -1,6 +1,7 @@ package be.mygod.vpnhotspot.manage import android.service.quicksettings.Tile +import androidx.annotation.CallSuper import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.net.IpNeighbour @@ -11,15 +12,28 @@ import java.net.Inet4Address @RequiresApi(24) abstract class IpNeighbourMonitoringTileService : KillableTileService(), IpNeighbourMonitor.Callback { private var neighbours: Collection = emptyList() + private var canRegister = false abstract fun updateTile() + @CallSuper override fun onStartListening() { super.onStartListening() - IpNeighbourMonitor.registerCallback(this) + synchronized(this) { canRegister = true } } + /** + * Lazily start [IpNeighbourMonitor], which could invoke root. + */ + protected fun listenForClients() = synchronized(this) { + if (canRegister) IpNeighbourMonitor.registerCallback(this) + } + + @CallSuper override fun onStopListening() { - IpNeighbourMonitor.unregisterCallback(this) + synchronized(this) { + canRegister = false + IpNeighbourMonitor.unregisterCallback(this) + } super.onStopListening() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotTileService.kt index 1469d46c..cab0e2f0 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/LocalOnlyHotspotTileService.kt @@ -40,6 +40,7 @@ class LocalOnlyHotspotTileService : IpNeighbourMonitoringTileService() { state = Tile.STATE_ACTIVE label = binder.configuration?.ssid ?: getText(R.string.tethering_temp_hotspot) subtitleDevices { it == iface } + listenForClients() } updateTile() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt index d2e2656c..b0aff373 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -92,6 +92,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin state = Tile.STATE_ACTIVE icon = if (interested.all(binder::isActive)) tileOn else tileOff subtitleDevices(interested::contains) + listenForClients() } } label = getText(labelString) @@ -179,6 +180,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin state = Tile.STATE_ACTIVE icon = if (interested.isNotEmpty() && interested.all(binder::isActive)) tileOn else tileOff subtitleDevices(interested::contains) + listenForClients() } false -> { state = Tile.STATE_INACTIVE diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/KillableTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/KillableTileService.kt index 78224b21..2a1feb81 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/KillableTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/KillableTileService.kt @@ -6,6 +6,7 @@ import android.os.Build import android.os.IBinder import android.service.quicksettings.Tile import android.service.quicksettings.TileService +import androidx.annotation.CallSuper import androidx.annotation.RequiresApi @RequiresApi(24) @@ -19,6 +20,7 @@ abstract class KillableTileService : TileService(), ServiceConnection { if (Build.VERSION.SDK_INT >= 29) subtitle = value } + @CallSuper override fun onServiceConnected(name: ComponentName?, service: IBinder?) { if (tapPending) { tapPending = false