Android11.0内置第三方Launcher并设置为默认,保留Launcher3并可切换

Android11.0内置第三方Launcher并设置为默认,保留Launcher3并可切换

  • Android11.0内置第三方Launcher并设置为默认,保留Launcher3并可切换

Android11.0内置第三方Launcher并设置为默认,保留Launcher3并可切换

本文描述Android11中内置第三方应用为Launcher并保住自带的Launcher3,刷机后系统启动时选择内置第三方应用显示并设置为默认Launcher。

  1. 添加自定义默认launcher属性,修改文件路径:/device/rockchip/rk356x/rk356x.prop;
persist.sys.def_launcher=
persist.sys.def_launcher_enable=false
  1. 在具体的产品中设置自定义属性,例如在rk3568_r中,修改路径:
    /device/rockchip/rk356x/rk3568_r/rk3568_r.mk
PRODUCT_PROPERTY_OVERRIDES += persist.sys.def_launcher=com.yjz.launcher
PRODUCT_PROPERTY_OVERRIDES += persist.sys.def_launcher_enable=true
  1. 选择并设置为默认launcher,修改路径 /frameworks/base/core/java/com/android/internal/app/ResolverActivity.java;
    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;
    }
  1. ResolverActivity里系统中有多个launcher时,是异步查询并通过onPostListReady()方法回调回来在进行设置,这时可能有弹窗闪一下,ResolverListAdapter可以对查询数据逻辑处理只返回内置应用一条数据,ResolverActivity中通过postRebuildListInternal()处理,修改路径:
    /frameworks/base/core/java/com/android/internal/app/ResolverListAdapter.java;
    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.
    }
  1. 如果当前为Launcher3显示 ,在设置-应用–默认应用-主屏幕应用中,切换设置默认为第三方Launcher应用,在第三方应用中,点击导航栏返回按键时,会返回到Launcher3,是因为切换设置默认launcher后,前一个的任务栈还存在,需要删除,修改路径: /packages/apps/PermissionController/src/com/android/permissioncontroller/role/model/HomeRoleBehavior.java;
//********省略代码******
    @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);
    }

//********省略代码******
}
  1. 自定义服务中清除旧任务栈逻辑:
    //********省略代码******
    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);
                        }
                    }
                }
            }
        }
    }
  1. 添加内置第三方应用launcher,在vendor/yjz/launcher/下放置应用及Android.mk
    /vendor/yjz/launcher/Android.mk
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)
  1. 添加YJZLauncher到编译系统里
    /device/rockchip/rk356x/rk3568_r/rk3568_r.mk
PRODUCT_PACKAGES += YJZLauncher
  1. 编译刷机验证

你可能感兴趣的:(Android11.0,android)