From 9e820e888fd98a327bec1e3f07913d06d3d29b82 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 30 May 2020 21:01:22 -0400 Subject: [PATCH] Use invoke operators --- .../vpnhotspot/manage/BluetoothTethering.kt | 2 +- .../mygod/vpnhotspot/net/TetheringManager.kt | 20 +++++++++---------- .../vpnhotspot/net/wifi/WifiApManager.kt | 12 +++++------ .../net/wifi/WifiP2pManagerHelper.kt | 10 +++++----- .../java/be/mygod/vpnhotspot/util/Utils.kt | 5 +++-- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt index e74dea6a..8722c23c 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/BluetoothTethering.kt @@ -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) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index 412d35c7..a1ea106e 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -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", diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt index 043b7aba..4036daae 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt @@ -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) { - "setWifiApConfiguration failed" - } + 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. diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pManagerHelper.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pManagerHelper.kt index 5d697a8e..9ca04fb0 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pManagerHelper.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pManagerHelper.kt @@ -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?): 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) + @Suppress("UNCHECKED_CAST") listener(getGroupList(args!![0]) as Collection) } else -> callSuper(interfacePersistentGroupInfoListener, proxy, method, args) } }) - requestPersistentGroupInfo.invoke(this, c, proxy) + requestPersistentGroupInfo(this, c, proxy) } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt index 10387731..cfa78883 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -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 {