diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt index d20085be..de626edb 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -1,5 +1,6 @@ package be.mygod.vpnhotspot.manage +import android.annotation.SuppressLint import android.annotation.TargetApi import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothManager @@ -123,6 +124,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : /** * https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384 */ + @SuppressLint("MissingPermission") @RequiresApi(24) fun start(callback: TetheringManager.StartTetheringCallback) { if (pendingCallback == null) try { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt index ac9981ec..86290ca2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -62,12 +62,16 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), } catch (e: RuntimeException) { app.logEvent("manage_write_settings") { param("message", e.toString()) } } - if (manager.isStarted) try { - manager.stop() - } catch (e: InvocationTargetException) { - if (e.targetException !is SecurityException) Timber.w(e) - manager.onException(e) - } else manager.start() + when (manager.isStarted) { + true -> try { + manager.stop() + } catch (e: InvocationTargetException) { + if (e.targetException !is SecurityException) Timber.w(e) + manager.onException(e) + } + false -> manager.start() + null -> manager.onClickNull() + } } } @@ -78,13 +82,13 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), override val icon get() = tetherType.icon override val title get() = this@TetherManager.title override val text get() = this@TetherManager.text - override val active get() = isStarted + override val active get() = isStarted == true } val data = Data() abstract val title: CharSequence abstract val tetherType: TetherType - open val isStarted get() = parent.enabledTypes.contains(tetherType) + open val isStarted: Boolean? get() = parent.enabledTypes.contains(tetherType) protected open val text: CharSequence get() = baseError ?: "" protected var baseError: String? = null @@ -92,6 +96,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), protected abstract fun start() protected abstract fun stop() + protected open fun onClickNull(): Unit = throw UnsupportedOperationException() override fun onTetheringStarted() = data.notifyChange() override fun onTetheringFailed(error: Int?) { @@ -314,7 +319,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), override val title get() = parent.getString(R.string.tethering_manage_bluetooth) override val tetherType get() = TetherType.BLUETOOTH override val type get() = VIEW_TYPE_BLUETOOTH - override val isStarted get() = tethering.active == true + override val isStarted get() = tethering.active override val text get() = listOfNotNull( if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null, baseError).joinToString("\n") @@ -324,6 +329,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), tethering.stop(this::onException) onTetheringStarted() // force flush state } + override fun onClickNull() = ManageBar.start(parent.requireContext()) } @RequiresApi(30) class Ethernet(parent: TetheringFragment) : TetherManager(parent) { 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 ed0c74be..4efd8174 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -186,7 +186,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin icon = tileOff } null -> { - state = Tile.STATE_UNAVAILABLE + state = Tile.STATE_INACTIVE icon = tileOff subtitle(tethering?.activeFailureCause?.readableMessage) } @@ -197,7 +197,8 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin } override fun onClick() { - when (tethering?.active) { + val tethering = tethering + if (tethering == null) tapPending = true else when (tethering.active) { true -> { val binder = binder if (binder == null) tapPending = true else { @@ -211,7 +212,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin } } false -> start() - else -> tapPending = true + else -> ManageBar.start(this) } } }