增加app在内外显示,支持长按显示隐藏,优化命名,主页app显示还有问题

This commit is contained in:
2025-11-03 08:56:21 +08:00
parent 552ec8dbe9
commit 8dc284c2f3
38 changed files with 1265 additions and 290 deletions

View File

@@ -9,8 +9,8 @@ import android.util.Log;
import com.tencent.mmkv.MMKV;
import com.ttstd.dialer.config.CommonConfig;
import com.ttstd.dialer.db.app.AppInfo;
import com.ttstd.dialer.db.app.AppRepository;
import com.ttstd.dialer.db.app.DesktopSortApp;
import com.ttstd.dialer.gson.GsonUtils;
import com.ttstd.dialer.utils.ApkUtils;
@@ -27,6 +27,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -73,6 +74,8 @@ public class AppManager {
this.add("com.android.settings");
this.add("com.android.dialer");
this.add("com.android.camera2");
this.add("com.android.messaging");
this.add("com.android.contacts");
this.add("com.tencent.mm");
this.add("com.jiangjia.gif");
this.add("com.ss.android.ugc.aweme");
@@ -142,9 +145,9 @@ public class AppManager {
}
// 第一步:获取所有桌面应用(空值安全处理)
private List<DesktopSortApp> getAllDesktopSortApps() {
private List<AppInfo> getAllDesktopSortApps() {
try {
List<DesktopSortApp> result = mAppRepository.getAllApp();
List<AppInfo> result = mAppRepository.getAllApp();
return result != null ? result : Collections.emptyList();
} catch (Exception e) {
Log.e(TAG, "获取桌面应用列表失败", e);
@@ -166,16 +169,27 @@ public class AppManager {
return;
}
List<DesktopSortApp> allFirstApps = allLauncherApps.stream()
List<AppInfo> allFirstApps = allLauncherApps.stream()
.filter(Objects::nonNull)
.map(this::resolveInfoToDesktopApp)
.filter(Objects::nonNull)
.sorted((o1, o2) -> Boolean.compare(ApkUtils.isSystemApp(mContext, o1.getPackageName()), ApkUtils.isSystemApp(mContext, o2.getPackageName())))
.sorted((o1, o2) -> Boolean.compare(ApkUtils.isSystemApp(mContext, o2.getPackageName()), ApkUtils.isSystemApp(mContext, o1.getPackageName())))
.sorted(getAppComparator())
.collect(Collectors.toList());
IntStream.range(0, allFirstApps.size())
.forEach(index -> allFirstApps.get(index).setPosition(index));
.forEach(new IntConsumer() {
@Override
public void accept(int index) {
AppInfo appInfo = allFirstApps.get(index);
if (DEFAULT_APP_PACKAGES.contains(appInfo.getPackageName())) {
appInfo.setOutside(1);
} else {
appInfo.setOutside(0);
}
appInfo.setPosition(index);
}
});
// 批量插入首次数据
allFirstApps.forEach(app -> {
@@ -195,15 +209,15 @@ public class AppManager {
}
// 处理未安装的应用(删除操作)
private CompletableFuture<Void> processUninstalledApps(List<DesktopSortApp> desktopSortApps) {
private CompletableFuture<Void> processUninstalledApps(List<AppInfo> appInfos) {
return CompletableFuture.runAsync(() -> {
notifyProgress(STEP_REMOVE_UNINSTALLED, 1, TOTAL_STEPS_NORMAL);
if (desktopSortApps.isEmpty()) {
if (appInfos.isEmpty()) {
Log.w(TAG, "桌面应用列表为空,跳过删除处理");
return;
}
List<Integer> ids = desktopSortApps.stream()
List<Integer> ids = appInfos.stream()
.filter(Objects::nonNull)
.filter(app -> !ApkUtils.isInstalled(mContext, app.getPackageName()))
.map(app -> {
@@ -240,7 +254,7 @@ public class AppManager {
return;
}
List<DesktopSortApp> newApps = resolveInfos.stream()
List<AppInfo> newApps = resolveInfos.stream()
.filter(Objects::nonNull)
.map(this::resolveInfoToDesktopApp)
.filter(Objects::nonNull)
@@ -273,20 +287,20 @@ public class AppManager {
private CompletableFuture<Void> updatePosition() {
return CompletableFuture.runAsync(() -> {
try {
List<DesktopSortApp> desktopSortApps = getAllDesktopSortApps();
if (desktopSortApps.isEmpty()) {
List<AppInfo> appInfos = getAllDesktopSortApps();
if (appInfos.isEmpty()) {
Log.w(TAG, "无应用数据,跳过位置更新");
return;
}
List<DesktopSortApp> sortedApps = desktopSortApps.stream()
List<AppInfo> sortedApps = appInfos.stream()
.filter(Objects::nonNull)
.sorted(Comparator.comparingInt(DesktopSortApp::getPosition))
.sorted(Comparator.comparingInt(AppInfo::getPosition))
.collect(Collectors.toList());
IntStream.range(0, sortedApps.size())
.forEach(index -> {
DesktopSortApp app = sortedApps.get(index);
AppInfo app = sortedApps.get(index);
app.setPosition(index);
try {
mAppRepository.update(app);
@@ -303,16 +317,16 @@ public class AppManager {
}
// 工具方法ResolveInfo转换为DesktopSortApp
private DesktopSortApp resolveInfoToDesktopApp(ResolveInfo resolveInfo) {
private AppInfo resolveInfoToDesktopApp(ResolveInfo resolveInfo) {
ComponentName component = new ComponentName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name
);
return new DesktopSortApp(mContext, component);
return new AppInfo(mContext, component);
}
// 工具方法:检查应用是否已存在
private boolean isAppExists(DesktopSortApp app) {
private boolean isAppExists(AppInfo app) {
try {
return mAppRepository.checkAppInfoExists(app.getPackageName(), app.getClassName()) > 0;
} catch (Exception e) {
@@ -322,8 +336,8 @@ public class AppManager {
}
// 工具方法:获取应用排序器(统一排序逻辑)
private Comparator<DesktopSortApp> getAppComparator() {
return Comparator.comparing(DesktopSortApp::getLabel, Collator.getInstance(Locale.CHINESE));
private Comparator<AppInfo> getAppComparator() {
return Comparator.comparing(AppInfo::getLabel, Collator.getInstance(Locale.CHINESE));
}
public void updateApp(String packageName) {
@@ -347,7 +361,7 @@ public class AppManager {
// 重构getAllApp方法复用现有处理逻辑
public void refreshAllApps() {
CompletableFuture.runAsync(() -> {
List<DesktopSortApp> allApps = getAllDesktopSortApps();
List<AppInfo> allApps = getAllDesktopSortApps();
if (allApps.isEmpty()) {
processFirstTimeApps().join();
} else {
@@ -362,12 +376,12 @@ public class AppManager {
PackageManager pm = mContext.getPackageManager();
List<ResolveInfo> resolveInfos = ApkUtils.getAllLauncherResolveInfo(mContext);
List<DesktopSortApp> defaultApps = resolveInfos.stream()
List<AppInfo> defaultApps = resolveInfos.stream()
.filter(ri -> DEFAULT_APP_PACKAGES.contains(ri.activityInfo.packageName))
.sorted((o1, o2) -> Collator.getInstance(Locale.CHINESE)
.compare(o1.activityInfo.loadLabel(pm), o2.activityInfo.loadLabel(pm)))
.map(ri -> new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name))
.map(component -> new DesktopSortApp(mContext, component))
.map(component -> new AppInfo(mContext, component))
.sorted((a, b) -> Boolean.compare(
ApkUtils.isSystemApp(mContext, b.getPackageName()),
ApkUtils.isSystemApp(mContext, a.getPackageName())))