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
val pan = pan ?: return null
return BluetoothAdapter.getDefaultAdapter()?.state == BluetoothAdapter.STATE_ON && try {
isTetheringOn.invoke(pan) as Boolean
isTetheringOn(pan) as Boolean
} catch (e: InvocationTargetException) {
activeFailureCause = e.cause ?: 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 ->
// setting exemption requires TETHER_PRIVILEGED permission
if (app.checkSelfPermission("android.permission.TETHER_PRIVILEGED") ==
PackageManager.PERMISSION_GRANTED) setExemptFromEntitlementCheck.invoke(builder, true)
setShouldShowEntitlementUi.invoke(builder, showProvisioningUi)
PackageManager.PERMISSION_GRANTED) setExemptFromEntitlementCheck(builder, true)
setShouldShowEntitlementUi(builder, showProvisioningUi)
if (address != null) {
val (localIPv4Address, clientAddress) = address
setStaticIpv4Addresses(builder, localIPv4Address, clientAddress)
}
build.invoke(builder)
build(builder)
}
val proxy = Proxy.newProxyInstance(interfaceStartTetheringCallback.classLoader,
arrayOf(interfaceStartTetheringCallback), object : InvocationHandler {
@@ -273,7 +273,7 @@ object TetheringManager {
}
}
})
startTethering.invoke(instance, request, handler.makeExecutor(), proxy)
startTethering(instance, request, handler.makeExecutor(), proxy)
return
} catch (e: InvocationTargetException) {
Timber.w(e, "Unable to invoke TetheringManager.startTethering, falling back to ConnectivityManager")
@@ -290,7 +290,7 @@ object TetheringManager {
}
}
}.build()
startTetheringLegacy.invoke(app.connectivity, type, showProvisioningUi, proxy, handler)
startTetheringLegacy(app.connectivity, type, showProvisioningUi, proxy, handler)
}
/**
@@ -305,11 +305,11 @@ object TetheringManager {
@RequiresApi(24)
fun stopTethering(type: Int) {
if (BuildCompat.isAtLeastR()) try {
stopTethering.invoke(instance, type)
stopTethering(instance, type)
} catch (e: InvocationTargetException) {
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
@@ -500,7 +500,7 @@ object TetheringManager {
@RequiresApi(30)
fun unregisterTetheringEventCallback(callback: TetheringEventCallback) {
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
* 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
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)
}
var configuration: WifiConfiguration
get() = getWifiApConfiguration.invoke(app.wifi) as? WifiConfiguration ?: WifiConfiguration()
set(value) {
require(setWifiApConfiguration.invoke(app.wifi, value) as? Boolean == true) {
get() = getWifiApConfiguration(app.wifi) as? WifiConfiguration ?: WifiConfiguration()
set(value) = require(setWifiApConfiguration(app.wifi, value) as? Boolean == true) {
"setWifiApConfiguration failed"
}
}
private val cancelLocalOnlyHotspotRequest by lazy {
WifiManager::class.java.getDeclaredMethod("cancelLocalOnlyHotspotRequest")
}
fun cancelLocalOnlyHotspotRequest() = cancelLocalOnlyHotspotRequest.invoke(app.wifi)
fun cancelLocalOnlyHotspotRequest() = cancelLocalOnlyHotspotRequest(app.wifi)
private val setWifiApEnabled by lazy {
WifiManager::class.java.getDeclaredMethod("setWifiApEnabled",
@@ -37,7 +35,7 @@ object WifiApManager {
* @return {@code true} if the operation succeeds, {@code false} otherwise
*/
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.

View File

@@ -28,7 +28,7 @@ object WifiP2pManagerHelper {
fun WifiP2pManager.setWifiP2pChannels(c: WifiP2pManager.Channel, lc: Int, oc: Int,
listener: WifiP2pManager.ActionListener) {
try {
setWifiP2pChannels.invoke(this, c, lc, oc, listener)
setWifiP2pChannels(this, c, lc, oc, listener)
} catch (_: NoSuchMethodException) {
app.logEvent("NoSuchMethod_setWifiP2pChannels")
listener.onFailure(UNSUPPORTED)
@@ -51,7 +51,7 @@ object WifiP2pManagerHelper {
}
}
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,
listener: WifiP2pManager.ActionListener) {
try {
deletePersistentGroup.invoke(this, c, netId, listener)
deletePersistentGroup(this, c, netId, listener)
} catch (_: NoSuchMethodException) {
app.logEvent("NoSuchMethod_deletePersistentGroup")
listener.onFailure(UNSUPPORTED)
@@ -96,11 +96,11 @@ object WifiP2pManagerHelper {
override fun invoke(proxy: Any, method: Method, args: Array<out Any?>?): Any? = when (method.name) {
"onPersistentGroupInfoAvailable" -> {
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)
}
})
requestPersistentGroupInfo.invoke(this, c, proxy)
requestPersistentGroupInfo(this, c, proxy)
}
}

View File

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