Skip to content

Commit fc33274

Browse files
author
维术
committed
[Exposed-UI] scan the apk in sdcard recursively.
1 parent 790ad48 commit fc33274

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

VirtualApp/app/src/main/java/io/virtualapp/home/repo/AppRepository.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.text.Collator;
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
20+
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Locale;
2223

@@ -43,6 +44,8 @@ public class AppRepository implements AppDataSource {
4344
"pp/downloader/apk",
4445
"pp/downloader/silent/apk");
4546

47+
private static final int MAX_SCAN_DEPTH = 4;
48+
4649
private Context mContext;
4750

4851
public AppRepository(Context context) {
@@ -85,7 +88,48 @@ public Promise<List<AppInfo>, Throwable, Void> getInstalledApps(Context context)
8588

8689
@Override
8790
public Promise<List<AppInfo>, Throwable, Void> getStorageApps(Context context, File rootDir) {
88-
return VUiKit.defer().when(() -> convertPackageInfoToAppData(context, findAndParseAPKs(context, rootDir, SCAN_PATH_LIST), false));
91+
// return VUiKit.defer().when(() -> convertPackageInfoToAppData(context, findAndParseAPKs(context, rootDir, SCAN_PATH_LIST), false));
92+
return VUiKit.defer().when(() -> convertPackageInfoToAppData(context, findAndParseApkRecursively(context, rootDir,null, 0), false));
93+
}
94+
95+
private List<PackageInfo> findAndParseApkRecursively(Context context, File rootDir, List<PackageInfo> result, int depth) {
96+
if (result == null) {
97+
result = new ArrayList<>();
98+
}
99+
100+
if (depth > MAX_SCAN_DEPTH) {
101+
return result;
102+
}
103+
104+
File[] dirFiles = rootDir.listFiles();
105+
106+
if (dirFiles == null) {
107+
return Collections.emptyList();
108+
}
109+
110+
for (File f: dirFiles) {
111+
if (f.isDirectory()) {
112+
List<PackageInfo> andParseApkRecursively = findAndParseApkRecursively(context, f, new ArrayList<>(), depth + 1);
113+
result.addAll(andParseApkRecursively);
114+
}
115+
116+
if (!(f.isFile() && f.getName().toLowerCase().endsWith(".apk"))) {
117+
continue;
118+
}
119+
120+
PackageInfo pkgInfo = null;
121+
try {
122+
pkgInfo = context.getPackageManager().getPackageArchiveInfo(f.getAbsolutePath(), 0);
123+
pkgInfo.applicationInfo.sourceDir = f.getAbsolutePath();
124+
pkgInfo.applicationInfo.publicSourceDir = f.getAbsolutePath();
125+
} catch (Exception e) {
126+
// Ignore
127+
}
128+
if (pkgInfo != null) {
129+
result.add(pkgInfo);
130+
}
131+
}
132+
return result;
89133
}
90134

91135
private List<PackageInfo> findAndParseAPKs(Context context, File rootDir, List<String> paths) {

0 commit comments

Comments
 (0)