diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt deleted file mode 100644 index 49ffff08..00000000 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherListeningTileService.kt +++ /dev/null @@ -1,32 +0,0 @@ -package be.mygod.vpnhotspot.manage - -import android.content.IntentFilter -import androidx.annotation.RequiresApi -import be.mygod.vpnhotspot.net.TetheringManager -import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces -import be.mygod.vpnhotspot.util.KillableTileService -import be.mygod.vpnhotspot.util.broadcastReceiver - -@RequiresApi(24) -abstract class TetherListeningTileService : KillableTileService() { - protected var tethered: List? = null - - private val receiver = broadcastReceiver { _, intent -> - tethered = intent.tetheredIfaces ?: return@broadcastReceiver - updateTile() - } - - override fun onStartListening() { - super.onStartListening() - tethered = registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) - ?.tetheredIfaces - updateTile() - } - - override fun onStopListening() { - unregisterReceiver(receiver) - super.onStopListening() - } - - protected abstract fun 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 983378f0..55bb7b44 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.manage import android.content.ComponentName import android.content.Context import android.content.Intent +import android.content.IntentFilter import android.graphics.drawable.Icon import android.os.IBinder import android.service.quicksettings.Tile @@ -13,7 +14,10 @@ import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.TetheringService import be.mygod.vpnhotspot.net.TetherType import be.mygod.vpnhotspot.net.TetheringManager +import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces import be.mygod.vpnhotspot.net.wifi.WifiApManager +import be.mygod.vpnhotspot.util.KillableTileService +import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.stopAndUnbind import timber.log.Timber @@ -21,27 +25,37 @@ import java.io.IOException import java.lang.reflect.InvocationTargetException @RequiresApi(24) -sealed class TetheringTileService : TetherListeningTileService(), TetheringManager.OnStartTetheringCallback { +sealed class TetheringTileService : KillableTileService(), TetheringManager.OnStartTetheringCallback { protected val tileOff by lazy { Icon.createWithResource(application, icon) } protected val tileOn by lazy { Icon.createWithResource(application, R.drawable.ic_quick_settings_tile_on) } protected abstract val labelString: Int protected abstract val tetherType: TetherType protected open val icon get() = tetherType.icon + protected var tethered: List? = null protected val interested get() = tethered?.filter { TetherType.ofInterface(it) == tetherType } protected var binder: TetheringService.Binder? = null + private val receiver = broadcastReceiver { _, intent -> + tethered = intent.tetheredIfaces ?: return@broadcastReceiver + updateTile() + } + protected abstract fun start() protected abstract fun stop() override fun onStartListening() { - bindService(Intent(this, TetheringService::class.java), this, Context.BIND_AUTO_CREATE) super.onStartListening() + bindService(Intent(this, TetheringService::class.java), this, Context.BIND_AUTO_CREATE) + tethered = registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED)) + ?.tetheredIfaces + updateTile() } override fun onStopListening() { - super.onStopListening() + unregisterReceiver(receiver) stopAndUnbind(this) + super.onStopListening() } override fun onServiceConnected(name: ComponentName?, service: IBinder?) { @@ -55,7 +69,7 @@ sealed class TetheringTileService : TetherListeningTileService(), TetheringManag binder = null } - override fun updateTile() { + protected open fun updateTile() { qsTile?.run { val interested = interested when {