Handle bluetooth error message correctly
This commit is contained in:
@@ -160,6 +160,7 @@ Greylisted/blacklisted APIs or internal constants: (some constants are hardcoded
|
||||
Hidden whitelisted APIs: (same catch as above, however, things in this list are less likely to be broken)
|
||||
|
||||
* (since API 24) `Landroid/bluetooth/BluetoothPan;->isTetheringOn()Z,system-api,whitelist`
|
||||
* (since API 24) `Landroid/bluetooth/BluetoothProfile;->PAN:I,system-api,whitelist`
|
||||
* (since API 30) `Landroid/content/Context;->TETHERING_SERVICE:Ljava/lang/String;,system-api,whitelist`
|
||||
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;-><init>()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123103)
|
||||
* (since API 24, prior to API 30) [`Landroid/net/ConnectivityManager$OnStartTetheringCallback;->onTetheringFailed()V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123104)
|
||||
|
||||
@@ -19,12 +19,11 @@ 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: () -> Unit) :
|
||||
BluetoothProfile.ServiceListener, AutoCloseable {
|
||||
companion object : BroadcastReceiver() {
|
||||
/**
|
||||
* PAN Profile
|
||||
* From BluetoothProfile.java.
|
||||
*/
|
||||
private const val PAN = 5
|
||||
private val isTetheringOn by lazy {
|
||||
@@ -33,7 +32,6 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
|
||||
|
||||
private fun registerBluetoothStateListener(receiver: BroadcastReceiver) =
|
||||
app.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
||||
private val Intent.bluetoothState get() = getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
|
||||
|
||||
private var pendingCallback: TetheringManager.StartTetheringCallback? = null
|
||||
|
||||
@@ -42,7 +40,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
|
||||
*/
|
||||
@TargetApi(24)
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
when (intent?.bluetoothState) {
|
||||
when (intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
|
||||
BluetoothAdapter.STATE_ON -> try {
|
||||
TetheringManager.startTethering(TetheringManager.TETHERING_BLUETOOTH, true, pendingCallback!!)
|
||||
} catch (e: IOException) {
|
||||
@@ -100,7 +98,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
|
||||
}
|
||||
}
|
||||
|
||||
private val receiver = broadcastReceiver { _, intent -> stateListener(intent.bluetoothState) }
|
||||
private val receiver = broadcastReceiver { _, _ -> stateListener() }
|
||||
|
||||
init {
|
||||
try {
|
||||
@@ -117,6 +115,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
|
||||
}
|
||||
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
||||
pan = proxy
|
||||
stateListener()
|
||||
}
|
||||
override fun close() {
|
||||
app.unregisterReceiver(receiver)
|
||||
|
||||
@@ -137,7 +137,10 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
|
||||
}
|
||||
@RequiresApi(24)
|
||||
class Bluetooth(parent: TetheringFragment) : TetherManager(parent), DefaultLifecycleObserver {
|
||||
private val tethering = BluetoothTethering(parent.requireContext()) { onTetheringStarted() }
|
||||
private val tethering = BluetoothTethering(parent.requireContext()) {
|
||||
data.text = makeErrorMessage()
|
||||
data.notifyChange()
|
||||
}
|
||||
|
||||
init {
|
||||
parent.viewLifecycleOwner.lifecycle.addObserver(this)
|
||||
@@ -151,9 +154,15 @@ 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<String>) = listOfNotNull(
|
||||
|
||||
private var baseError: CharSequence? = null
|
||||
private fun makeErrorMessage(): CharSequence = listOfNotNull(
|
||||
if (tethering.active == null) tethering.activeFailureCause?.readableMessage else null,
|
||||
super.makeErrorMessage(errored).let { if (it.isEmpty()) null else it }).joinToString("\n")
|
||||
baseError).joinToString("\n")
|
||||
override fun makeErrorMessage(errored: List<String>): CharSequence {
|
||||
baseError = super.makeErrorMessage(errored).let { if (it.isEmpty()) null else it }
|
||||
return makeErrorMessage()
|
||||
}
|
||||
|
||||
override fun start() = BluetoothTethering.start(this)
|
||||
override fun stop() {
|
||||
|
||||
Reference in New Issue
Block a user