Properly handle missing bluetooth permissions

This commit is contained in:
Mygod
2021-06-11 02:15:49 +08:00
parent 6f81f8a6ff
commit 440b22faa1
3 changed files with 21 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
package be.mygod.vpnhotspot.manage package be.mygod.vpnhotspot.manage
import android.annotation.SuppressLint
import android.annotation.TargetApi import android.annotation.TargetApi
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager 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 * https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384
*/ */
@SuppressLint("MissingPermission")
@RequiresApi(24) @RequiresApi(24)
fun start(callback: TetheringManager.StartTetheringCallback) { fun start(callback: TetheringManager.StartTetheringCallback) {
if (pendingCallback == null) try { if (pendingCallback == null) try {

View File

@@ -62,12 +62,16 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
app.logEvent("manage_write_settings") { param("message", e.toString()) } app.logEvent("manage_write_settings") { param("message", e.toString()) }
} }
if (manager.isStarted) try { when (manager.isStarted) {
manager.stop() true -> try {
} catch (e: InvocationTargetException) { manager.stop()
if (e.targetException !is SecurityException) Timber.w(e) } catch (e: InvocationTargetException) {
manager.onException(e) if (e.targetException !is SecurityException) Timber.w(e)
} else manager.start() 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 icon get() = tetherType.icon
override val title get() = this@TetherManager.title override val title get() = this@TetherManager.title
override val text get() = this@TetherManager.text override val text get() = this@TetherManager.text
override val active get() = isStarted override val active get() = isStarted == true
} }
val data = Data() val data = Data()
abstract val title: CharSequence abstract val title: CharSequence
abstract val tetherType: TetherType 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 open val text: CharSequence get() = baseError ?: ""
protected var baseError: String? = null protected var baseError: String? = null
@@ -92,6 +96,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
protected abstract fun start() protected abstract fun start()
protected abstract fun stop() protected abstract fun stop()
protected open fun onClickNull(): Unit = throw UnsupportedOperationException()
override fun onTetheringStarted() = data.notifyChange() override fun onTetheringStarted() = data.notifyChange()
override fun onTetheringFailed(error: Int?) { 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 title get() = parent.getString(R.string.tethering_manage_bluetooth)
override val tetherType get() = TetherType.BLUETOOTH override val tetherType get() = TetherType.BLUETOOTH
override val type get() = VIEW_TYPE_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( override val text get() = listOfNotNull(
if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null, if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null,
baseError).joinToString("\n") baseError).joinToString("\n")
@@ -324,6 +329,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
tethering.stop(this::onException) tethering.stop(this::onException)
onTetheringStarted() // force flush state onTetheringStarted() // force flush state
} }
override fun onClickNull() = ManageBar.start(parent.requireContext())
} }
@RequiresApi(30) @RequiresApi(30)
class Ethernet(parent: TetheringFragment) : TetherManager(parent) { class Ethernet(parent: TetheringFragment) : TetherManager(parent) {

View File

@@ -186,7 +186,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
icon = tileOff icon = tileOff
} }
null -> { null -> {
state = Tile.STATE_UNAVAILABLE state = Tile.STATE_INACTIVE
icon = tileOff icon = tileOff
subtitle(tethering?.activeFailureCause?.readableMessage) subtitle(tethering?.activeFailureCause?.readableMessage)
} }
@@ -197,7 +197,8 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
} }
override fun onClick() { override fun onClick() {
when (tethering?.active) { val tethering = tethering
if (tethering == null) tapPending = true else when (tethering.active) {
true -> { true -> {
val binder = binder val binder = binder
if (binder == null) tapPending = true else { if (binder == null) tapPending = true else {
@@ -211,7 +212,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
} }
} }
false -> start() false -> start()
else -> tapPending = true else -> ManageBar.start(this)
} }
} }
} }