Android16 默认第三方Launcher为主桌面

frameworks/base/core/java/com/android/internal/app/ResolverActivity.java

java 复制代码
+import android.os.SystemProperties;

protected void onCreate(Bundle savedInstanceState, Intent intent,
            CharSequence title, int defaultTitleRes, Intent[] initialIntents,
            List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {
        
         *******
+
+        if(mResolvingHome){
+            setDefaultLauncher();
+            finish();
+            return;
+        }
+
         if (configureContentView()) {
             return;
         }

     }


+
+    //add
+    private void setDefaultLauncher() {
+        try {
+            String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
+            if(defLauncher != null && defLauncher.length() > 0) {
+                Log.i("deflauncher", "---persist.sys.def_launcher=" + defLauncher);
+                String[] packConfig = defLauncher.split("/");
+                String default_pckname = packConfig[0];
+                String default_clsname = packConfig[1];
+                ComponentName preActivity = new ComponentName(default_pckname, default_clsname);
+                Log.i("deflauncher", "---default_pckname="+default_pckname);
+                Log.i("deflauncher", "---default_clsname="+default_clsname);
+
+                Intent homeIntent = new Intent();
+                homeIntent.setAction(Intent.ACTION_MAIN);
+                homeIntent.addCategory(Intent.CATEGORY_HOME);
+                homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
+
+                List<ResolveInfo> list = new ArrayList<ResolveInfo>();
+                PackageManager pm = getPackageManager();
+                list = pm.queryIntentActivities(homeIntent, 0);
+                Log.i("deflauncher", "---queryIntentActivity list 1 size="+list.size());
+
+                ResolveInfo info = mPm.resolveActivity(homeIntent, 0);
+                if(info!=null) Log.i("deflauncher", "---queryIntentActivity packageName="+info.activityInfo.packageName);
+
+                ComponentName[] set = new ComponentName[list.size()];
+                int bestMatch = 0;
+                for (int i = 0; i < list.size(); i++) {
+                    ResolveInfo r = list.get(i);
+                    set[i] = new ComponentName(r.activityInfo.packageName,r.activityInfo.name);
+                    Log.i("deflauncher", "---queryIntentActivity="+r.activityInfo.packageName);
+                    if (r.match > bestMatch) bestMatch = r.match;
+                }
+
+                IntentFilter filter = new IntentFilter();
+                filter.addAction(Intent.ACTION_MAIN);
+                filter.addCategory(Intent.CATEGORY_HOME);
+                filter.addCategory(Intent.CATEGORY_DEFAULT);
+
+                pm.addPreferredActivity(filter, bestMatch, set, preActivity);
+                Log.i("deflauncher", "---set def_launcher end");
+
+                startDefaultLauncher();
+            }else{
+                Log.i("deflauncher", "---persist.sys.def_launcher is null");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private boolean startDefaultLauncher(){
+        String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
+        if(defLauncher == null){
+            Log.i("deflauncher", "persist.sys.def_launcher not exists");
+            return false;
+        }
+        String[] packConfig = defLauncher.split("/");
+        String defPackageName = packConfig[0];
+        String defClassName = packConfig[1];
+
+        try {
+            Intent intent1 = new Intent(Intent.ACTION_MAIN);
+            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            intent1.addCategory(Intent.CATEGORY_LAUNCHER);
+            intent1.setComponent(new ComponentName(defPackageName, defClassName));
+            startActivity(intent1);
+            return true;
+        }catch (Exception e){
+            Log.i("deflauncher", defPackageName + " start Fail");
+            return false;
+        }
+    }
+    // add end

frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java

java 复制代码
 boolean startHomeOnTaskDisplayArea(int userId, String reason, TaskDisplayArea taskDisplayArea,
            boolean allowInstrumenting, boolean fromHomeKey) {

         ......


        // Updates the home component of the intent.
        homeIntent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
        homeIntent.setFlags(homeIntent.getFlags() | FLAG_ACTIVITY_NEW_TASK);
        // Updates the extra information of the intent.
        if (fromHomeKey) {
            homeIntent.putExtra(WindowManagerPolicy.EXTRA_FROM_HOME_KEY, true);
        }
        homeIntent.putExtra(WindowManagerPolicy.EXTRA_START_REASON, reason);


+        //add start
+        final PackageManager mPm = mService.mContext.getPackageManager();
+        Intent homeIntent1=new Intent();
+        homeIntent1.setAction(Intent.ACTION_MAIN);
+        homeIntent1.addCategory(Intent.CATEGORY_HOME);
+        homeIntent1.addCategory(Intent.CATEGORY_DEFAULT);
+
+        ResolveInfo info = mPm.resolveActivity(homeIntent1, PackageManager.MATCH_DEFAULT_ONLY);
+        if(info != null) Log.d("deflauncher","startHomeOnTaskDisplayArea currentPkg="+info.activityInfo.packageName);
+
+        //if there is a default Launcher?
+        String defLauncher = SystemProperties.get("persist.sys.def_launcher", null);
+        if(defLauncher != null && defLauncher.length() > 0){
+            Log.i("deflauncher", "startHomeOnTaskDisplayArea def_launcher="+defLauncher);
+            String[] packConfig = defLauncher.split("/");
+            String default_pckname = packConfig[0];
+            String default_clsname = packConfig[1];
+            ComponentName DefaultLauncher = new ComponentName(default_pckname, default_clsname);
+
+            ArrayList<ResolveInfo> homeActivities = new ArrayList<ResolveInfo>();
+            ComponentName currentDefaultHome = mPm.getHomeActivities(homeActivities);
+            ComponentName[] mHomeComponentSet = new ComponentName[homeActivities.size()];
+            for (int i = 0; i < homeActivities.size(); i++) {
+                final ResolveInfo r = homeActivities.get(i);
+                final ActivityInfo activityInfo = r.activityInfo;
+                ComponentName activityName = new ComponentName(activityInfo.packageName, activityInfo.name);
+                mHomeComponentSet[i] = activityName;
+                Log.i("deflauncher", "startHomeOnTaskDisplayArea packageName="+activityInfo.packageName);
+            }
+            IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
+            mHomeFilter.addCategory(Intent.CATEGORY_HOME);
+            mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
+
+            mPm.replacePreferredActivity(mHomeFilter,IntentFilter.MATCH_CATEGORY_EMPTY,
+                    mHomeComponentSet, DefaultLauncher);
+        }
+        //add end
+


        // Update the reason for ANR debugging to verify if the user activity is the one that
        // actually launched.
        final String myReason = reason + ":" + userId + ":" + UserHandle.getUserId(
                aInfo.applicationInfo.uid) + ":" + taskDisplayArea.getDisplayId();
        mService.getActivityStartController().startHomeActivity(homeIntent, aInfo, myReason,
                taskDisplayArea);
        return true;
    }
相关推荐
Android系统攻城狮3 个月前
Android tinyalsa深度解析之pcm_plugin_write调用流程与实战(一百七十九)
android·pcm·tinyalsa·android16·音频进阶·android音频进阶
Android系统攻城狮4 个月前
Android16进阶之BassBoost.setStrength调用流程与实战(三百零四)
android16·音频进阶·android音频进阶·bassbosst
longji4 个月前
android 01 AOSP android16 aaos 编译及webview升级
android·aaos·aosp·android16
Android系统攻城狮4 个月前
Android16进阶之MediaRecorder.getPreferredDevice调用流程与实战(二百六十九)
mediarecorder·android16·音频进阶·android音频进阶·mediarecord
Android系统攻城狮5 个月前
Android16进阶之MediaRecorder.setAudioEncodingBitRate调用流程与实战(二百六十四)
android16·音频进阶·mediarecoder
Android系统攻城狮5 个月前
Android16进阶之MediaRecorder.setVideoSource调用流程与实战(二百五十六)
android16·音频进阶·mediarecord
Android系统攻城狮5 个月前
Android16进阶之MediaRecorder.setVideoEncoder调用流程与实战(二百五十八)
mediarecorder·android16·android音频进阶
Android系统攻城狮5 个月前
Android16进阶之MediaPlayer.deselectTrack调用流程与实战(二百五十一)
mediaplayer·android16·音频进阶·mediaplyer
loitawu5 个月前
Rockchip Android16 系统裁剪指南
android·android16·android裁剪·系统裁剪·rockchip app