11package com.simplemobiletools.filemanager.helpers
22
3- import android.text.TextUtils
43import com.simplemobiletools.commons.extensions.areDigitsOnly
54import com.simplemobiletools.commons.extensions.showErrorToast
65import com.simplemobiletools.commons.models.FileDirItem
76import com.simplemobiletools.filemanager.activities.SimpleActivity
87import com.simplemobiletools.filemanager.extensions.config
98import com.stericson.RootShell.execution.Command
109import com.stericson.RootTools.RootTools
10+ import java.io.File
1111import java.util.*
1212
1313class RootHelpers {
14- fun askRootIFNeeded (activity : SimpleActivity , callback : (success: Boolean ) -> Unit ) {
15- val command = object : Command (0 , " ls -la | awk '{ print $2 }' " ) {
14+ fun askRootIfNeeded (activity : SimpleActivity , callback : (success: Boolean ) -> Unit ) {
15+ val command = object : Command (0 , " ls -lA " ) {
1616 override fun commandOutput (id : Int , line : String ) {
17- activity.config.lsHasHardLinksColumn = line.areDigitsOnly()
1817 callback(true )
1918 super .commandOutput(id, line)
2019 }
@@ -28,43 +27,96 @@ class RootHelpers {
2827 }
2928 }
3029
31- fun getFiles (activity : SimpleActivity , path : String , callback : (fileDirItems: ArrayList <FileDirItem >) -> Unit ) {
30+ fun getFiles (activity : SimpleActivity , path : String , callback : (originalPath: String , fileDirItems: ArrayList <FileDirItem >) -> Unit ) {
3231 val files = ArrayList <FileDirItem >()
33- val showHidden = activity.config.shouldShowHidden
34- val sizeColumnIndex = if (activity.config.lsHasHardLinksColumn) 5 else 4
32+ val hiddenArgument = if ( activity.config.shouldShowHidden) " -A " else " "
33+ val cmd = " ls $hiddenArgument$path "
3534
36- val cmd = " ls -la $path | awk '{ system(\" echo \" \$ 1\" \" \$ $sizeColumnIndex \" `find ${path.trimEnd(' /' )} /\" \$ NF\" -mindepth 1 -maxdepth 1 | wc -l` \" \$ NF\" \" )}'"
3735 val command = object : Command (0 , cmd) {
3836 override fun commandOutput (id : Int , line : String ) {
39- val parts = line.split(" " )
40- if (parts.size >= 4 ) {
41- val permissions = parts[0 ].trim()
42- val isDirectory = permissions.startsWith(" d" )
43- val isFile = permissions.startsWith(" -" )
44- val size = if (isFile) parts[1 ].trim() else " 0"
45- val childrenCnt = if (isFile) " 0" else parts[2 ].trim()
46- val filename = TextUtils .join(" " , parts.subList(3 , parts.size)).trimStart(' /' )
47-
48- if ((! showHidden && filename.startsWith(" ." )) || (! isDirectory && ! isFile) || ! size.areDigitsOnly() || ! childrenCnt.areDigitsOnly()) {
49- super .commandOutput(id, line)
50- return
51- }
37+ val file = File (path, line)
38+ val isDirectory = file.isDirectory
39+ val fileDirItem = FileDirItem (file.absolutePath, line, isDirectory, 0 , 0 )
40+ files.add(fileDirItem)
41+ super .commandOutput(id, line)
42+ }
43+
44+ override fun commandCompleted (id : Int , exitcode : Int ) {
45+ getChildrenCount(activity, files, path, callback)
46+ super .commandCompleted(id, exitcode)
47+ }
48+ }
49+
50+ runCommand(activity, command)
51+ }
52+
53+ private fun getChildrenCount (activity : SimpleActivity , files : ArrayList <FileDirItem >, path : String , callback : (originalPath: String , fileDirItems: ArrayList <FileDirItem >) -> Unit ) {
54+ val hiddenArgument = if (activity.config.shouldShowHidden) " -A " else " "
55+ var cmd = " "
56+ files.forEach {
57+ cmd + = if (it.isDirectory) {
58+ " ls $hiddenArgument${it.path} |wc -l;"
59+ } else {
60+ " echo 0;"
61+ }
62+ }
63+ cmd = cmd.trimEnd(' ;' ) + " | cat"
64+
65+ val lines = ArrayList <String >()
66+ val command = object : Command (0 , cmd) {
67+ override fun commandOutput (id : Int , line : String ) {
68+ lines.add(line)
69+ super .commandOutput(id, line)
70+ }
5271
53- val fileSize = size.toLong()
54- val filePath = " ${path.trimEnd(' /' )} /$filename "
55- val fileDirItem = FileDirItem (filePath, filename, isDirectory, childrenCnt.toInt(), fileSize)
56- files.add(fileDirItem)
72+ override fun commandCompleted (id : Int , exitcode : Int ) {
73+ files.forEachIndexed { index, fileDirItem ->
74+ val childrenCount = lines[index]
75+ if (childrenCount.areDigitsOnly()) {
76+ fileDirItem.children = childrenCount.toInt()
77+ }
5778 }
79+ getFileSizes(activity, files, path, callback)
80+ super .commandCompleted(id, exitcode)
81+ }
82+ }
83+
84+ runCommand(activity, command)
85+ }
5886
87+ private fun getFileSizes (activity : SimpleActivity , files : ArrayList <FileDirItem >, path : String , callback : (originalPath: String , fileDirItems: ArrayList <FileDirItem >) -> Unit ) {
88+ var cmd = " "
89+ files.forEach {
90+ cmd + = if (it.isDirectory) {
91+ " echo 0;"
92+ } else {
93+ " stat -c %s ${it.path} ;"
94+ }
95+ }
96+
97+ val lines = ArrayList <String >()
98+ val command = object : Command (0 , cmd) {
99+ override fun commandOutput (id : Int , line : String ) {
100+ lines.add(line)
59101 super .commandOutput(id, line)
60102 }
61103
62104 override fun commandCompleted (id : Int , exitcode : Int ) {
63- callback(files)
105+ files.forEachIndexed { index, fileDirItem ->
106+ val childrenCount = lines[index]
107+ if (childrenCount.areDigitsOnly()) {
108+ fileDirItem.size = childrenCount.toLong()
109+ }
110+ }
111+ callback(path, files)
64112 super .commandCompleted(id, exitcode)
65113 }
66114 }
67115
116+ runCommand(activity, command)
117+ }
118+
119+ private fun runCommand (activity : SimpleActivity , command : Command ) {
68120 try {
69121 RootTools .getShell(true ).add(command)
70122 } catch (e: Exception ) {
0 commit comments