Android 8.1 添加 自定义可切换桌面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lb5761311/article/details/84290702

现在产品有一个需求,在工厂测试的时候启动时Android原生桌面launcher3,测试完毕后启动我们产品默认的app。
所以自己在Android启动 桌面的时候做一个属性判断,如果属性

persist.defaulthome = false

就启动launcher3, = true就启动我们产品app。
这个属性修改由我们产品测试apk 在产品测试完成之后修改。

   SystemProperties.set("persist.defaulthome","true");

在Android 8.1 由于seLinux权限管理很严格,默认app 修改这个属性一定会报 avc denied 错误,
所以第一步,添加persist.defaulthome 权限控制修改
device/mediatek/sepolicy/basic/non_plat/non_plat/property.te
添加
定义新的属性声明

type persist_defaulthome_prop, property_type;

device/mediatek/sepolicy/basic/non_plat/non_plat/property_contexts
定义persist.defaulthome 属于persist_defaulthome_prop 这个域

persist.defaulthome u:object_r:persist_defaulthome_prop:s0

修改 系统app对于 persist_defaulthome操作权限
device/mediatek/sepolicy/basic/non_plat/system_app.te

allow system_app persist_defaulthome_prop:property_service set;
allow system_app persist_defaulthome_prop:file {getattr open read };

这样我们的测试app 在测试完成之后,只要一行代码就完成属性修改

   SystemProperties.set("persist.defaulthome","true");

Android启动 最终会调用 startHomeActivityLocked 启动默认桌面,所以通过这个函数来获取属性值最终选择启动的app。
文件 ActivityManagerService.java

private static final String DEFAULT_HOME = "persist.defaulthome";
boolean startHomeActivityLocked(int userId, String reason) {
        if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                && mTopAction == null) {
            // We are running in factory test mode, but unable to find
            // the factory test app, so just sit around displaying the
            // error message and don't try to start anything.
            return false;
        }
        if(GetProvisioned()!=1){
            PutProvisioned();
        }
        Intent intent = getHomeIntent();
        ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
        if (aInfo != null) {
           //add  start  by blv --------------
            Log.e("PPTV","start home!!!!!!!");
            PackageManager pm = mContext.getPackageManager();
            Intent newintent = new Intent(Intent.ACTION_MAIN);
            newintent.addCategory(Intent.CATEGORY_HOME);
            List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(newintent, 0);
            boolean defaultHome =  SystemProperties.getBoolean(DEFAULT_HOME,false);
			Log.e("PPTV","SystemProperties  defaultHome is =======" + defaultHome);
            if(resolveInfoList != null){
                  int size = resolveInfoList.size();
                  for(int i = 0; i < size; i++){
                      ResolveInfo rInfo = resolveInfoList.get(i);
                      if(defaultHome){
                          if(rInfo.activityInfo.name.equals("xxxxxxx")){
                              Log.e("PPTV","start custom home!!!!!!!");
                              aInfo = rInfo.activityInfo;
                          }
			         }else{
                          if(rInfo.activityInfo.name.equals("com.android.launcher3.Launcher")){
                              Log.e("PPTV","start default home!!!!!!!");
                              aInfo = rInfo.activityInfo;
                          }
                    }
                }
            }
            // add  end ----------
            intent.setComponent(new ComponentName(
                    aInfo.applicationInfo.packageName, aInfo.name));
            // Don't do this if the home app is currently being
            // instrumented.
            aInfo = new ActivityInfo(aInfo);
            aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
            ProcessRecord app = getProcessRecordLocked(aInfo.processName,
                    aInfo.applicationInfo.uid, true);
            if (app == null || app.instr == null) {
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);
                // For ANR debugging to verify if the user activity is the one that actually
                // launched.
                final String myReason = reason + ":" + userId + ":" + resolvedUserId;
                mActivityStarter.startHomeActivityLocked(intent, aInfo, myReason);
            }
        } else {
            Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
        }

        return true;
    }

猜你喜欢

转载自blog.csdn.net/lb5761311/article/details/84290702