optimize root file/folder fetching a bit

This commit is contained in:
tibbi 2018-04-17 14:46:52 +02:00
parent 7e1982b3f1
commit 955f22ebc8

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.filemanager.helpers
import android.app.Activity
import com.simplemobiletools.commons.extensions.areDigitsOnly
import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.filemanager.extensions.config
import com.stericson.RootShell.execution.Command
@ -28,13 +29,13 @@ class RootHelpers(val activity: Activity) {
}
fun getFiles(path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
val files = ArrayList<FileDirItem>()
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
val cmd = "ls $hiddenArgument$path"
getFullLines(path) {
val fullLines = it
val files = ArrayList<FileDirItem>()
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
val cmd = "ls $hiddenArgument$path"
val command = object : Command(0, cmd) {
override fun commandOutput(id: Int, line: String) {
val file = File(path, line)
@ -83,12 +84,8 @@ class RootHelpers(val activity: Activity) {
private fun getChildrenCount(files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
var cmd = ""
files.forEach {
cmd += if (it.isDirectory) {
"ls $hiddenArgument${it.path} |wc -l;"
} else {
"echo 0;"
}
files.filter { it.isDirectory }.forEach {
cmd += "ls $hiddenArgument${it.path} |wc -l;"
}
cmd = cmd.trimEnd(';') + " | cat"
@ -100,13 +97,18 @@ class RootHelpers(val activity: Activity) {
}
override fun commandCompleted(id: Int, exitcode: Int) {
files.forEachIndexed { index, fileDirItem ->
files.filter { it.isDirectory }.forEachIndexed { index, fileDirItem ->
val childrenCount = lines[index]
if (childrenCount.areDigitsOnly()) {
fileDirItem.children = childrenCount.toInt()
}
}
getFileSizes(files, path, callback)
if (activity.config.sorting and SORT_BY_SIZE == 0) {
callback(path, files)
} else {
getFileSizes(files, path, callback)
}
super.commandCompleted(id, exitcode)
}
}
@ -144,6 +146,7 @@ class RootHelpers(val activity: Activity) {
}
}
}
callback(path, files)
super.commandCompleted(id, exitcode)
}