Correctly handle tethering package in mainline module

This commit is contained in:
Mygod
2020-05-29 05:36:20 +08:00
parent 6e848caa87
commit 382291a74e
2 changed files with 39 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package be.mygod.vpnhotspot.net
import android.content.res.Resources import android.content.res.Resources
import androidx.core.os.BuildCompat import androidx.core.os.BuildCompat
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import java.util.regex.Pattern import java.util.regex.Pattern
@@ -33,24 +34,35 @@ enum class TetherType {
private val ncmRegexs: List<Pattern> private val ncmRegexs: List<Pattern>
private val ethernetRegex: Pattern? private val ethernetRegex: Pattern?
private fun Pair<String?, Resources>.getRegexs(name: String) = second
.getStringArray(second.getIdentifier(name, "array", first))
.filterNotNull()
.map { it.toPattern() }
/** /**
* Source: https://android.googlesource.com/platform/frameworks/base/+/32e772f/packages/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java#93 * Source: https://android.googlesource.com/platform/frameworks/base/+/32e772f/packages/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java#93
*/ */
init { init {
val system = Resources.getSystem() val system = "android" to Resources.getSystem()
fun getRegexs(name: String) = system if (BuildCompat.isAtLeastR()) {
.getStringArray(system.getIdentifier(name, "array", "android")) val tethering = TetheringManager.PACKAGE to app.packageManager.getResourcesForApplication(
.filterNotNull() TetheringManager.resolvedService.serviceInfo.applicationInfo)
.map { it.toPattern() } usbRegexs = tethering.getRegexs("config_tether_usb_regexs")
usbRegexs = getRegexs("config_tether_usb_regexs") wifiRegexs = tethering.getRegexs("config_tether_wifi_regexs")
wifiRegexs = getRegexs("config_tether_wifi_regexs") wifiP2pRegexs = tethering.getRegexs("config_tether_wifi_p2p_regexs")
wifiP2pRegexs = if (BuildCompat.isAtLeastR()) getRegexs("config_tether_wifi_p2p_regexs") else emptyList() bluetoothRegexs = tethering.getRegexs("config_tether_bluetooth_regexs")
wimaxRegexs = getRegexs("config_tether_wimax_regexs") ncmRegexs = tethering.getRegexs("config_tether_ncm_regexs")
bluetoothRegexs = getRegexs("config_tether_bluetooth_regexs") } else {
ncmRegexs = if (BuildCompat.isAtLeastR()) getRegexs("config_tether_ncm_regexs") else emptyList() usbRegexs = system.getRegexs("config_tether_usb_regexs")
wifiRegexs = system.getRegexs("config_tether_wifi_regexs")
wifiP2pRegexs = emptyList()
bluetoothRegexs = system.getRegexs("config_tether_bluetooth_regexs")
ncmRegexs = emptyList()
}
wimaxRegexs = system.getRegexs("config_tether_wimax_regexs")
// available since Android 4.0: https://android.googlesource.com/platform/frameworks/base/+/c96a667162fab44a250503caccb770109a9cb69a // available since Android 4.0: https://android.googlesource.com/platform/frameworks/base/+/c96a667162fab44a250503caccb770109a9cb69a
ethernetRegex = system.getString(system.getIdentifier("config_ethernet_iface_regex", "string", ethernetRegex = system.second.getString(system.second.getIdentifier(
"android")).run { if (isEmpty()) null else toPattern() } "config_ethernet_iface_regex", "string", system.first)).run { if (isEmpty()) null else toPattern() }
} }
/** /**

View File

@@ -1,5 +1,6 @@
package be.mygod.vpnhotspot.net package be.mygod.vpnhotspot.net
import android.annotation.TargetApi
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@@ -55,8 +56,15 @@ object TetheringManager {
* @hide * @hide
* @see android.net.TetheringManager * @see android.net.TetheringManager
*/ */
@RequiresApi(30)
const val TETHERING_SERVICE = "tethering" const val TETHERING_SERVICE = "tethering"
@RequiresApi(30)
const val PACKAGE = "com.android.networkstack.tethering"
@RequiresApi(30)
private const val TETHERING_CONNECTOR_CLASS = "android.net.ITetheringConnector"
/** /**
* This is a sticky broadcast since almost forever. * This is a sticky broadcast since almost forever.
* *
@@ -140,7 +148,12 @@ object TetheringManager {
@get:RequiresApi(30) @get:RequiresApi(30)
private val clazz by lazy { Class.forName("android.net.TetheringManager") } private val clazz by lazy { Class.forName("android.net.TetheringManager") }
@get:RequiresApi(30) @get:RequiresApi(30)
private val instance by lazy { app.getSystemService(TETHERING_SERVICE) } private val instance by lazy @TargetApi(30) { app.getSystemService(TETHERING_SERVICE) }
@get:RequiresApi(30)
val resolvedService by lazy @TargetApi(30) {
app.packageManager.queryIntentServices(Intent(TETHERING_CONNECTOR_CLASS),
PackageManager.MATCH_SYSTEM_ONLY).single()
}
@get:RequiresApi(24) @get:RequiresApi(24)
private val classOnStartTetheringCallback by lazy { private val classOnStartTetheringCallback by lazy {