11package com.tans.tfiletransporter.file
22
3+ import android.content.Context
34import android.os.Build
5+ import android.os.Environment
46import com.tans.tfiletransporter.transferproto.fileexplore.model.FileExploreDir
57import com.tans.tfiletransporter.transferproto.fileexplore.model.FileExploreFile
68import com.tans.tfiletransporter.transferproto.fileexplore.model.ScanDirReq
@@ -9,39 +11,55 @@ import java.io.File
911
1012val LOCAL_DEVICE = " ${Build .BRAND } ${Build .MODEL } "
1113
12- fun ScanDirReq.scanChildren (rootDir : File ): ScanDirResp {
13- val rootDirString = rootDir.canonicalPath
14- val currentFile = File (rootDir, requestPath)
15- return if (currentFile.isDirectory && currentFile.canRead()) {
16- try {
17- val children = currentFile.listFiles() ? : emptyArray<File >()
18- val childrenDirs = mutableListOf<FileExploreDir >()
19- val childrenFiles = mutableListOf<FileExploreFile >()
20- for (c in children) {
21- if (c.canRead()) {
22- if (c.isDirectory) {
23- childrenDirs.add(c.toFileExploreDir(rootDirString))
24- } else {
25- if (c.length() > 0 ) {
26- childrenFiles.add(c.toFileExploreFile(rootDirString))
14+ fun ScanDirReq.scanChildren (context : Context ): ScanDirResp {
15+ return try {
16+ val fileSeparator = File .separator
17+ if (requestPath.isBlank() || requestPath == fileSeparator) {
18+ val defaultStorageFile = Environment .getExternalStorageDirectory()
19+ val othersSdCardFiles =
20+ getSdCardPaths(context, true ).map { File (it) }.filter { ! defaultStorageFile.hasTargetParent(it) }
21+ ScanDirResp (
22+ path = fileSeparator,
23+ childrenFiles = emptyList(),
24+ childrenDirs = listOf (FileExploreDir (
25+ name = " Default Storage" ,
26+ path = defaultStorageFile.canonicalPath,
27+ childrenCount = defaultStorageFile.listFiles()?.size ? : 0 ,
28+ lastModify = defaultStorageFile.lastModified()
29+ )) + othersSdCardFiles.map { it.toFileExploreDir() }
30+ )
31+ } else {
32+ val currentFile = File (requestPath)
33+ if (currentFile.isDirectory && currentFile.canRead()) {
34+ val children = currentFile.listFiles() ? : emptyArray<File >()
35+ val childrenDirs = mutableListOf<FileExploreDir >()
36+ val childrenFiles = mutableListOf<FileExploreFile >()
37+ for (c in children) {
38+ if (c.canRead()) {
39+ if (c.isDirectory) {
40+ childrenDirs.add(c.toFileExploreDir())
41+ } else {
42+ if (c.length() > 0 ) {
43+ childrenFiles.add(c.toFileExploreFile())
44+ }
2745 }
2846 }
2947 }
48+ ScanDirResp (
49+ path = requestPath,
50+ childrenDirs = childrenDirs,
51+ childrenFiles = childrenFiles
52+ )
53+ } else {
54+ ScanDirResp (
55+ path = requestPath,
56+ childrenDirs = emptyList(),
57+ childrenFiles = emptyList()
58+ )
3059 }
31- ScanDirResp (
32- path = requestPath,
33- childrenDirs = childrenDirs,
34- childrenFiles = childrenFiles
35- )
36- } catch (e: Throwable ) {
37- e.printStackTrace()
38- ScanDirResp (
39- path = requestPath,
40- childrenDirs = emptyList(),
41- childrenFiles = emptyList()
42- )
4360 }
44- } else {
61+ } catch (e: Throwable ) {
62+ e.printStackTrace()
4563 ScanDirResp (
4664 path = requestPath,
4765 childrenDirs = emptyList(),
@@ -50,11 +68,11 @@ fun ScanDirReq.scanChildren(rootDir: File): ScanDirResp {
5068 }
5169}
5270
53- fun File.toFileExploreDir (rootDirString : String ): FileExploreDir {
71+ fun File.toFileExploreDir (): FileExploreDir {
5472 return if (isDirectory) {
5573 FileExploreDir (
5674 name = name,
57- path = this .canonicalPath.removePrefix(rootDirString) ,
75+ path = this .canonicalPath,
5876 childrenCount = listFiles()?.size ? : 0 ,
5977 lastModify = lastModified()
6078 )
@@ -63,11 +81,11 @@ fun File.toFileExploreDir(rootDirString: String): FileExploreDir {
6381 }
6482}
6583
66- fun File.toFileExploreFile (rootDirString : String ): FileExploreFile {
84+ fun File.toFileExploreFile (): FileExploreFile {
6785 return if (isFile) {
6886 FileExploreFile (
6987 name = name,
70- path = this .canonicalPath.removePrefix(rootDirString) ,
88+ path = this .canonicalPath,
7189 size = length(),
7290 lastModify = lastModified()
7391 )
0 commit comments