From 54c9ae0ceccffd82490b4ad970369b4813c874be Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 11 Jun 2021 01:46:51 +0800 Subject: [PATCH] Fix turning off bluetooth tethering --- .../vpnhotspot/manage/BluetoothTethering.kt | 48 ++++++++++++------- .../mygod/vpnhotspot/manage/TetherManager.kt | 5 +- .../vpnhotspot/manage/TetheringTileService.kt | 5 +- 3 files changed, 34 insertions(+), 24 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 cedc9c0a..4f79aa44 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -67,27 +67,11 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : pendingCallback = null app.unregisterReceiver(this) } - - /** - * https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384 - */ - @RequiresApi(24) - fun start(callback: TetheringManager.StartTetheringCallback) { - if (pendingCallback != null) return - try { - if (adapter?.state == BluetoothAdapter.STATE_OFF) { - registerBluetoothStateListener(this) - pendingCallback = callback - adapter.enable() - } else TetheringManager.startTethering(TetheringManager.TETHERING_BLUETOOTH, true, callback) - } catch (e: SecurityException) { - SmartSnackbar.make(e.readableMessage).shortToast().show() - } - } } private var connected = false private var pan: BluetoothProfile? = null + private var stoppedByUser = false var activeFailureCause: Throwable? = null /** * Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java @@ -96,7 +80,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : val pan = pan ?: return null if (!connected) return null activeFailureCause = null - return adapter?.state == BluetoothAdapter.STATE_ON && try { + val on = adapter?.state == BluetoothAdapter.STATE_ON && try { pan.isTetheringOn } catch (e: InvocationTargetException) { activeFailureCause = e.cause ?: e @@ -104,6 +88,10 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : else Timber.w(e) return null } + return if (stoppedByUser) { + if (!on) stoppedByUser = false + false + } else on } private val receiver = broadcastReceiver { _, _ -> stateListener() } @@ -125,11 +113,35 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) : override fun onServiceDisconnected(profile: Int) { connected = false + stoppedByUser = false } override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { connected = true stateListener() } + + /** + * https://android.googlesource.com/platform/packages/apps/Settings/+/b1af85d/src/com/android/settings/TetherSettings.java#384 + */ + @RequiresApi(24) + fun start(callback: TetheringManager.StartTetheringCallback) { + if (pendingCallback != null) return + try { + if (adapter?.state == BluetoothAdapter.STATE_OFF) { + registerBluetoothStateListener(BluetoothTethering) + pendingCallback = callback + adapter.enable() + } else TetheringManager.startTethering(TetheringManager.TETHERING_BLUETOOTH, true, callback) + } catch (e: SecurityException) { + SmartSnackbar.make(e.readableMessage).shortToast().show() + } + } + @RequiresApi(24) + fun stop(callback: (Exception) -> Unit) { + TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, callback) + stoppedByUser = true + } + override fun close() { app.unregisterReceiver(receiver) pan?.closePan() 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 55dd363f..ac9981ec 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -319,10 +319,9 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null, baseError).joinToString("\n") - override fun start() = BluetoothTethering.start(this) + override fun start() = tethering.start(this) override fun stop() { - TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, this::onException) - Thread.sleep(1) // give others a room to breathe + tethering.stop(this::onException) onTetheringStarted() // force flush state } } 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 6483beea..ed0c74be 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetheringTileService.kt @@ -151,10 +151,9 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin override val labelString get() = R.string.tethering_manage_bluetooth override val tetherType get() = TetherType.BLUETOOTH - override fun start() = BluetoothTethering.start(this) + override fun start() = tethering!!.start(this) override fun stop() { - TetheringManager.stopTethering(TetheringManager.TETHERING_BLUETOOTH, this::onException) - Thread.sleep(1) // give others a room to breathe + tethering!!.stop(this::onException) onTetheringStarted() // force flush state }