merge everything we need about root items into 1 command
This commit is contained in:
parent
5f60827763
commit
fa35107abe
|
@ -1,6 +1,7 @@
|
||||||
package com.simplemobiletools.filemanager.helpers
|
package com.simplemobiletools.filemanager.helpers
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.text.TextUtils
|
||||||
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.activities.SimpleActivity
|
||||||
|
@ -39,49 +40,30 @@ class RootHelpers {
|
||||||
fun getFiles(context: Context, path: String, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
|
fun getFiles(context: Context, path: String, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
|
||||||
val files = ArrayList<FileDirItem>()
|
val files = ArrayList<FileDirItem>()
|
||||||
val showHidden = context.config.shouldShowHidden
|
val showHidden = context.config.shouldShowHidden
|
||||||
val SEPARATOR = "|||"
|
|
||||||
|
|
||||||
val command = object : Command(0, "ls -la $path | awk '{print \$1,\"$SEPARATOR\",$4,\"$SEPARATOR\",\$NF}'") {
|
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) {
|
||||||
override fun commandOutput(id: Int, line: String) {
|
override fun commandOutput(id: Int, line: String) {
|
||||||
val parts = line.split(SEPARATOR)
|
val parts = line.split(" ")
|
||||||
|
if (parts.size >= 4) {
|
||||||
val filename = parts[2].trim().trimStart('/')
|
|
||||||
if (showHidden || !filename.startsWith(".")) {
|
|
||||||
val filePath = "${path.trimEnd('/')}/$filename"
|
|
||||||
val permissions = parts[0].trim()
|
val permissions = parts[0].trim()
|
||||||
val isDirectory = permissions.startsWith("d")
|
val isDirectory = permissions.startsWith("d")
|
||||||
val fileSize = if (permissions.startsWith("-")) parts[1].trim().toLong() else 0L
|
val isFile = permissions.startsWith("-")
|
||||||
val fileDirItem = FileDirItem(filePath, filename, isDirectory, 0, fileSize)
|
val size = if (isFile) parts[1].trim() else "0"
|
||||||
files.add(fileDirItem)
|
val childrenCnt = if (isFile) "0" else parts[2].trim()
|
||||||
}
|
val filename = TextUtils.join(" ", parts.subList(3, parts.size)).trimStart('/')
|
||||||
|
|
||||||
|
if ((!showHidden && filename.startsWith(".")) || (!isDirectory && !isFile) || !areDigitsOnly(size) || !areDigitsOnly(childrenCnt)) {
|
||||||
super.commandOutput(id, line)
|
super.commandOutput(id, line)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun commandTerminated(id: Int, reason: String?) {
|
val fileSize = size.toLong()
|
||||||
super.commandTerminated(id, reason)
|
val filePath = "${path.trimEnd('/')}/$filename"
|
||||||
}
|
val fileDirItem = FileDirItem(filePath, filename, isDirectory, childrenCnt.toInt(), fileSize)
|
||||||
|
|
||||||
override fun commandCompleted(id: Int, exitcode: Int) {
|
|
||||||
super.commandCompleted(id, exitcode)
|
|
||||||
getFileDirParameters(files, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RootTools.getShell(true).add(command)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getFileDirParameters(oldItems: ArrayList<FileDirItem>, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
|
|
||||||
val files = ArrayList<FileDirItem>()
|
|
||||||
val shell = RootTools.getShell(true)
|
|
||||||
oldItems.forEach {
|
|
||||||
val command = object : Command(0, "find ${it.path} -mindepth 1 -maxdepth 1 | wc -l") {
|
|
||||||
override fun commandOutput(id: Int, line: String) {
|
|
||||||
val areDigitsOnly = line.matches(Regex("[0-9 ]+"))
|
|
||||||
if (areDigitsOnly) {
|
|
||||||
val children = line.trim().toInt()
|
|
||||||
val fileDirItem = FileDirItem(it.path, it.name, it.isDirectory, children, it.size)
|
|
||||||
files.add(fileDirItem)
|
files.add(fileDirItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
super.commandOutput(id, line)
|
super.commandOutput(id, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +76,8 @@ class RootHelpers {
|
||||||
super.commandCompleted(id, exitcode)
|
super.commandCompleted(id, exitcode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shell.add(command)
|
RootTools.getShell(true).add(command)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun areDigitsOnly(value: String) = value.matches(Regex("[0-9 ]+"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue