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
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 {

View File

@@ -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) {

View File

@@ -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)
}
}
}