Fix resources possibly defined under a different package name

This commit is contained in:
Mygod
2021-04-16 14:40:32 -04:00
parent 56155df3b8
commit 446ab8e624
3 changed files with 26 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.util.Event0 import be.mygod.vpnhotspot.util.Event0
import be.mygod.vpnhotspot.util.findIdentifier
import timber.log.Timber import timber.log.Timber
import java.util.regex.Pattern import java.util.regex.Pattern
@@ -40,12 +41,13 @@ enum class TetherType(@DrawableRes val icon: Int) {
@RequiresApi(30) // unused on lower APIs @RequiresApi(30) // unused on lower APIs
val listener = Event0() val listener = Event0()
private fun Pair<String?, Resources>.getRegexs(name: String) = second.getIdentifier(name, "array", first).let { private fun Pair<String, Resources>.getRegexs(name: String, alternativePackage: String? = null): List<Pattern> {
if (it == 0) { 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)) if (name == "config_tether_wigig_regexs") Timber.i("$name is empty") else Timber.w(Exception(name))
emptyList() emptyList()
} else try { } else try {
second.getStringArray(it).filterNotNull().map { it.toPattern() } second.getStringArray(id).filterNotNull().map { it.toPattern() }
} catch (_: Resources.NotFoundException) { } catch (_: Resources.NotFoundException) {
Timber.w(Exception("$name not found")) Timber.w(Exception("$name not found"))
emptyList() emptyList()
@@ -57,14 +59,15 @@ enum class TetherType(@DrawableRes val icon: Int) {
if (!requiresUpdate) return@synchronized if (!requiresUpdate) return@synchronized
requiresUpdate = false requiresUpdate = false
TetheringManager.registerTetheringEventCallback(null, this) TetheringManager.registerTetheringEventCallback(null, this)
val tethering = "com.android.networkstack.tethering" to app.packageManager.getResourcesForApplication( val info = TetheringManager.resolvedService.serviceInfo
TetheringManager.resolvedService.serviceInfo.applicationInfo) val tethering = "com.android.networkstack.tethering" to
usbRegexs = tethering.getRegexs("config_tether_usb_regexs") app.packageManager.getResourcesForApplication(info.applicationInfo)
wifiRegexs = tethering.getRegexs("config_tether_wifi_regexs") usbRegexs = tethering.getRegexs("config_tether_usb_regexs", info.packageName)
wigigRegexs = tethering.getRegexs("config_tether_wigig_regexs") wifiRegexs = tethering.getRegexs("config_tether_wifi_regexs", info.packageName)
wifiP2pRegexs = tethering.getRegexs("config_tether_wifi_p2p_regexs") wigigRegexs = tethering.getRegexs("config_tether_wigig_regexs", info.packageName)
bluetoothRegexs = tethering.getRegexs("config_tether_bluetooth_regexs") wifiP2pRegexs = tethering.getRegexs("config_tether_wifi_p2p_regexs", info.packageName)
ncmRegexs = tethering.getRegexs("config_tether_ncm_regexs") bluetoothRegexs = tethering.getRegexs("config_tether_bluetooth_regexs", info.packageName)
ncmRegexs = tethering.getRegexs("config_tether_ncm_regexs", info.packageName)
} }
@RequiresApi(30) @RequiresApi(30)

View File

@@ -7,6 +7,7 @@ import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.root.SettingsGlobalPut import be.mygod.vpnhotspot.root.SettingsGlobalPut
import be.mygod.vpnhotspot.util.findIdentifier
import kotlinx.coroutines.* import kotlinx.coroutines.*
import timber.log.Timber import timber.log.Timber
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@@ -41,10 +42,11 @@ class TetherTimeoutMonitor(private val timeout: Long = 0,
val delay = if (Build.VERSION.SDK_INT >= 28) try { val delay = if (Build.VERSION.SDK_INT >= 28) try {
if (Build.VERSION.SDK_INT < 30) Resources.getSystem().run { if (Build.VERSION.SDK_INT < 30) Resources.getSystem().run {
getInteger(getIdentifier("config_wifi_framework_soft_ap_timeout_delay", "integer", "android")) getInteger(getIdentifier("config_wifi_framework_soft_ap_timeout_delay", "integer", "android"))
} else app.packageManager.getResourcesForApplication(WifiApManager.resolvedActivity.activityInfo } else {
.applicationInfo).run { val info = WifiApManager.resolvedActivity.activityInfo
getInteger(getIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds", "integer", val resources = app.packageManager.getResourcesForApplication(info.applicationInfo)
"com.android.wifi.resources")) resources.getInteger(resources.findIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds",
"integer", "com.android.wifi.resources", info.packageName))
} }
} catch (e: Resources.NotFoundException) { } catch (e: Resources.NotFoundException) {
Timber.w(e) Timber.w(e)

View File

@@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.util
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi import android.annotation.TargetApi
import android.content.* import android.content.*
import android.content.res.Resources
import android.net.InetAddresses import android.net.InetAddresses
import android.net.LinkProperties import android.net.LinkProperties
import android.net.RouteInfo import android.net.RouteInfo
@@ -134,6 +135,11 @@ var MenuItem.isNotGone: Boolean
isEnabled = value 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) @get:RequiresApi(26)
private val newLookup by lazy @TargetApi(26) { private val newLookup by lazy @TargetApi(26) {
MethodHandles.Lookup::class.java.getDeclaredConstructor(Class::class.java, Int::class.java).apply { MethodHandles.Lookup::class.java.getDeclaredConstructor(Class::class.java, Int::class.java).apply {