From e81e66b84fbeeff67f02666ac7fd0681c75386ec Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 19 Mar 2019 10:14:55 +0800 Subject: [PATCH] Handle permission issues in BluetoothTethering --- .../vpnhotspot/manage/BluetoothTethering.kt | 21 ++++++++++++++++++- .../mygod/vpnhotspot/manage/TetherManager.kt | 2 ++ .../mygod/vpnhotspot/net/TetheringManager.kt | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) 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 22fa411f..b496bf63 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -8,12 +8,16 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.widget.Toast import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.util.broadcastReceiver +import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.widget.SmartSnackbar import timber.log.Timber +import java.io.IOException +import java.lang.reflect.InvocationTargetException class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) : BluetoothProfile.ServiceListener, AutoCloseable { @@ -39,8 +43,23 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) : @TargetApi(24) override fun onReceive(context: Context?, intent: Intent?) { when (intent?.bluetoothState) { - BluetoothAdapter.STATE_ON -> { + BluetoothAdapter.STATE_ON -> try { TetheringManager.start(TetheringManager.TETHERING_BLUETOOTH, true, pendingCallback!!) + } catch (e: IOException) { + Timber.w(e) + Toast.makeText(context, e.readableMessage, Toast.LENGTH_LONG).show() + pendingCallback!!.onException() + } catch (e: InvocationTargetException) { + if (e.targetException !is SecurityException) Timber.w(e) + var cause: Throwable? = e + while (cause != null) { + cause = cause.cause + if (cause != null && cause !is InvocationTargetException) { + Toast.makeText(context, cause.readableMessage, Toast.LENGTH_LONG).show() + pendingCallback!!.onException() + break + } + } } BluetoothAdapter.STATE_OFF, BluetoothAdapter.ERROR -> { } else -> return // ignore transition states 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 bdfa5414..f1a0a1ab 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -163,6 +163,8 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), override val type get() = VIEW_TYPE_BLUETOOTH override val isStarted get() = tethering.active == true + override fun onException() = ManageBar.start(parent.requireContext()) + override fun start() = BluetoothTethering.start(this) override fun stop() { TetheringManager.stop(TetheringManager.TETHERING_BLUETOOTH) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index 68a7a905..23d5b9d2 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -30,6 +30,8 @@ object TetheringManager { * Called when starting tethering failed. */ fun onTetheringFailed() { } + + fun onException() { } } /**