add an activity in RootHelpers constructor to avoid passing it around

This commit is contained in:
tibbi
2018-04-16 18:46:11 +02:00
parent dd8d54bd4f
commit 638d3dc070
5 changed files with 41 additions and 35 deletions

View File

@ -277,7 +277,7 @@ class MainActivity : SimpleActivity() {
Thread { Thread {
config.isRootAvailable = RootTools.isRootAvailable() config.isRootAvailable = RootTools.isRootAvailable()
if (config.isRootAvailable && config.enableRootAccess) { if (config.isRootAvailable && config.enableRootAccess) {
RootHelpers().askRootIfNeeded(this) { RootHelpers(this).askRootIfNeeded {
config.enableRootAccess = it config.enableRootAccess = it
} }
} }

View File

@ -136,7 +136,7 @@ class SettingsActivity : SimpleActivity() {
settings_enable_root_access.isChecked = config.enableRootAccess settings_enable_root_access.isChecked = config.enableRootAccess
settings_enable_root_access_holder.setOnClickListener { settings_enable_root_access_holder.setOnClickListener {
if (!config.enableRootAccess) { if (!config.enableRootAccess) {
RootHelpers().askRootIfNeeded(this) { RootHelpers(this).askRootIfNeeded {
toggleRootAccess(it) toggleRootAccess(it)
} }
} else { } else {

View File

@ -67,7 +67,7 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
} }
} }
else -> { else -> {
RootHelpers().createFileFolder(activity, path, false) { RootHelpers(activity).createFileFolder(path, false) {
if (it) { if (it) {
success(alertDialog) success(alertDialog)
} else { } else {
@ -100,7 +100,7 @@ class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val ca
} }
} }
else -> { else -> {
RootHelpers().createFileFolder(activity, path, true) { RootHelpers(activity).createFileFolder(path, true) {
if (it) { if (it) {
success(alertDialog) success(alertDialog)
} else { } else {

View File

@ -25,7 +25,6 @@ import com.simplemobiletools.filemanager.extensions.tryOpenPathIntent
import com.simplemobiletools.filemanager.helpers.PATH import com.simplemobiletools.filemanager.helpers.PATH
import com.simplemobiletools.filemanager.helpers.RootHelpers import com.simplemobiletools.filemanager.helpers.RootHelpers
import com.simplemobiletools.filemanager.interfaces.ItemOperationsListener import com.simplemobiletools.filemanager.interfaces.ItemOperationsListener
import com.stericson.RootTools.RootTools
import kotlinx.android.synthetic.main.items_fragment.* import kotlinx.android.synthetic.main.items_fragment.*
import kotlinx.android.synthetic.main.items_fragment.view.* import kotlinx.android.synthetic.main.items_fragment.view.*
import java.io.File import java.io.File
@ -183,7 +182,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
} else if (!context!!.config.enableRootAccess || !context!!.isPathOnRoot(path)) { } else if (!context!!.config.enableRootAccess || !context!!.isPathOnRoot(path)) {
getRegularItemsOf(path, callback) getRegularItemsOf(path, callback)
} else { } else {
RootHelpers().getFiles(activity as SimpleActivity, path, callback) RootHelpers(activity!!).getFiles(path, callback)
} }
} }
}.start() }.start()
@ -281,10 +280,13 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
override fun deleteFiles(files: ArrayList<FileDirItem>) { override fun deleteFiles(files: ArrayList<FileDirItem>) {
val hasFolder = files.any { it.isDirectory } val hasFolder = files.any { it.isDirectory }
if (context!!.isPathOnRoot(files.firstOrNull()?.path ?: context!!.config.internalStoragePath)) { val firstPath = files.firstOrNull()?.path
files.forEach { if (firstPath == null || firstPath.isEmpty()) {
RootTools.deleteFileOrDirectory(it.path, false) return
} }
if (context!!.isPathOnRoot(firstPath)) {
RootHelpers(activity!!).deleteFiles(files)
} else { } else {
(activity as SimpleActivity).deleteFiles(files, hasFolder) { (activity as SimpleActivity).deleteFiles(files, hasFolder) {
if (!it) { if (!it) {

View File

@ -1,18 +1,18 @@
package com.simplemobiletools.filemanager.helpers package com.simplemobiletools.filemanager.helpers
import android.app.Activity
import com.simplemobiletools.commons.extensions.areDigitsOnly import com.simplemobiletools.commons.extensions.areDigitsOnly
import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.filemanager.activities.SimpleActivity
import com.simplemobiletools.filemanager.extensions.config import com.simplemobiletools.filemanager.extensions.config
import com.stericson.RootShell.execution.Command import com.stericson.RootShell.execution.Command
import com.stericson.RootTools.RootTools import com.stericson.RootTools.RootTools
import java.io.File import java.io.File
import java.util.*
class RootHelpers { class RootHelpers(val activity: Activity) {
fun askRootIfNeeded(activity: SimpleActivity, callback: (success: Boolean) -> Unit) { fun askRootIfNeeded(callback: (success: Boolean) -> Unit) {
val command = object : Command(0, "ls -lA") { val cmd = "ls -lA"
val command = object : Command(0, cmd) {
override fun commandOutput(id: Int, line: String) { override fun commandOutput(id: Int, line: String) {
callback(true) callback(true)
super.commandOutput(id, line) super.commandOutput(id, line)
@ -27,7 +27,7 @@ class RootHelpers {
} }
} }
fun getFiles(activity: SimpleActivity, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) { fun getFiles(path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
val files = ArrayList<FileDirItem>() val files = ArrayList<FileDirItem>()
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else "" val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
val cmd = "ls $hiddenArgument$path" val cmd = "ls $hiddenArgument$path"
@ -45,17 +45,17 @@ class RootHelpers {
if (files.isEmpty()) { if (files.isEmpty()) {
callback(path, files) callback(path, files)
} else { } else {
getChildrenCount(activity, files, path, callback) getChildrenCount(files, path, callback)
} }
super.commandCompleted(id, exitcode) super.commandCompleted(id, exitcode)
} }
} }
runCommand(activity, command) runCommand(command)
} }
private fun getChildrenCount(activity: SimpleActivity, files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) { private fun getChildrenCount(files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else "" val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
var cmd = "" var cmd = ""
files.forEach { files.forEach {
@ -81,15 +81,15 @@ class RootHelpers {
fileDirItem.children = childrenCount.toInt() fileDirItem.children = childrenCount.toInt()
} }
} }
getFileSizes(activity, files, path, callback) getFileSizes(files, path, callback)
super.commandCompleted(id, exitcode) super.commandCompleted(id, exitcode)
} }
} }
runCommand(activity, command) runCommand(command)
} }
private fun getFileSizes(activity: SimpleActivity, files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) { private fun getFileSizes(files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
var cmd = "" var cmd = ""
files.forEach { files.forEach {
cmd += if (it.isDirectory) { cmd += if (it.isDirectory) {
@ -124,10 +124,10 @@ class RootHelpers {
} }
} }
runCommand(activity, command) runCommand(command)
} }
private fun runCommand(activity: SimpleActivity, command: Command) { private fun runCommand(command: Command) {
try { try {
RootTools.getShell(true).add(command) RootTools.getShell(true).add(command)
} catch (e: Exception) { } catch (e: Exception) {
@ -135,8 +135,8 @@ class RootHelpers {
} }
} }
fun createFileFolder(activity: SimpleActivity, path: String, isFile: Boolean, callback: (success: Boolean) -> Unit) { fun createFileFolder(path: String, isFile: Boolean, callback: (success: Boolean) -> Unit) {
tryMountAsRW(activity, path) { tryMountAsRW(path) {
val mountPoint = it val mountPoint = it
val targetPath = path.trim('/') val targetPath = path.trim('/')
val mainCommand = if (isFile) "touch" else "mkdir" val mainCommand = if (isFile) "touch" else "mkdir"
@ -144,25 +144,25 @@ class RootHelpers {
val command = object : Command(0, cmd) { val command = object : Command(0, cmd) {
override fun commandCompleted(id: Int, exitcode: Int) { override fun commandCompleted(id: Int, exitcode: Int) {
callback(exitcode == 0) callback(exitcode == 0)
mountAsRO(activity, mountPoint) mountAsRO(mountPoint)
super.commandCompleted(id, exitcode) super.commandCompleted(id, exitcode)
} }
} }
runCommand(activity, command) runCommand(command)
} }
} }
private fun mountAsRO(activity: SimpleActivity, mountPoint: String?) { private fun mountAsRO(mountPoint: String?) {
if (mountPoint != null) { if (mountPoint != null) {
val cmd = "umount -r \"$mountPoint\"" val cmd = "umount -r \"$mountPoint\""
val command = object : Command(0, cmd) {} val command = object : Command(0, cmd) {}
runCommand(activity, command) runCommand(command)
} }
} }
// inspired by Amaze File Manager // inspired by Amaze File Manager
private fun tryMountAsRW(activity: SimpleActivity, path: String, callback: (mountPoint: String?) -> Unit) { private fun tryMountAsRW(path: String, callback: (mountPoint: String?) -> Unit) {
val mountPoints = ArrayList<String>() val mountPoints = ArrayList<String>()
val command = object : Command(0, "mount") { val command = object : Command(0, "mount") {
@ -190,7 +190,7 @@ class RootHelpers {
callback(null) callback(null)
} else if (types.contains("ro")) { } else if (types.contains("ro")) {
val mountCommand = "mount -o rw,remount $mountPoint" val mountCommand = "mount -o rw,remount $mountPoint"
mountAsRW(activity, mountCommand) { mountAsRW(mountCommand) {
callback(it) callback(it)
} }
} }
@ -200,10 +200,10 @@ class RootHelpers {
} }
} }
runCommand(activity, command) runCommand(command)
} }
private fun mountAsRW(activity: SimpleActivity, commandString: String, callback: (mountPoint: String) -> Unit) { private fun mountAsRW(commandString: String, callback: (mountPoint: String) -> Unit) {
val command = object : Command(0, commandString) { val command = object : Command(0, commandString) {
override fun commandOutput(id: Int, line: String) { override fun commandOutput(id: Int, line: String) {
callback(line) callback(line)
@ -211,6 +211,10 @@ class RootHelpers {
} }
} }
runCommand(activity, command) runCommand(command)
}
fun deleteFiles(fileDirItems: ArrayList<FileDirItem>) {
} }
} }