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). 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. * (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. 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.SoftApConfigurationCompat
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.root.WifiApCommands import be.mygod.vpnhotspot.root.WifiApCommands
import be.mygod.vpnhotspot.util.getRootCause
import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.widget.SmartSnackbar import be.mygod.vpnhotspot.widget.SmartSnackbar
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -95,7 +96,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
data.notifyChange() data.notifyChange()
} }
override fun onException(e: Exception) { 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) { GlobalScope.launch(Dispatchers.Main.immediate) {
val context = parent.context ?: app val context = parent.context ?: app
Toast.makeText(context, e.readableMessage, Toast.LENGTH_LONG).show() Toast.makeText(context, e.readableMessage, Toast.LENGTH_LONG).show()

View File

@@ -196,7 +196,9 @@ class TetheringFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClick
null null
} catch (eRoot: Exception) { } catch (eRoot: Exception) {
eRoot.addSuppressed(e) eRoot.addSuppressed(e)
if (Build.VERSION.SDK_INT !in 26..29 || eRoot.getRootCause() !is SecurityException) {
Timber.w(eRoot) Timber.w(eRoot)
}
SmartSnackbar.make(eRoot).show() SmartSnackbar.make(eRoot).show()
null 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.TetheringManager.tetheredIfaces
import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.net.wifi.WifiApManager
import be.mygod.vpnhotspot.util.broadcastReceiver import be.mygod.vpnhotspot.util.broadcastReceiver
import be.mygod.vpnhotspot.util.getRootCause
import be.mygod.vpnhotspot.util.readableMessage import be.mygod.vpnhotspot.util.readableMessage
import be.mygod.vpnhotspot.util.stopAndUnbind import be.mygod.vpnhotspot.util.stopAndUnbind
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -122,7 +123,7 @@ sealed class TetheringTileService : IpNeighbourMonitoringTileService(), Tetherin
updateTile() updateTile()
} }
override fun onException(e: Exception) { 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) { GlobalScope.launch(Dispatchers.Main.immediate) {
Toast.makeText(this@TetheringTileService, e.readableMessage, Toast.LENGTH_LONG).show() 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.net.InetAddresses
import android.os.Build import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.RemoteException
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
@@ -32,9 +33,11 @@ import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
import java.util.concurrent.Executor import java.util.concurrent.Executor
val Throwable.readableMessage: String get() = if (this is InvocationTargetException) { tailrec fun Throwable.getRootCause(): Throwable {
targetException.readableMessage if (this is InvocationTargetException || this is RemoteException) return (cause ?: return this).getRootCause()
} else localizedMessage ?: javaClass.name 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 * This is a hack: we wrap longs around in 1 billion and such. Hopefully every language counts in base 10 and this works