Preliminary support for Ethernet and NCM tethering

This commit is contained in:
Mygod
2020-05-28 00:06:34 -04:00
parent 3314f01dc9
commit 1ca69cf3a6
12 changed files with 146 additions and 18 deletions

View File

@@ -1,12 +1,13 @@
package be.mygod.vpnhotspot.net
import android.content.res.Resources
import androidx.core.os.BuildCompat
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
import java.util.regex.Pattern
enum class TetherType {
NONE, WIFI_P2P, USB, WIFI, WIMAX, BLUETOOTH;
NONE, WIFI_P2P, USB, WIFI, WIMAX, BLUETOOTH, NCM, ETHERNET;
val icon get() = when (this) {
USB -> R.drawable.ic_device_usb
@@ -14,6 +15,9 @@ enum class TetherType {
WIFI -> R.drawable.ic_device_network_wifi
WIMAX -> R.drawable.ic_action_contactless
BLUETOOTH -> R.drawable.ic_device_bluetooth
// if you have an issue with these Ethernet icon namings, blame Google
NCM -> R.drawable.ic_action_settings_ethernet
ETHERNET -> R.drawable.ic_content_inbox
else -> R.drawable.ic_device_wifi_tethering
}
val isWifi get() = when (this) {
@@ -24,43 +28,48 @@ enum class TetherType {
companion object {
private val usbRegexs: List<Pattern>
private val wifiRegexs: List<Pattern>
private val wifiP2pRegexs: List<Pattern>
private val wimaxRegexs: List<Pattern>
private val bluetoothRegexs: List<Pattern>
private val ncmRegexs: List<Pattern>
private val ethernetRegex: Pattern?
/**
* Source: https://android.googlesource.com/platform/frameworks/base/+/61fa313/core/res/res/values/config.xml#328
* Source: https://android.googlesource.com/platform/frameworks/base/+/32e772f/packages/Tethering/src/com/android/networkstack/tethering/TetheringConfiguration.java#93
*/
init {
val appRes = app.resources
val sysRes = Resources.getSystem()
usbRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_usb_regexs", "array", "android"))
.filterNotNull()
.map { it.toPattern() }
wifiRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_wifi_regexs", "array", "android"))
.filterNotNull()
.map { it.toPattern() }
wimaxRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_wimax_regexs", "array", "android"))
.filterNotNull()
.map { it.toPattern() }
bluetoothRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_bluetooth_regexs", "array", "android"))
fun getRegexs(name: String) = appRes.getStringArray(sysRes
.getIdentifier(name, "array", "android"))
.filterNotNull()
.map { it.toPattern() }
usbRegexs = getRegexs("config_tether_usb_regexs")
wifiRegexs = getRegexs("config_tether_wifi_regexs")
wifiP2pRegexs = if (BuildCompat.isAtLeastR()) getRegexs("config_tether_wifi_p2p_regexs") else emptyList()
wimaxRegexs = getRegexs("config_tether_wimax_regexs")
bluetoothRegexs = getRegexs("config_tether_bluetooth_regexs")
ncmRegexs = if (BuildCompat.isAtLeastR()) getRegexs("config_tether_ncm_regexs") else emptyList()
ethernetRegex = if (BuildCompat.isAtLeastR()) {
appRes.getString(sysRes.getIdentifier("config_ethernet_iface_regex", "string", "android")).run {
if (isEmpty()) null else toPattern()
}
} else null
}
/**
* Based on: https://android.googlesource.com/platform/frameworks/base/+/0e3d092/services/core/java/com/android/server/connectivity/Tethering.java#311
* Based on: https://android.googlesource.com/platform/frameworks/base/+/5d36f01/packages/Tethering/src/com/android/networkstack/tethering/Tethering.java#479
*/
fun ofInterface(iface: String?, p2pDev: String? = null) = when {
iface == null -> NONE
iface == p2pDev -> WIFI_P2P
wifiRegexs.any { it.matcher(iface).matches() } -> WIFI
wifiP2pRegexs.any { it.matcher(iface).matches() } -> WIFI_P2P
usbRegexs.any { it.matcher(iface).matches() } -> USB
bluetoothRegexs.any { it.matcher(iface).matches() } -> BLUETOOTH
ncmRegexs.any { it.matcher(iface).matches() } -> NCM
wimaxRegexs.any { it.matcher(iface).matches() } -> WIMAX
ethernetRegex?.matcher(iface)?.matches() == true -> ETHERNET
else -> NONE
}
}

View File

@@ -79,6 +79,7 @@ object TetheringManager {
*/
const val EXTRA_ERRORED_TETHER = "erroredArray"
// tethering types supported by enableTetheringInternal: https://android.googlesource.com/platform/frameworks/base/+/5d36f01/packages/Tethering/src/com/android/networkstack/tethering/Tethering.java#549
/**
* Wifi tethering type.
* @see [startTethering].
@@ -103,6 +104,22 @@ object TetheringManager {
*/
@RequiresApi(24)
const val TETHERING_BLUETOOTH = 2
/**
* Ncm local tethering type.
*
* Requires NETWORK_SETTINGS permission, which is sadly not obtainable.
* @see [startTethering]
*/
@RequiresApi(30)
const val TETHERING_NCM = 4
/**
* Ethernet tethering type.
*
* Requires MANAGE_USB permission, also.
* @see [startTethering]
*/
@RequiresApi(30)
const val TETHERING_ETHERNET = 5
@get:RequiresApi(30)
private val clazz by lazy { Class.forName("android.net.TetheringManager") }