Skip to content

Commit fa35107

Browse files
committed
merge everything we need about root items into 1 command
1 parent 5f60827 commit fa35107

File tree

1 file changed

+20
-37
lines changed
  • app/src/main/kotlin/com/simplemobiletools/filemanager/helpers

1 file changed

+20
-37
lines changed
Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.simplemobiletools.filemanager.helpers
22

33
import android.content.Context
4+
import android.text.TextUtils
45
import com.simplemobiletools.commons.extensions.showErrorToast
56
import com.simplemobiletools.commons.models.FileDirItem
67
import com.simplemobiletools.filemanager.activities.SimpleActivity
@@ -39,19 +40,27 @@ class RootHelpers {
3940
fun getFiles(context: Context, path: String, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
4041
val files = ArrayList<FileDirItem>()
4142
val showHidden = context.config.shouldShowHidden
42-
val SEPARATOR = "|||"
4343

44-
val command = object : Command(0, "ls -la $path | awk '{print \$1,\"$SEPARATOR\",$4,\"$SEPARATOR\",\$NF}'") {
44+
val cmd = "ls -la $path | awk '{ system(\"echo \"\$1\" \"\$4\" `find $path/\"\$NF\" -mindepth 1 -maxdepth 1 | wc -l` \"\$NF\" \")}'"
45+
val command = object : Command(0, cmd) {
4546
override fun commandOutput(id: Int, line: String) {
46-
val parts = line.split(SEPARATOR)
47-
48-
val filename = parts[2].trim().trimStart('/')
49-
if (showHidden || !filename.startsWith(".")) {
50-
val filePath = "${path.trimEnd('/')}/$filename"
47+
val parts = line.split(" ")
48+
if (parts.size >= 4) {
5149
val permissions = parts[0].trim()
5250
val isDirectory = permissions.startsWith("d")
53-
val fileSize = if (permissions.startsWith("-")) parts[1].trim().toLong() else 0L
54-
val fileDirItem = FileDirItem(filePath, filename, isDirectory, 0, fileSize)
51+
val isFile = permissions.startsWith("-")
52+
val size = if (isFile) parts[1].trim() else "0"
53+
val childrenCnt = if (isFile) "0" else parts[2].trim()
54+
val filename = TextUtils.join(" ", parts.subList(3, parts.size)).trimStart('/')
55+
56+
if ((!showHidden && filename.startsWith(".")) || (!isDirectory && !isFile) || !areDigitsOnly(size) || !areDigitsOnly(childrenCnt)) {
57+
super.commandOutput(id, line)
58+
return
59+
}
60+
61+
val fileSize = size.toLong()
62+
val filePath = "${path.trimEnd('/')}/$filename"
63+
val fileDirItem = FileDirItem(filePath, filename, isDirectory, childrenCnt.toInt(), fileSize)
5564
files.add(fileDirItem)
5665
}
5766

@@ -63,38 +72,12 @@ class RootHelpers {
6372
}
6473

6574
override fun commandCompleted(id: Int, exitcode: Int) {
75+
callback(files)
6676
super.commandCompleted(id, exitcode)
67-
getFileDirParameters(files, callback)
6877
}
6978
}
7079
RootTools.getShell(true).add(command)
7180
}
7281

73-
fun getFileDirParameters(oldItems: ArrayList<FileDirItem>, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
74-
val files = ArrayList<FileDirItem>()
75-
val shell = RootTools.getShell(true)
76-
oldItems.forEach {
77-
val command = object : Command(0, "find ${it.path} -mindepth 1 -maxdepth 1 | wc -l") {
78-
override fun commandOutput(id: Int, line: String) {
79-
val areDigitsOnly = line.matches(Regex("[0-9 ]+"))
80-
if (areDigitsOnly) {
81-
val children = line.trim().toInt()
82-
val fileDirItem = FileDirItem(it.path, it.name, it.isDirectory, children, it.size)
83-
files.add(fileDirItem)
84-
}
85-
super.commandOutput(id, line)
86-
}
87-
88-
override fun commandTerminated(id: Int, reason: String?) {
89-
super.commandTerminated(id, reason)
90-
}
91-
92-
override fun commandCompleted(id: Int, exitcode: Int) {
93-
callback(files)
94-
super.commandCompleted(id, exitcode)
95-
}
96-
}
97-
shell.add(command)
98-
}
99-
}
82+
private fun areDigitsOnly(value: String) = value.matches(Regex("[0-9 ]+"))
10083
}

0 commit comments

Comments
 (0)