From 9686645c032a5b28ba8e29547526e29f5bc7e286 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 29 May 2020 18:23:39 -0400 Subject: [PATCH] Propagate Bluetooth error message out to UI --- .../be/mygod/vpnhotspot/manage/BluetoothTethering.kt | 3 +++ .../java/be/mygod/vpnhotspot/manage/TetherManager.kt | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) 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 c8eea2ca..0f9c2e0e 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -84,14 +84,17 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) : } private var pan: BluetoothProfile? = null + var activeFailureCause: Throwable? = null /** * Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java */ val active: Boolean? get() { + activeFailureCause = null val pan = pan ?: return null return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try { isTetheringOn.invoke(pan) as Boolean } catch (e: InvocationTargetException) { + activeFailureCause = e if (e.cause is SecurityException && BuildCompat.isAtLeastR()) Timber.d(e) else Timber.w(e) return null } 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 603b6e17..e2b17360 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -109,9 +109,11 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), return e.readableMessage }) } + protected open fun makeErrorMessage(errored: List): CharSequence = errored + .filter { TetherType.ofInterface(it) == tetherType } + .joinToString("\n") { "$it: ${getErrorMessage(it)}" } fun updateErrorMessage(errored: List) { - data.text = errored.filter { TetherType.ofInterface(it) == tetherType } - .joinToString("\n") { "$it: ${getErrorMessage(it)}" } + data.text = makeErrorMessage(errored) data.notifyChange() } @@ -149,6 +151,9 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), override val isStarted get() = tethering.active == true override fun onException() = ManageBar.start(parent.context ?: app) + override fun makeErrorMessage(errored: List) = listOfNotNull( + if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null, + super.makeErrorMessage(errored).let { if (it.isEmpty()) null else it }).joinToString("\n") override fun start() = BluetoothTethering.start(this) override fun stop() {