From 5cb9b6cc4f3c863e8b872c415b92adaef816fd8c Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 4 Sep 2017 18:58:15 +0200 Subject: [PATCH] recheck at app launch if root permission hasnt been revoked --- .../filemanager/activities/MainActivity.kt | 6 ++++++ .../filemanager/fragments/ItemsFragment.kt | 2 +- .../filemanager/helpers/RootHelpers.kt | 13 ++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index c21ee979..b91d4c8e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -25,6 +25,7 @@ import com.simplemobiletools.filemanager.SCROLL_STATE import com.simplemobiletools.filemanager.dialogs.ChangeSortingDialog import com.simplemobiletools.filemanager.extensions.config import com.simplemobiletools.filemanager.fragments.ItemsFragment +import com.simplemobiletools.filemanager.helpers.RootHelpers import com.stericson.RootTools.RootTools import kotlinx.android.synthetic.main.activity_main.* import java.util.* @@ -256,6 +257,11 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br private fun checkIfRootAvailable() { Thread({ config.isRootAvailable = RootTools.isRootAvailable() + if (config.isRootAvailable && config.enableRootAccess) { + RootHelpers().askRootIFNeeded(this) { + config.enableRootAccess = it + } + } }).start() } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt index 92f9d63d..30190825 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt @@ -189,7 +189,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener { } private fun getRootItemsOf(path: String, callback: (items: ArrayList) -> Unit) { - RootHelpers().getFiles(context, path.trimEnd('/'), callback) + RootHelpers().getFiles(activity as SimpleActivity, path.trimEnd('/'), callback) } private fun getChildren(file: File): Int { 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 2ec907ee..c16c7e05 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt @@ -1,6 +1,5 @@ package com.simplemobiletools.filemanager.helpers -import android.content.Context import android.text.TextUtils import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.models.FileDirItem @@ -37,9 +36,9 @@ class RootHelpers { } } - fun getFiles(context: Context, path: String, callback: (fileDirItems: ArrayList) -> Unit) { + fun getFiles(activity: SimpleActivity, path: String, callback: (fileDirItems: ArrayList) -> Unit) { val files = ArrayList() - val showHidden = context.config.shouldShowHidden + val showHidden = activity.config.shouldShowHidden val cmd = "ls -la $path | awk '{ system(\"echo \"\$1\" \"\$4\" `find $path/\"\$NF\" -mindepth 1 -maxdepth 1 | wc -l` \"\$NF\" \")}'" val command = object : Command(0, cmd) { @@ -76,8 +75,12 @@ class RootHelpers { super.commandCompleted(id, exitcode) } } - RootTools.getShell(true).add(command) + try { + RootTools.getShell(true).add(command) + } catch (e: Exception) { + activity.showErrorToast(e) + } } - private fun areDigitsOnly(value: String) = value.matches(Regex("[0-9 ]+")) + private fun areDigitsOnly(value: String) = value.matches(Regex("[0-9]+")) }