From d278c5daabdc223b61db994e006c58ff2b35cf1a Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 13 Jan 2018 19:05:59 +0800 Subject: [PATCH] Catch IOExceptions when doing SU --- .../be/mygod/vpnhotspot/RepeaterService.kt | 2 +- .../vpnhotspot/SettingsPreferenceFragment.kt | 17 ++++++++++----- .../main/java/be/mygod/vpnhotspot/Utils.kt | 21 ++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt index 8321ca07..14a0753b 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/RepeaterService.kt @@ -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" -> { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt index eabccaa2..69408602 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/SettingsPreferenceFragment.kt @@ -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 { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/Utils.kt b/mobile/src/main/java/be/mygod/vpnhotspot/Utils.kt index da7e8018..9bb975d1 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/Utils.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/Utils.kt @@ -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): 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())