Stop using unsupported API for bluetooth
This commit is contained in:
@@ -151,8 +151,6 @@ API restrictions are updated up to [commit `ebe7044`](https://android.googlesour
|
|||||||
|
|
||||||
Greylisted/blacklisted APIs or internal constants: (some constants are hardcoded or implicitly used)
|
Greylisted/blacklisted APIs or internal constants: (some constants are hardcoded or implicitly used)
|
||||||
|
|
||||||
* (since API 24, prior to API 31) `Landroid/bluetooth/BluetoothPan;-><init>(Landroid/content/Context;Landroid/bluetooth/BluetoothProfile$ServiceListener;)V,unsupported`
|
|
||||||
* (since API 31) `Landroid/bluetooth/BluetoothPan;-><init>(Landroid/content/Context;Landroid/bluetooth/BluetoothProfile$ServiceListener;Landroid/bluetooth/BluetoothAdapter;)V,unsupported`
|
|
||||||
* (prior to API 30) `Landroid/net/ConnectivityManager;->getLastTetherError(Ljava/lang/String;)I,max-target-r`
|
* (prior to API 30) `Landroid/net/ConnectivityManager;->getLastTetherError(Ljava/lang/String;)I,max-target-r`
|
||||||
* (since API 30) `Landroid/net/ConnectivityModuleConnector;->IN_PROCESS_SUFFIX:Ljava/lang/String;`
|
* (since API 30) `Landroid/net/ConnectivityModuleConnector;->IN_PROCESS_SUFFIX:Ljava/lang/String;`
|
||||||
* (since API 30) `Landroid/net/TetheringManager$TetheringEventCallback;->onTetherableInterfaceRegexpsChanged(Landroid/net/TetheringManager$TetheringInterfaceRegexps;)V,blocked`
|
* (since API 30) `Landroid/net/TetheringManager$TetheringEventCallback;->onTetherableInterfaceRegexpsChanged(Landroid/net/TetheringManager$TetheringInterfaceRegexps;)V,blocked`
|
||||||
|
|||||||
@@ -29,24 +29,10 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
*/
|
*/
|
||||||
private const val PAN = 5
|
private const val PAN = 5
|
||||||
private val clazz by lazy { Class.forName("android.bluetooth.BluetoothPan") }
|
private val clazz by lazy { Class.forName("android.bluetooth.BluetoothPan") }
|
||||||
private val constructor by lazy {
|
|
||||||
(if (BuildCompat.isAtLeastS()) {
|
|
||||||
clazz.getDeclaredConstructor(Context::class.java, BluetoothProfile.ServiceListener::class.java,
|
|
||||||
BluetoothAdapter::class.java)
|
|
||||||
} else {
|
|
||||||
clazz.getDeclaredConstructor(Context::class.java, BluetoothProfile.ServiceListener::class.java)
|
|
||||||
}).apply {
|
|
||||||
isAccessible = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private val isTetheringOn by lazy { clazz.getDeclaredMethod("isTetheringOn") }
|
private val isTetheringOn by lazy { clazz.getDeclaredMethod("isTetheringOn") }
|
||||||
|
|
||||||
private val adapter = app.getSystemService<BluetoothManager>()?.adapter
|
private val adapter = app.getSystemService<BluetoothManager>()?.adapter
|
||||||
fun pan(context: Context, serviceListener: BluetoothProfile.ServiceListener) = (if (BuildCompat.isAtLeastS()) {
|
private val BluetoothProfile.isTetheringOn get() = isTetheringOn(this) as Boolean
|
||||||
constructor.newInstance(context, serviceListener, adapter)
|
|
||||||
} else constructor.newInstance(context, serviceListener)) as BluetoothProfile
|
|
||||||
val BluetoothProfile.isTetheringOn get() = isTetheringOn(this) as Boolean
|
|
||||||
fun BluetoothProfile.closePan() = adapter!!.closeProfileProxy(PAN, this)
|
|
||||||
|
|
||||||
private fun registerBluetoothStateListener(receiver: BroadcastReceiver) =
|
private fun registerBluetoothStateListener(receiver: BroadcastReceiver) =
|
||||||
app.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
app.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
||||||
@@ -70,6 +56,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var proxyCreated = false
|
||||||
private var connected = false
|
private var connected = false
|
||||||
private var pan: BluetoothProfile? = null
|
private var pan: BluetoothProfile? = null
|
||||||
private var stoppedByUser = false
|
private var stoppedByUser = false
|
||||||
@@ -98,12 +85,13 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
private val receiver = broadcastReceiver { _, _ -> stateListener() }
|
private val receiver = broadcastReceiver { _, _ -> stateListener() }
|
||||||
|
|
||||||
fun ensureInit(context: Context) {
|
fun ensureInit(context: Context) {
|
||||||
|
val adapter = adapter ?: return
|
||||||
activeFailureCause = null
|
activeFailureCause = null
|
||||||
if (pan == null) try {
|
if (!proxyCreated) try {
|
||||||
pan = pan(context, this)
|
check(adapter.getProfileProxy(context, this, PAN))
|
||||||
} catch (e: ReflectiveOperationException) {
|
proxyCreated = true
|
||||||
if (e.cause is SecurityException && BuildCompat.isAtLeastS()) Timber.d(e.readableMessage)
|
} catch (e: SecurityException) {
|
||||||
else Timber.w(e)
|
if (BuildCompat.isAtLeastS()) Timber.d(e.readableMessage) else Timber.w(e)
|
||||||
activeFailureCause = e
|
activeFailureCause = e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,6 +105,7 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
stoppedByUser = false
|
stoppedByUser = false
|
||||||
}
|
}
|
||||||
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
|
||||||
|
pan = proxy
|
||||||
connected = true
|
connected = true
|
||||||
stateListener()
|
stateListener()
|
||||||
}
|
}
|
||||||
@@ -146,6 +135,6 @@ class BluetoothTethering(context: Context, val stateListener: () -> Unit) :
|
|||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
app.unregisterReceiver(receiver)
|
app.unregisterReceiver(receiver)
|
||||||
pan?.closePan()
|
adapter!!.closeProfileProxy(PAN, pan)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user