From 446ab8e624f49cb110f593b74b7bdda0ec04c1b5 Mon Sep 17 00:00:00 2001 From: Mygod Date: Fri, 16 Apr 2021 14:40:32 -0400 Subject: [PATCH] Fix resources possibly defined under a different package name --- .../be/mygod/vpnhotspot/net/TetherType.kt | 25 +++++++++++-------- .../net/monitor/TetherTimeoutMonitor.kt | 10 +++++--- .../java/be/mygod/vpnhotspot/util/Utils.kt | 6 +++++ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt index 048d30d6..30ee80da 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt @@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.util.Event0 +import be.mygod.vpnhotspot.util.findIdentifier import timber.log.Timber import java.util.regex.Pattern @@ -40,12 +41,13 @@ enum class TetherType(@DrawableRes val icon: Int) { @RequiresApi(30) // unused on lower APIs val listener = Event0() - private fun Pair.getRegexs(name: String) = second.getIdentifier(name, "array", first).let { - if (it == 0) { + private fun Pair.getRegexs(name: String, alternativePackage: String? = null): List { + val id = second.findIdentifier(name, "array", first, alternativePackage) + return if (id == 0) { if (name == "config_tether_wigig_regexs") Timber.i("$name is empty") else Timber.w(Exception(name)) emptyList() } else try { - second.getStringArray(it).filterNotNull().map { it.toPattern() } + second.getStringArray(id).filterNotNull().map { it.toPattern() } } catch (_: Resources.NotFoundException) { Timber.w(Exception("$name not found")) emptyList() @@ -57,14 +59,15 @@ enum class TetherType(@DrawableRes val icon: Int) { if (!requiresUpdate) return@synchronized requiresUpdate = false TetheringManager.registerTetheringEventCallback(null, this) - val tethering = "com.android.networkstack.tethering" to app.packageManager.getResourcesForApplication( - TetheringManager.resolvedService.serviceInfo.applicationInfo) - usbRegexs = tethering.getRegexs("config_tether_usb_regexs") - wifiRegexs = tethering.getRegexs("config_tether_wifi_regexs") - wigigRegexs = tethering.getRegexs("config_tether_wigig_regexs") - wifiP2pRegexs = tethering.getRegexs("config_tether_wifi_p2p_regexs") - bluetoothRegexs = tethering.getRegexs("config_tether_bluetooth_regexs") - ncmRegexs = tethering.getRegexs("config_tether_ncm_regexs") + val info = TetheringManager.resolvedService.serviceInfo + val tethering = "com.android.networkstack.tethering" to + app.packageManager.getResourcesForApplication(info.applicationInfo) + usbRegexs = tethering.getRegexs("config_tether_usb_regexs", info.packageName) + wifiRegexs = tethering.getRegexs("config_tether_wifi_regexs", info.packageName) + wigigRegexs = tethering.getRegexs("config_tether_wigig_regexs", info.packageName) + wifiP2pRegexs = tethering.getRegexs("config_tether_wifi_p2p_regexs", info.packageName) + bluetoothRegexs = tethering.getRegexs("config_tether_bluetooth_regexs", info.packageName) + ncmRegexs = tethering.getRegexs("config_tether_ncm_regexs", info.packageName) } @RequiresApi(30) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt index f3e0ee1b..7c39e695 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt @@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.root.SettingsGlobalPut +import be.mygod.vpnhotspot.util.findIdentifier import kotlinx.coroutines.* import timber.log.Timber import kotlin.coroutines.CoroutineContext @@ -41,10 +42,11 @@ class TetherTimeoutMonitor(private val timeout: Long = 0, val delay = if (Build.VERSION.SDK_INT >= 28) try { if (Build.VERSION.SDK_INT < 30) Resources.getSystem().run { getInteger(getIdentifier("config_wifi_framework_soft_ap_timeout_delay", "integer", "android")) - } else app.packageManager.getResourcesForApplication(WifiApManager.resolvedActivity.activityInfo - .applicationInfo).run { - getInteger(getIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds", "integer", - "com.android.wifi.resources")) + } else { + val info = WifiApManager.resolvedActivity.activityInfo + val resources = app.packageManager.getResourcesForApplication(info.applicationInfo) + resources.getInteger(resources.findIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds", + "integer", "com.android.wifi.resources", info.packageName)) } } catch (e: Resources.NotFoundException) { Timber.w(e) 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 83d53315..c0a3a20b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/util/Utils.kt @@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.util import android.annotation.SuppressLint import android.annotation.TargetApi import android.content.* +import android.content.res.Resources import android.net.InetAddresses import android.net.LinkProperties import android.net.RouteInfo @@ -134,6 +135,11 @@ var MenuItem.isNotGone: Boolean isEnabled = value } +fun Resources.findIdentifier(name: String, defType: String, defPackage: String, alternativePackage: String? = null) = + getIdentifier(name, defType, defPackage).let { + if (alternativePackage != null && it == 0) getIdentifier(name, defType, alternativePackage) else it + } + @get:RequiresApi(26) private val newLookup by lazy @TargetApi(26) { MethodHandles.Lookup::class.java.getDeclaredConstructor(Class::class.java, Int::class.java).apply {