Handle permission issues in BluetoothTethering

This commit is contained in:
Mygod
2019-03-19 10:14:55 +08:00
parent 16688ebe7d
commit e81e66b84f
3 changed files with 24 additions and 1 deletions

View File

@@ -8,12 +8,16 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.TetheringManager import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import timber.log.Timber import timber.log.Timber
import java.io.IOException
import java.lang.reflect.InvocationTargetException
class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) : class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
BluetoothProfile.ServiceListener, AutoCloseable { BluetoothProfile.ServiceListener, AutoCloseable {
@@ -39,8 +43,23 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
@TargetApi(24) @TargetApi(24)
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.bluetoothState) { when (intent?.bluetoothState) {
BluetoothAdapter.STATE_ON -> { BluetoothAdapter.STATE_ON -> try {
TetheringManager.start(TetheringManager.TETHERING_BLUETOOTH, true, pendingCallback!!) 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 -> { } BluetoothAdapter.STATE_OFF, BluetoothAdapter.ERROR -> { }
else -> return // ignore transition states else -> return // ignore transition states

View File

@@ -163,6 +163,8 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
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 == true
override fun onException() = ManageBar.start(parent.requireContext())
override fun start() = BluetoothTethering.start(this) override fun start() = BluetoothTethering.start(this)
override fun stop() { override fun stop() {
TetheringManager.stop(TetheringManager.TETHERING_BLUETOOTH) TetheringManager.stop(TetheringManager.TETHERING_BLUETOOTH)

View File

@@ -30,6 +30,8 @@ object TetheringManager {
* Called when starting tethering failed. * Called when starting tethering failed.
*/ */
fun onTetheringFailed() { } fun onTetheringFailed() { }
fun onException() { }
} }
/** /**