From 60ac93e29164d5d4332fd358d1156057ac131827 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 27 May 2020 09:52:51 -0400 Subject: [PATCH] Support tether errors in Android 11 preview --- README.md | 1 + .../mygod/vpnhotspot/manage/TetherManager.kt | 57 +++++++++++-------- .../mygod/vpnhotspot/net/TetheringManager.kt | 13 +++++ 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 905f3fe4..e3d37a95 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ Non-public API list: * (since API 24) [`Landroid/net/ConnectivityManager;->getLastTetherError(Ljava/lang/String;)I,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123309) * (since API 24) [`Landroid/net/ConnectivityManager;->startTethering(IZLandroid/net/ConnectivityManager$OnStartTetheringCallback;Landroid/os/Handler;)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123408) * (since API 24) [`Landroid/net/ConnectivityManager;->stopTethering(I)V,system-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#123410) +* (since API 30) `Landroid/net/TetheringManager;->TETHER_ERROR_*:I,system-api,test-api,whitelist` * (since API 23) [`Landroid/net/wifi/WifiConfiguration;->apBand:I,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#131529) * (since API 23) [`Landroid/net/wifi/WifiConfiguration;->apChannel:I,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#131530) * (since API 26) [`Landroid/net/wifi/WifiManager;->cancelLocalOnlyHotspotRequest()V,greylist`](https://android.googlesource.com/platform/prebuilts/runtime/+/3d07e5c/appcompat/hiddenapi-flags.csv#132250) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt index d069f870..077a7d54 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/TetherManager.kt @@ -7,6 +7,7 @@ import android.view.View import android.widget.Toast import androidx.annotation.RequiresApi import androidx.core.net.toUri +import androidx.core.os.BuildCompat import androidx.core.view.updatePaddingRelative import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner @@ -99,31 +100,39 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(), (viewHolder as ViewHolder).manager = this } - fun updateErrorMessage(errored: List) { - data.text = errored.filter { TetherType.ofInterface(it) == tetherType }.joinToString("\n") { - "$it: " + try { - when (val error = TetheringManager.getLastTetherError(it)) { - TetheringManager.TETHER_ERROR_NO_ERROR -> "TETHER_ERROR_NO_ERROR" - TetheringManager.TETHER_ERROR_UNKNOWN_IFACE -> "TETHER_ERROR_UNKNOWN_IFACE" - TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL -> "TETHER_ERROR_SERVICE_UNAVAIL" - TetheringManager.TETHER_ERROR_UNSUPPORTED -> "TETHER_ERROR_UNSUPPORTED" - TetheringManager.TETHER_ERROR_UNAVAIL_IFACE -> "TETHER_ERROR_UNAVAIL_IFACE" - TetheringManager.TETHER_ERROR_MASTER_ERROR -> "TETHER_ERROR_MASTER_ERROR" - TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR -> "TETHER_ERROR_TETHER_IFACE_ERROR" - TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR -> "TETHER_ERROR_UNTETHER_IFACE_ERROR" - TetheringManager.TETHER_ERROR_ENABLE_NAT_ERROR -> "TETHER_ERROR_ENABLE_NAT_ERROR" - TetheringManager.TETHER_ERROR_DISABLE_NAT_ERROR -> "TETHER_ERROR_DISABLE_NAT_ERROR" - TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR -> "TETHER_ERROR_IFACE_CFG_ERROR" - TetheringManager.TETHER_ERROR_PROVISION_FAILED -> "TETHER_ERROR_PROVISION_FAILED" - TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR -> "TETHER_ERROR_DHCPSERVER_ERROR" - TetheringManager.TETHER_ERROR_ENTITLEMENT_UNKNOWN -> "TETHER_ERROR_ENTITLEMENT_UNKNOWN" - else -> app.getString(R.string.failure_reason_unknown, error) - } - } catch (e: InvocationTargetException) { - if (Build.VERSION.SDK_INT !in 24..25 || e.cause !is SecurityException) Timber.w(e) else Timber.d(e) - e.readableMessage - } + private fun getErrorMessage(iface: String): String { + val error = try { + TetheringManager.getLastTetherError(iface) + } catch (e: InvocationTargetException) { + if (Build.VERSION.SDK_INT !in 24..25 || e.cause !is SecurityException) Timber.w(e) else Timber.d(e) + return e.readableMessage } + if (BuildCompat.isAtLeastR()) try { + TetheringManager.tetherErrors.get(error)?.let { return it } + } catch (e: ReflectiveOperationException) { + Timber.w(e) + } + return when (error) { + TetheringManager.TETHER_ERROR_NO_ERROR -> "TETHER_ERROR_NO_ERROR" + TetheringManager.TETHER_ERROR_UNKNOWN_IFACE -> "TETHER_ERROR_UNKNOWN_IFACE" + TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL -> "TETHER_ERROR_SERVICE_UNAVAIL" + TetheringManager.TETHER_ERROR_UNSUPPORTED -> "TETHER_ERROR_UNSUPPORTED" + TetheringManager.TETHER_ERROR_UNAVAIL_IFACE -> "TETHER_ERROR_UNAVAIL_IFACE" + TetheringManager.TETHER_ERROR_MASTER_ERROR -> "TETHER_ERROR_MASTER_ERROR" + TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR -> "TETHER_ERROR_TETHER_IFACE_ERROR" + TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR -> "TETHER_ERROR_UNTETHER_IFACE_ERROR" + TetheringManager.TETHER_ERROR_ENABLE_NAT_ERROR -> "TETHER_ERROR_ENABLE_NAT_ERROR" + TetheringManager.TETHER_ERROR_DISABLE_NAT_ERROR -> "TETHER_ERROR_DISABLE_NAT_ERROR" + TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR -> "TETHER_ERROR_IFACE_CFG_ERROR" + TetheringManager.TETHER_ERROR_PROVISION_FAILED -> "TETHER_ERROR_PROVISION_FAILED" + TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR -> "TETHER_ERROR_DHCPSERVER_ERROR" + TetheringManager.TETHER_ERROR_ENTITLEMENT_UNKNOWN -> "TETHER_ERROR_ENTITLEMENT_UNKNOWN" + else -> app.getString(R.string.failure_reason_unknown, error) + } + } + fun updateErrorMessage(errored: List) { + data.text = errored.filter { TetherType.ofInterface(it) == tetherType } + .joinToString("\n") { "$it: ${getErrorMessage(it)}" } data.notifyChange() } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index c72711fb..a192cf9d 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -5,6 +5,7 @@ import android.net.ConnectivityManager import android.os.Build import android.os.Handler import androidx.annotation.RequiresApi +import androidx.collection.SparseArrayCompat import androidx.core.os.BuildCompat import be.mygod.vpnhotspot.App.Companion.app import com.android.dx.stock.ProxyBuilder @@ -88,6 +89,18 @@ object TetheringManager { */ const val TETHERING_BLUETOOTH = 2 + @get:RequiresApi(30) + val tetherErrors by lazy { + SparseArrayCompat().apply { + for (field in Class.forName("android.net.TetheringManager").declaredFields) try { + // all TETHER_ERROR_* are system-api since API 30 + if (field.name.startsWith("TETHER_ERROR_")) put(field.get(null) as Int, field.name) + } catch (e: Exception) { + Timber.w(e) + } + } + } + private val classOnStartTetheringCallback by lazy { Class.forName("android.net.ConnectivityManager\$OnStartTetheringCallback") }