Ask for ROOT only when it's needed

This commit is contained in:
Mygod
2015-12-06 20:04:41 +08:00
parent ac98caa4b4
commit 93084f0ed0
13 changed files with 82 additions and 112 deletions
@@ -75,17 +75,11 @@ object Console {
shell.close()
}
def runRootCommand(command: String): String = runRootCommand(Array(command))
def runRootCommand(command: String, context: String): String = runRootCommand(Array(command), context)
def runRootCommand(commands: Array[String]): String = runRootCommand(commands, "u:r:init_shell:s0")
def runRootCommand(commands: Array[String], context: String): String = {
if (!isRoot) {
return null
}
val shell = openRootShell(context)
def runRootCommand(commands: String*): String = runRootCommand(commands.toArray)
def runRootCommand(commands: Array[String]): String = {
val shell = openRootShell("u:r:init_shell:s0")
val sb = new StringBuilder
shell.addCommand(commands, 0, ((commandCode: Int, exitCode: Int, output: util.List[String]) => {
shell.addCommand(commands, 0, ((_, exitCode, output) => {
if (exitCode < 0) {
shell.close()
} else {
@@ -140,12 +140,14 @@ object Utils {
}
// Blocked > 3 seconds
def toggleAirplaneMode(context: Context) {
if (ShadowsocksApplication.isRoot) {
Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0"))
def toggleAirplaneMode(context: Context) = {
if (Console.isRoot) {
Console.runRootCommand("ndc resolver flushdefaultif", "ndc resolver flushif wlan0")
true
} else if (Build.VERSION.SDK_INT < 17) {
toggleBelowApiLevel17(context)
}
true
} else false
}
//noinspection ScalaDeprecation
@@ -305,8 +307,7 @@ object Utils {
var compatible: Boolean = false
var version: Boolean = false
val command = Array(iptables + " --version", iptables + " -L -t nat -n")
val lines = Console.runRootCommand(command)
val lines = Console.runRootCommand(iptables + " --version", iptables + " -L -t nat -n")
if (lines == null) return
if (lines.contains("OUTPUT")) {
@@ -384,9 +385,7 @@ object Utils {
hasRedirectSupport = 1
val command = Utils.getIptables + " -t nat -A OUTPUT -p udp --dport 54 -j REDIRECT --to 8154"
val lines = Console.runRootCommand(command)
Console.runRootCommand(command.replace("-A", "-D"))
val lines = Console.runRootCommand(command, command.replace("-A", "-D"))
if (lines == null) return
if (lines.contains("No chain/target/match")) {
hasRedirectSupport = 0