Catch IOExceptions when doing SU

This commit is contained in:
Mygod
2018-01-13 19:05:59 +08:00
parent f6a5ed9d64
commit d278c5daab
3 changed files with 29 additions and 11 deletions

View File

@@ -194,7 +194,7 @@ class RepeaterService : Service(), WifiP2pManager.ChannelListener, VpnListener.C
handler.removeCallbacks(onVpnUnavailable)
when (status) {
Status.STARTING -> {
val matcher = patternNetworkInfo.matcher(loggerSu("dumpsys ${Context.WIFI_P2P_SERVICE}"))
val matcher = patternNetworkInfo.matcher(loggerSu("dumpsys ${Context.WIFI_P2P_SERVICE}") ?: "")
when {
!matcher.find() -> startFailure("Root unavailable")
matcher.group(2) == "true" -> {

View File

@@ -5,7 +5,9 @@ import android.net.Uri
import android.os.Bundle
import android.support.customtabs.CustomTabsIntent
import android.support.v4.content.ContextCompat
import android.widget.Toast
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers
import java.io.IOException
class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
private val customTabsIntent by lazy {
@@ -19,11 +21,16 @@ class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
true
}
findPreference("misc.logcat").setOnPreferenceClickListener {
val intent = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
.inputStream.bufferedReader().use { it.readText() })
startActivity(Intent.createChooser(intent, getString(R.string.abc_shareactionprovider_share_with)))
try {
val intent = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
.inputStream.bufferedReader().use { it.readText() })
startActivity(Intent.createChooser(intent, getString(R.string.abc_shareactionprovider_share_with)))
} catch (e: IOException) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
e.printStackTrace()
}
true
}
findPreference("misc.source").setOnPreferenceClickListener {

View File

@@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
import java.io.IOException
import java.io.InputStream
fun debugLog(tag: String?, message: String?) {
@@ -28,18 +29,28 @@ fun loggerSuStream(command: String): InputStream {
.redirectErrorStream(true)
.start()
process.waitFor()
val err = process.errorStream.bufferedReader().use { it.readText() }
if (!err.isBlank()) Log.e(NOISYSU_TAG, err)
val err = try {
process.errorStream.bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
null
}
if (!err.isNullOrBlank()) Log.e(NOISYSU_TAG, err)
return process.inputStream
}
fun loggerSu(command: String): String = loggerSuStream(command).bufferedReader().use { it.readText() }
fun loggerSu(command: String): String? = try {
loggerSuStream(command).bufferedReader().use { it.readText() }
} catch (e: IOException) {
e.printStackTrace()
null
}
fun noisySu(commands: Iterable<String>): Boolean {
var out = loggerSu("""function noisy() { "$@" || echo "$@" exited with $?; }
${commands.joinToString("\n") { if (it.startsWith("while ")) it else "noisy $it" }}
echo $NOISYSU_SUFFIX""")
val result = out == NOISYSU_SUFFIX
out = out.removeSuffix(NOISYSU_SUFFIX)
if (!out.isBlank()) Log.i(NOISYSU_TAG, out)
out = out?.removeSuffix(NOISYSU_SUFFIX)
if (!out.isNullOrBlank()) Log.i(NOISYSU_TAG, out)
return result
}
fun noisySu(vararg commands: String) = noisySu(commands.asIterable())