本文描述Android11中内置第三方应用为Launcher并保住自带的Launcher3,刷机后系统启动时选择内置第三方应用显示并设置为默认Launcher。
persist.sys.def_launcher=
persist.sys.def_launcher_enable=false
PRODUCT_PROPERTY_OVERRIDES += persist.sys.def_launcher=com.yjz.launcher
PRODUCT_PROPERTY_OVERRIDES += persist.sys.def_launcher_enable=true
public final void onPostListReady(ResolverListAdapter listAdapter, boolean doPostProcessing,
boolean rebuildCompleted) {
if (isAutolaunching()) {
return;
}
//新增
if (rebuildCompleted && hasSetDefLauncher()) {
return;
}
//*******省略代码******
}
final boolean postRebuildListInternal(boolean rebuildCompleted) {
//*******省略代码******
//添加
if (rebuildCompleted && (hasSetDefLauncher() || maybeAutolaunchActivity())) {
return true;
}
//*******省略代码******
}
//添加
private boolean hasSetDefLauncher() {
if (SystemProperties.getBoolean("persist.sys.def_launcher_enable", false)) {
String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
if (defLauncher != null && !"".equals(defLauncher)) {
int size = mMultiProfilePagerAdapter.getActiveListAdapter().getDisplayResolveInfoCount();
for (int i = 0; i < size; i++) {
DisplayResolveInfo dInfo = mMultiProfilePagerAdapter.getActiveListAdapter().getDisplayResolveInfo(i);
if (null != dInfo && defLauncher.equals(dInfo.getResolveInfo().activityInfo.packageName)) {
Log.d(TAG, "find need set default launcher, set default launcher--->" + dInfo.getResolveInfo().activityInfo.packageName);
startSelected(i, true, false);
return true;
}
}
}
}
return false;
}
protected boolean rebuildList(boolean doPostProcessing) {
//********省略代码******
if (mBaseResolveList != null) {
currentResolveList = mUnfilteredResolveList = new ArrayList<>();
mResolverListController.addResolveListDedupe(currentResolveList,
mResolverListCommunicator.getTargetIntent(),
mBaseResolveList);
} else {
currentResolveList = mUnfilteredResolveList =
mResolverListController.getResolversForIntent(
/* shouldGetResolvedFilter= */ true,
mResolverListCommunicator.shouldGetActivityMetadata(),
mIntents);
if (currentResolveList == null) {
processSortedList(currentResolveList, doPostProcessing);
return true;
}
List<ResolvedComponentInfo> originalList =
mResolverListController.filterIneligibleActivities(currentResolveList,
true);
if (originalList != null) {
mUnfilteredResolveList = originalList;
}
}
//添加
if (null != currentResolveList && SystemProperties.getBoolean("persist.sys.def_launcher_enable", false)) {
String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
if (defLauncher != null && !"".equals(defLauncher)) {
for (ResolvedComponentInfo info : currentResolveList) {
ResolveInfo resolveInfo = info.getResolveInfoAt(0);
if (defLauncher.equals(resolveInfo.activityInfo.packageName)) {
currentResolveList = new ArrayList<>();
currentResolveList.add(info);
Log.d(TAG, "find set default launcher--->" + resolveInfo.activityInfo.packageName);
break;
}
}
}
}
//********省略代码******
// So far we only support a single other profile at a time.
// The first one we see gets special treatment.
}
//********省略代码******
@Override
public void onHolderSelectedAsUser(@NonNull Role role, @NonNull String packageName,
@NonNull UserHandle user, @NonNull Context context) {
// Launch the new home app so the change is immediately visible even if the home button is
// not pressed.
//这里是发送一个广播到自定义服务中去删除旧的任务栈
Intent it = new Intent("com.yjz.ACTION_SET_DEF_LAUNCHER_ENABLE");
it.putExtra("packageName", packageName);
context.sendBroadcast(it);
Intent intent = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_HOME)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
//********省略代码******
}
//********省略代码******
private class CustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("com.yjz.ACTION_SET_DEF_LAUNCHER_ENABLE".equals(action)) {
String packageName = intent.getStringExtra("packageName");
if (null != packageName && !"".equals(packageName)) {
String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
if (defLauncher != null && !"".equals(defLauncher) && defLauncher.equals(packageName)) {
SystemProperties.set("persist.sys.def_launcher_enable", "true");
} else {
SystemProperties.set("persist.sys.def_launcher_enable", "false");
}
clearOldLauncherTask(packageName, context);
}
}
}
}
private void clearOldLauncherTask(String packageName, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = manager.getRunningTasks(Integer.MAX_VALUE);
if (null != list) {
for (ActivityManager.RunningTaskInfo item : list) {
if (item.configuration.windowConfiguration.getActivityType() == WindowConfiguration.ACTIVITY_TYPE_HOME) {
if (!item.realActivity.getPackageName().equals(packageName)) {
final int tId = item.taskId;
try {
ActivityTaskManager.getService().removeTask(tId);
} catch (RemoteException e) {
Log.w(TAG, "Failed to remove task=" + tId, e);
}
}
}
}
}
}
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := YJZLauncher
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
include $(BUILD_PREBUILT)
PRODUCT_PACKAGES += YJZLauncher