Handle TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION correctly

This commit is contained in:
Mygod
2020-07-09 02:31:51 +08:00
parent df4acae7d0
commit e27d4c4068
5 changed files with 44 additions and 26 deletions

View File

@@ -212,6 +212,7 @@ Hidden whitelisted APIs: (same catch as above, however, things in this list are
* (since API 24) [`Landroid/net/TetheringManager;->TETHERING_USB:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148944) * (since API 24) [`Landroid/net/TetheringManager;->TETHERING_USB:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148944)
* (since API 24) [`Landroid/net/TetheringManager;->TETHERING_WIFI:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148945) * (since API 24) [`Landroid/net/TetheringManager;->TETHERING_WIFI:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148945)
* [`Landroid/net/TetheringManager;->TETHER_ERROR_*:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148947) * [`Landroid/net/TetheringManager;->TETHER_ERROR_*:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148947)
* (since API 30) [`Landroid/net/TetheringManager;->TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148954)
* (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_FAILED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148964) * (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_FAILED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148964)
* (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_STARTED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148965) * (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_STARTED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148965)
* (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_STOPPED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148966) * (since API 30) [`Landroid/net/TetheringManager;->TETHER_HARDWARE_OFFLOAD_STOPPED:I,system-api,test-api,whitelist`](https://android.googlesource.com/platform/prebuilts/runtime/+/4601d91/appcompat/hiddenapi-flags.csv#148966)

View File

@@ -92,7 +92,13 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
override fun onTetheringStarted() = data.notifyChange() override fun onTetheringStarted() = data.notifyChange()
override fun onTetheringFailed(error: Int?) { override fun onTetheringFailed(error: Int?) {
Timber.d("onTetheringFailed: $error") Timber.d("onTetheringFailed: $error")
error?.let { SmartSnackbar.make("$tetherType: ${TetheringManager.tetherErrorLookup(it)}").show() } if (Build.VERSION.SDK_INT < 30 || error != TetheringManager.TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION) {
error?.let { SmartSnackbar.make("$tetherType: ${TetheringManager.tetherErrorLookup(it)}").show() }
} else GlobalScope.launch(Dispatchers.Main.immediate) {
val context = parent.context ?: app
Toast.makeText(context, R.string.permission_missing, Toast.LENGTH_LONG).show()
ManageBar.start(context)
}
data.notifyChange() data.notifyChange()
} }
override fun onException(e: Exception) { override fun onException(e: Exception) {

View File

@@ -297,31 +297,38 @@ object TetheringManager {
fun startTethering(type: Int, showProvisioningUi: Boolean, callback: StartTetheringCallback, fun startTethering(type: Int, showProvisioningUi: Boolean, callback: StartTetheringCallback,
handler: Handler? = null, cacheDir: File = app.deviceStorage.codeCacheDir) { handler: Handler? = null, cacheDir: File = app.deviceStorage.codeCacheDir) {
if (Build.VERSION.SDK_INT >= 30) try { if (Build.VERSION.SDK_INT >= 30) try {
val proxy = proxy(callback)
val executor = handler.makeExecutor() val executor = handler.makeExecutor()
try { startTethering(type, true, showProvisioningUi,
startTethering(type, true, showProvisioningUi, executor, proxy) executor, proxy(object : StartTetheringCallback {
} catch (e1: InvocationTargetException) { override fun onTetheringStarted() = callback.onTetheringStarted()
if (e1.targetException is SecurityException) GlobalScope.launch(Dispatchers.Unconfined) { override fun onTetheringFailed(error: Int?) {
val result = try { if (error != TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION) callback.onTetheringFailed(error)
RootManager.use { it.execute(StartTethering(type, showProvisioningUi)) } else GlobalScope.launch(Dispatchers.Unconfined) {
} catch (e2: Exception) { val result = try {
e2.addSuppressed(e1) RootManager.use { it.execute(StartTethering(type, showProvisioningUi)) }
try { } catch (eRoot: Exception) {
// last resort: start tethering without trying to bypass entitlement check try { // last resort: start tethering without trying to bypass entitlement check
startTethering(type, false, showProvisioningUi, executor, proxy) startTethering(type, false, showProvisioningUi, executor, proxy(callback))
Timber.w(e2) Timber.w(eRoot)
} catch (e3: Exception) { } catch (e: Exception) {
e3.addSuppressed(e2) e.addSuppressed(eRoot)
Timber.w(e3) callback.onException(e)
callback.onException(e3) }
return@launch
}
when {
result == null -> callback.onTetheringStarted()
result.value == TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION -> try {
startTethering(type, false, showProvisioningUi, executor, proxy(callback))
} catch (e: Exception) {
callback.onException(e)
}
else -> callback.onTetheringFailed(result.value)
} }
return@launch
} }
if (result == null) callback.onTetheringStarted() }
else callback.onTetheringFailed(result.value) override fun onException(e: Exception) = callback.onException(e)
} else callback.onException(e1) }))
}
} catch (e: Exception) { } catch (e: Exception) {
callback.onException(e) callback.onException(e)
} else @Suppress("DEPRECATION") try { } else @Suppress("DEPRECATION") try {
@@ -607,6 +614,8 @@ object TetheringManager {
"TETHER_ERROR_TETHER_IFACE_ERROR", "TETHER_ERROR_UNTETHER_IFACE_ERROR", "TETHER_ERROR_ENABLE_NAT_ERROR", "TETHER_ERROR_TETHER_IFACE_ERROR", "TETHER_ERROR_UNTETHER_IFACE_ERROR", "TETHER_ERROR_ENABLE_NAT_ERROR",
"TETHER_ERROR_DISABLE_NAT_ERROR", "TETHER_ERROR_IFACE_CFG_ERROR", "TETHER_ERROR_PROVISION_FAILED", "TETHER_ERROR_DISABLE_NAT_ERROR", "TETHER_ERROR_IFACE_CFG_ERROR", "TETHER_ERROR_PROVISION_FAILED",
"TETHER_ERROR_DHCPSERVER_ERROR", "TETHER_ERROR_ENTITLEMENT_UNKNOWN") { clazz } "TETHER_ERROR_DHCPSERVER_ERROR", "TETHER_ERROR_ENTITLEMENT_UNKNOWN") { clazz }
@RequiresApi(30)
const val TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION = 14
val Intent.tetheredIfaces get() = getStringArrayListExtra( val Intent.tetheredIfaces get() = getStringArrayListExtra(
if (Build.VERSION.SDK_INT >= 26) EXTRA_ACTIVE_TETHER else EXTRA_ACTIVE_TETHER_LEGACY) if (Build.VERSION.SDK_INT >= 26) EXTRA_ACTIVE_TETHER else EXTRA_ACTIVE_TETHER_LEGACY)

View File

@@ -151,6 +151,7 @@
<string name="failure_reason_unknown">未知 #%d</string> <string name="failure_reason_unknown">未知 #%d</string>
<string name="exception_interface_not_found">错误:未找到下游接口</string> <string name="exception_interface_not_found">错误:未找到下游接口</string>
<string name="noisy_su_failure">发生异常,详情请查看调试信息。</string> <string name="noisy_su_failure">发生异常,详情请查看调试信息。</string>
<string name="permission_missing">权限不足</string>
<string name="configuration_view">设置 WLAN</string> <string name="configuration_view">设置 WLAN</string>
<string name="configuration_share">使用 QR 码分享</string> <string name="configuration_share">使用 QR 码分享</string>

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Values copied from: Values copied from:
* https://android.googlesource.com/platform/packages/apps/Settings/+/7686ef8/res/xml/tether_prefs.xml https://android.googlesource.com/platform/packages/apps/Settings/+/7686ef8/res/xml/tether_prefs.xml
* https://android.googlesource.com/platform/packages/apps/Settings/+/37c9c5b/res/values/strings.xml https://android.googlesource.com/platform/packages/apps/Settings/+/37c9c5b/res/values/strings.xml
* @string/usb_tethering_button_text * @string/usb_tethering_button_text
* @string/wifi_hotspot_checkbox_text * @string/wifi_hotspot_checkbox_text
* @string/bluetooth_tether_checkbox_text * @string/bluetooth_tether_checkbox_text
@@ -12,7 +12,7 @@
* @string/wifi_ap_choose_5G * @string/wifi_ap_choose_5G
* @string/wifi_advanced_mac_address_title * @string/wifi_advanced_mac_address_title
* @string/wifi_hidden_network * @string/wifi_hidden_network
* https://android.googlesource.com/platform/frameworks/base/+/f41fdc9/packages/SystemUI/res/values/strings.xml#874 https://android.googlesource.com/platform/frameworks/base/+/f41fdc9/packages/SystemUI/res/values/strings.xml#874
* @plurals/quick_settings_hotspot_secondary_label_num_devices * @plurals/quick_settings_hotspot_secondary_label_num_devices
--> -->
<resources> <resources>
@@ -171,6 +171,7 @@
<string name="failure_reason_unknown">unknown #%d</string> <string name="failure_reason_unknown">unknown #%d</string>
<string name="exception_interface_not_found">Fatal: Downstream interface not found</string> <string name="exception_interface_not_found">Fatal: Downstream interface not found</string>
<string name="noisy_su_failure">Something went wrong, please check the debug information.</string> <string name="noisy_su_failure">Something went wrong, please check the debug information.</string>
<string name="permission_missing">Permission missing</string>
<string name="configuration_view">Wi\u2011Fi configuration</string> <string name="configuration_view">Wi\u2011Fi configuration</string>
<string name="configuration_share">Share via QR code</string> <string name="configuration_share">Share via QR code</string>