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.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<String?, Resources>.getRegexs(name: String) = second.getIdentifier(name, "array", first).let {
if (it == 0) {
private fun Pair<String, Resources>.getRegexs(name: String, alternativePackage: String? = null): List<Pattern> {
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)

View File

@@ -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)

View File

@@ -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 {