Use invoke operators

This commit is contained in:
Mygod
2020-05-30 21:01:22 -04:00
parent 6e5714913c
commit 9e820e888f
5 changed files with 24 additions and 25 deletions

View File

@@ -92,7 +92,7 @@ class BluetoothTethering(context: Context, val stateListener: (Int) -> Unit) :
activeFailureCause = null activeFailureCause = null
val pan = pan ?: return null val pan = pan ?: return null
return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try { return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try {
isTetheringOn.invoke(pan) as Boolean isTetheringOn(pan) as Boolean
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
activeFailureCause = e.cause ?: e activeFailureCause = e.cause ?: e
if (e.cause is SecurityException && BuildCompat.isAtLeastR()) Timber.d(e) else Timber.w(e) if (e.cause is SecurityException && BuildCompat.isAtLeastR()) Timber.d(e) else Timber.w(e)

View File

@@ -248,13 +248,13 @@ object TetheringManager {
val request = newTetheringRequestBuilder.newInstance(type).let { builder -> val request = newTetheringRequestBuilder.newInstance(type).let { builder ->
// setting exemption requires TETHER_PRIVILEGED permission // setting exemption requires TETHER_PRIVILEGED permission
if (app.checkSelfPermission("android.permission.TETHER_PRIVILEGED") == if (app.checkSelfPermission("android.permission.TETHER_PRIVILEGED") ==
PackageManager.PERMISSION_GRANTED) setExemptFromEntitlementCheck.invoke(builder, true) PackageManager.PERMISSION_GRANTED) setExemptFromEntitlementCheck(builder, true)
setShouldShowEntitlementUi.invoke(builder, showProvisioningUi) setShouldShowEntitlementUi(builder, showProvisioningUi)
if (address != null) { if (address != null) {
val (localIPv4Address, clientAddress) = address val (localIPv4Address, clientAddress) = address
setStaticIpv4Addresses(builder, localIPv4Address, clientAddress) setStaticIpv4Addresses(builder, localIPv4Address, clientAddress)
} }
build.invoke(builder) build(builder)
} }
val proxy = Proxy.newProxyInstance(interfaceStartTetheringCallback.classLoader, val proxy = Proxy.newProxyInstance(interfaceStartTetheringCallback.classLoader,
arrayOf(interfaceStartTetheringCallback), object : InvocationHandler { arrayOf(interfaceStartTetheringCallback), object : InvocationHandler {
@@ -273,7 +273,7 @@ object TetheringManager {
} }
} }
}) })
startTethering.invoke(instance, request, handler.makeExecutor(), proxy) startTethering(instance, request, handler.makeExecutor(), proxy)
return return
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
Timber.w(e, "Unable to invoke TetheringManager.startTethering, falling back to ConnectivityManager") Timber.w(e, "Unable to invoke TetheringManager.startTethering, falling back to ConnectivityManager")
@@ -290,7 +290,7 @@ object TetheringManager {
} }
} }
}.build() }.build()
startTetheringLegacy.invoke(app.connectivity, type, showProvisioningUi, proxy, handler) startTetheringLegacy(app.connectivity, type, showProvisioningUi, proxy, handler)
} }
/** /**
@@ -305,11 +305,11 @@ object TetheringManager {
@RequiresApi(24) @RequiresApi(24)
fun stopTethering(type: Int) { fun stopTethering(type: Int) {
if (BuildCompat.isAtLeastR()) try { if (BuildCompat.isAtLeastR()) try {
stopTethering.invoke(instance, type) stopTethering(instance, type)
} catch (e: InvocationTargetException) { } catch (e: InvocationTargetException) {
Timber.w(e, "Unable to invoke TetheringManager.stopTethering, falling back to ConnectivityManager") Timber.w(e, "Unable to invoke TetheringManager.stopTethering, falling back to ConnectivityManager")
} }
stopTetheringLegacy.invoke(app.connectivity, type) stopTetheringLegacy(app.connectivity, type)
} }
/** /**
@@ -487,7 +487,7 @@ object TetheringManager {
}) })
} }
} }
registerTetheringEventCallback.invoke(instance, executor ?: null.makeExecutor(), proxy) registerTetheringEventCallback(instance, executor ?: null.makeExecutor(), proxy)
} }
/** /**
* Remove tethering event callback previously registered with * Remove tethering event callback previously registered with
@@ -500,7 +500,7 @@ object TetheringManager {
@RequiresApi(30) @RequiresApi(30)
fun unregisterTetheringEventCallback(callback: TetheringEventCallback) { fun unregisterTetheringEventCallback(callback: TetheringEventCallback) {
val proxy = synchronized(callbackMap) { callbackMap.remove(callback) } ?: return val proxy = synchronized(callbackMap) { callbackMap.remove(callback) } ?: return
unregisterTetheringEventCallback.invoke(instance, proxy) unregisterTetheringEventCallback(instance, proxy)
} }
/** /**
@@ -536,7 +536,7 @@ object TetheringManager {
* @return error The error code of the last error tethering or untethering the named * @return error The error code of the last error tethering or untethering the named
* interface * interface
*/ */
fun getLastTetherError(iface: String): Int = getLastTetherError.invoke(app.connectivity, iface) as Int fun getLastTetherError(iface: String): Int = getLastTetherError(app.connectivity, iface) as Int
// tether errors defined in ConnectivityManager up to Android 10 // tether errors defined in ConnectivityManager up to Android 10
private val tetherErrors29 = arrayOf("TETHER_ERROR_NO_ERROR", "TETHER_ERROR_UNKNOWN_IFACE", private val tetherErrors29 = arrayOf("TETHER_ERROR_NO_ERROR", "TETHER_ERROR_UNKNOWN_IFACE",

View File

@@ -10,17 +10,15 @@ object WifiApManager {
WifiManager::class.java.getDeclaredMethod("setWifiApConfiguration", WifiConfiguration::class.java) WifiManager::class.java.getDeclaredMethod("setWifiApConfiguration", WifiConfiguration::class.java)
} }
var configuration: WifiConfiguration var configuration: WifiConfiguration
get() = getWifiApConfiguration.invoke(app.wifi) as? WifiConfiguration ?: WifiConfiguration() get() = getWifiApConfiguration(app.wifi) as? WifiConfiguration ?: WifiConfiguration()
set(value) { set(value) = require(setWifiApConfiguration(app.wifi, value) as? Boolean == true) {
require(setWifiApConfiguration.invoke(app.wifi, value) as? Boolean == true) { "setWifiApConfiguration failed"
"setWifiApConfiguration failed"
}
} }
private val cancelLocalOnlyHotspotRequest by lazy { private val cancelLocalOnlyHotspotRequest by lazy {
WifiManager::class.java.getDeclaredMethod("cancelLocalOnlyHotspotRequest") WifiManager::class.java.getDeclaredMethod("cancelLocalOnlyHotspotRequest")
} }
fun cancelLocalOnlyHotspotRequest() = cancelLocalOnlyHotspotRequest.invoke(app.wifi) fun cancelLocalOnlyHotspotRequest() = cancelLocalOnlyHotspotRequest(app.wifi)
private val setWifiApEnabled by lazy { private val setWifiApEnabled by lazy {
WifiManager::class.java.getDeclaredMethod("setWifiApEnabled", WifiManager::class.java.getDeclaredMethod("setWifiApEnabled",
@@ -37,7 +35,7 @@ object WifiApManager {
* @return {@code true} if the operation succeeds, {@code false} otherwise * @return {@code true} if the operation succeeds, {@code false} otherwise
*/ */
private fun WifiManager.setWifiApEnabled(wifiConfig: WifiConfiguration?, enabled: Boolean) = private fun WifiManager.setWifiApEnabled(wifiConfig: WifiConfiguration?, enabled: Boolean) =
setWifiApEnabled.invoke(this, wifiConfig, enabled) as Boolean setWifiApEnabled(this, wifiConfig, enabled) as Boolean
/** /**
* Although the functionalities were removed in API 26, it is already not functioning correctly on API 25. * Although the functionalities were removed in API 26, it is already not functioning correctly on API 25.

View File

@@ -28,7 +28,7 @@ object WifiP2pManagerHelper {
fun WifiP2pManager.setWifiP2pChannels(c: WifiP2pManager.Channel, lc: Int, oc: Int, fun WifiP2pManager.setWifiP2pChannels(c: WifiP2pManager.Channel, lc: Int, oc: Int,
listener: WifiP2pManager.ActionListener) { listener: WifiP2pManager.ActionListener) {
try { try {
setWifiP2pChannels.invoke(this, c, lc, oc, listener) setWifiP2pChannels(this, c, lc, oc, listener)
} catch (_: NoSuchMethodException) { } catch (_: NoSuchMethodException) {
app.logEvent("NoSuchMethod_setWifiP2pChannels") app.logEvent("NoSuchMethod_setWifiP2pChannels")
listener.onFailure(UNSUPPORTED) listener.onFailure(UNSUPPORTED)
@@ -51,7 +51,7 @@ object WifiP2pManagerHelper {
} }
} }
fun WifiP2pManager.startWps(c: WifiP2pManager.Channel, wps: WpsInfo, listener: WifiP2pManager.ActionListener) { fun WifiP2pManager.startWps(c: WifiP2pManager.Channel, wps: WpsInfo, listener: WifiP2pManager.ActionListener) {
startWps!!.invoke(this, c, wps, listener) startWps!!(this, c, wps, listener)
} }
/** /**
@@ -66,7 +66,7 @@ object WifiP2pManagerHelper {
fun WifiP2pManager.deletePersistentGroup(c: WifiP2pManager.Channel, netId: Int, fun WifiP2pManager.deletePersistentGroup(c: WifiP2pManager.Channel, netId: Int,
listener: WifiP2pManager.ActionListener) { listener: WifiP2pManager.ActionListener) {
try { try {
deletePersistentGroup.invoke(this, c, netId, listener) deletePersistentGroup(this, c, netId, listener)
} catch (_: NoSuchMethodException) { } catch (_: NoSuchMethodException) {
app.logEvent("NoSuchMethod_deletePersistentGroup") app.logEvent("NoSuchMethod_deletePersistentGroup")
listener.onFailure(UNSUPPORTED) listener.onFailure(UNSUPPORTED)
@@ -96,11 +96,11 @@ object WifiP2pManagerHelper {
override fun invoke(proxy: Any, method: Method, args: Array<out Any?>?): Any? = when (method.name) { override fun invoke(proxy: Any, method: Method, args: Array<out Any?>?): Any? = when (method.name) {
"onPersistentGroupInfoAvailable" -> { "onPersistentGroupInfoAvailable" -> {
if (args?.size != 1) Timber.w(IllegalArgumentException("Unexpected args: $args")) if (args?.size != 1) Timber.w(IllegalArgumentException("Unexpected args: $args"))
@Suppress("UNCHECKED_CAST") listener(getGroupList.invoke(args!![0]) as Collection<WifiP2pGroup>) @Suppress("UNCHECKED_CAST") listener(getGroupList(args!![0]) as Collection<WifiP2pGroup>)
} }
else -> callSuper(interfacePersistentGroupInfoListener, proxy, method, args) else -> callSuper(interfacePersistentGroupInfoListener, proxy, method, args)
} }
}) })
requestPersistentGroupInfo.invoke(this, c, proxy) requestPersistentGroupInfo(this, c, proxy)
} }
} }

View File

@@ -112,8 +112,9 @@ private val parseNumericAddress by lazy @SuppressLint("SoonBlockedPrivateApi") {
isAccessible = true isAccessible = true
} }
} }
fun parseNumericAddress(address: String) = if (Build.VERSION.SDK_INT >= 29) fun parseNumericAddress(address: String) = if (Build.VERSION.SDK_INT >= 29) {
InetAddresses.parseNumericAddress(address) else parseNumericAddress.invoke(null, address) as InetAddress InetAddresses.parseNumericAddress(address)
} else parseNumericAddress(null, address) as InetAddress
fun Context.launchUrl(url: String) { fun Context.launchUrl(url: String) {
if (app.hasTouch) try { if (app.hasTouch) try {