refactor(settings): 替换自定义 ToggleButton 为 SwitchButton 并优化默认 launcher 设置逻辑
This commit is contained in:
@@ -35,8 +35,6 @@ import android.util.Log;
|
||||
import android.view.SurfaceControl;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import com.ttstd.dialer.BuildConfig;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
@@ -484,47 +482,44 @@ public class SystemUtils {
|
||||
* @param launcherActivityClass 你的桌面 Activity 类,例如 MyLauncherActivity.class
|
||||
*/
|
||||
public static void setDefaultLauncher(Context context, Class<?> launcherActivityClass) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
addRoleHolderAsUser(context, context.getPackageName());
|
||||
} else {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
// 1. 创建桌面过滤的 IntentFilter
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
// 1. 创建桌面过滤的 IntentFilter
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
// 2. 查询当前系统中所有的桌面应用(用来构建 ComponentName 数组)
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
// 2. 查询当前系统中所有的桌面应用(用来构建 ComponentName 数组)
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo info = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
if (info.match > bestMatch) {
|
||||
bestMatch = info.match; // 获取最匹配的值
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo info = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
if (info.match > bestMatch) {
|
||||
bestMatch = info.match; // 获取最匹配的值
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 构建你自己的桌面 ComponentName
|
||||
ComponentName myLauncher = new ComponentName(context, launcherActivityClass);
|
||||
// 3. 构建你自己的桌面 ComponentName
|
||||
ComponentName myLauncher = new ComponentName(context, launcherActivityClass);
|
||||
|
||||
// 4. 替换首选 Activity(核心步骤)
|
||||
try {
|
||||
// 注意:API 29 (Android 10) 中此方法对第三方应用废弃,但对系统签名应用依然有效
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, myLauncher);
|
||||
Logger.i("LauncherHelper", "成功设置为默认桌面");
|
||||
|
||||
// 可选:发送一个回到桌面的 Intent 来验证
|
||||
// Intent startHome = new Intent(Intent.ACTION_MAIN);
|
||||
// startHome.addCategory(Intent.CATEGORY_HOME);
|
||||
// startHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
// context.startActivity(startHome);
|
||||
|
||||
} catch (SecurityException e) {
|
||||
Logger.e("LauncherHelper", "缺少权限或未生效,请检查系统签名是否正确", e);
|
||||
} catch (Exception e) {
|
||||
Logger.e("LauncherHelper", "设置默认桌面失败", e);
|
||||
// 4. 替换首选 Activity(核心步骤)
|
||||
try {
|
||||
// 注意:API 29 (Android 10) 中此方法对第三方应用废弃,但对系统签名应用依然有效
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, myLauncher);
|
||||
Logger.i("LauncherHelper", "成功设置为默认桌面");
|
||||
} catch (SecurityException e) {
|
||||
Logger.e("LauncherHelper", "缺少权限或未生效,请检查系统签名是否正确", e);
|
||||
} catch (Exception e) {
|
||||
Logger.e("LauncherHelper", "设置默认桌面失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,25 +602,39 @@ public class SystemUtils {
|
||||
public static void setOtherDefaultLauncher(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
|
||||
homeIntent.addCategory(Intent.CATEGORY_HOME);
|
||||
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
Optional<ResolveInfo> systemLauncher = resolveInfos.stream().filter(new Predicate<ResolveInfo>() {
|
||||
@Override
|
||||
public boolean test(ResolveInfo resolveInfo) {
|
||||
return !BuildConfig.APPLICATION_ID.equals(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
}).filter(new Predicate<ResolveInfo>() {
|
||||
@Override
|
||||
public boolean test(ResolveInfo resolveInfo) {
|
||||
return ApkUtils.isSystemApp(context, resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
}).findAny();
|
||||
systemLauncher.ifPresent(resolveInfo -> addRoleHolderAsUser(context, resolveInfo.activityInfo.packageName));
|
||||
|
||||
Optional<ResolveInfo> systemLauncher = resolveInfos.stream()
|
||||
.filter(info -> !context.getPackageName().equals(info.activityInfo.packageName))
|
||||
.filter(info -> ApkUtils.isSystemApp(context, info.activityInfo.packageName))
|
||||
.findFirst();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
systemLauncher.ifPresent(info -> addRoleHolderAsUser(context, info.activityInfo.packageName));
|
||||
} else {
|
||||
pm.clearPackagePreferredActivities(context.getPackageName());
|
||||
systemLauncher.ifPresent(info -> {
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
|
||||
filter.addCategory(Intent.CATEGORY_HOME);
|
||||
filter.addCategory(Intent.CATEGORY_DEFAULT);
|
||||
|
||||
int bestMatch = 0;
|
||||
ComponentName[] set = new ComponentName[resolveInfos.size()];
|
||||
for (int i = 0; i < resolveInfos.size(); i++) {
|
||||
ResolveInfo res = resolveInfos.get(i);
|
||||
set[i] = new ComponentName(res.activityInfo.packageName, res.activityInfo.name);
|
||||
if (res.match > bestMatch) bestMatch = res.match;
|
||||
}
|
||||
ComponentName otherLauncher = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
|
||||
try {
|
||||
pm.replacePreferredActivity(filter, bestMatch, set, otherLauncher);
|
||||
} catch (Exception e) {
|
||||
Logger.e(TAG, "setOtherDefaultLauncher error", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user