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]+")) }