From c8d790b24608ad602326694d9d481cbcd3976b24 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 3 Sep 2017 18:53:11 +0200 Subject: [PATCH] add a simple echo message at checking root access --- .../activities/SettingsActivity.kt | 15 ++++++++-- .../filemanager/helpers/RootHelpers.kt | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SettingsActivity.kt index ca8914ff..05a1fce3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SettingsActivity.kt @@ -8,6 +8,7 @@ import com.simplemobiletools.commons.extensions.updateTextColors import com.simplemobiletools.commons.helpers.SHOW_ALL_TABS import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.extensions.config +import com.simplemobiletools.filemanager.helpers.RootHelpers import kotlinx.android.synthetic.main.activity_settings.* class SettingsActivity : SimpleActivity() { @@ -74,8 +75,18 @@ class SettingsActivity : SimpleActivity() { private fun setupEnableRootAccess() { settings_enable_root_access.isChecked = config.enableRootAccess settings_enable_root_access_holder.setOnClickListener { - settings_enable_root_access.toggle() - config.enableRootAccess = settings_enable_root_access.isChecked + if (!config.enableRootAccess) { + RootHelpers().askRootIFNeeded(this) { + toggleRootAccess(it) + } + } else { + toggleRootAccess(false) + } } } + + private fun toggleRootAccess(enable: Boolean) { + settings_enable_root_access.isChecked = enable + config.enableRootAccess = enable + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt index 82172ebe..cdcb0a62 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt @@ -1,10 +1,38 @@ package com.simplemobiletools.filemanager.helpers +import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.models.FileDirItem +import com.simplemobiletools.filemanager.activities.SimpleActivity import com.stericson.RootShell.execution.Command import com.stericson.RootTools.RootTools class RootHelpers { + fun askRootIFNeeded(activity: SimpleActivity, callback: (success: Boolean) -> Unit) { + val SIMPLE_MOBILE_TOOLS = "simple mobile tools" + val command = object : Command(0, "echo $SIMPLE_MOBILE_TOOLS") { + override fun commandOutput(id: Int, line: String) { + if (line == SIMPLE_MOBILE_TOOLS) + callback(true) + super.commandOutput(id, line) + } + + override fun commandTerminated(id: Int, reason: String?) { + super.commandTerminated(id, reason) + } + + override fun commandCompleted(id: Int, exitcode: Int) { + super.commandCompleted(id, exitcode) + } + } + + try { + RootTools.getShell(true).add(command) + } catch (exception: Exception) { + activity.showErrorToast(exception) + callback(false) + } + } + fun getFiles(path: String, callback: (fileDirItems: ArrayList) -> Unit) { val command = object : Command(0, "ls -la $path | awk '{print \$1,\$NF}'") { override fun commandOutput(id: Int, line: String) {