Suppress SecurityException on Android 8-10

This commit is contained in:
Mygod
2020-07-07 04:20:24 +08:00
parent 293140f64e
commit 82dc01ab37
5 changed files with 14 additions and 7 deletions

View File

@@ -37,7 +37,7 @@ The following features in the app requires it to be installed under `/system/pri
One way to do this is to use [App systemizer for Magisk](https://github.com/Magisk-Modules-Repo/terminal_systemizer).
* (since Android 11, since app v2.9.1) `android.permission.BLUETOOTH_PRIVILEGED`: Use the Bluetooth tethering shortcut switch in app.
* (prior to Android 11, since app v2.4.0) `android.permission.OVERRIDE_WIFI_CONFIG`: Read/write system Wi-Fi hotspot configuration. ([#117](https://github.com/Mygod/VPNHotspot/issues/117))
* (Android 8-10, since app v2.4.0) `android.permission.OVERRIDE_WIFI_CONFIG`: Read/write system Wi-Fi hotspot configuration. ([#117](https://github.com/Mygod/VPNHotspot/issues/117))
Installing as system app also has the side benefit of launching root daemon less frequently due to having privileged permissions listed below.

View File

@@ -23,6 +23,7 @@ import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat
import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.root.WifiApCommands
import be.mygod.vpnhotspot.util.getRootCause
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.Dispatchers
@@ -95,7 +96,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
data.notifyChange()
}
override fun onException(e: Exception) {
if (e !is InvocationTargetException || e.targetException !is SecurityException) Timber.w(e)
if (e.getRootCause() !is SecurityException) Timber.w(e)
GlobalScope.launch(Dispatchers.Main.immediate) {
val context = parent.context ?: app
Toast.makeText(context, e.readableMessage, Toast.LENGTH_LONG).show()

View File

@@ -196,7 +196,9 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
null
} catch (eRoot: Exception) {
eRoot.addSuppressed(e)
Timber.w(eRoot)
if (Build.VERSION.SDK_INT !in 26..29 || eRoot.getRootCause() !is SecurityException) {
Timber.w(eRoot)
}
SmartSnackbar.make(eRoot).show()
null
}

View File

@@ -18,6 +18,7 @@ import be.mygod.vpnhotspot.net.TetheringManager
import be.mygod.vpnhotspot.net.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.getRootCause
import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.util.stopAndUnbind
import kotlinx.coroutines.Dispatchers
@@ -122,7 +123,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
updateTile()
}
override fun onException(e: Exception) {
if (e !is InvocationTargetException || e.targetException !is SecurityException) Timber.w(e)
if (e.getRootCause() !is SecurityException) Timber.w(e)
GlobalScope.launch(Dispatchers.Main.immediate) {
Toast.makeText(this@TetheringTileService, e.readableMessage, Toast.LENGTH_LONG).show()
}

View File

@@ -6,6 +6,7 @@ import android.content.*
import android.net.InetAddresses
import android.os.Build
import android.os.Handler
import android.os.RemoteException
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
@@ -32,9 +33,11 @@ import java.net.NetworkInterface
import java.net.SocketException
import java.util.concurrent.Executor
val Throwable.readableMessage: String get() = if (this is InvocationTargetException) {
targetException.readableMessage
} else localizedMessage ?: javaClass.name
tailrec fun Throwable.getRootCause(): Throwable {
if (this is InvocationTargetException || this is RemoteException) return (cause ?: return this).getRootCause()
return this
}
val Throwable.readableMessage: String get() = getRootCause().run { localizedMessage ?: javaClass.name }
/**
* This is a hack: we wrap longs around in 1 billion and such. Hopefully every language counts in base 10 and this works