Android客制化------设置默认Launcher

要看客户是否需要保留系统默认的Launcher,如果需要保留的话,则需要在framework中去设置,如果不需要的,就挺好处理,在mk文件中添加这个属性LOCAL_OVERRIDES_PACKAGES := Launcher3(实际没有去尝试过这个属性,一般都是让系统不编译所有的Launcher,不同的平台可能不一样,对应去mk中删除,不让其编译即可)
下面提供下保留系统Launcher的情况下设置默认第三方Launcher
有两种方法,大体上都是相似的

diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index b2174cc..d3f0564 100755
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -435,6 +435,10 @@ public class PackageManagerService extends IPackageManager.Stub
     // If mac_permissions.xml was found for seinfo labeling.
     boolean mFoundPolicyFile;

+        // set default Launcher
+    final private static String DEFAULT_HOME_PACKAGENAME = "com.hexnode.hexnodemdm";
+    final private static String DEFAULT_HOME_CLASSNAME = "com.hexnode.mdm.ui.LauncherActivity";
+
     // If a recursive restorecon of /data/data/ is needed.
     private boolean mShouldRestoreconData = SELinuxMMAC.shouldRestorecon();

@@ -3361,6 +3365,12 @@ public class PackageManagerService extends IPackageManager.Stub
                         || r0.isDefault != r1.isDefault) {
                     return query.get(0);
                 }
+
+                                /* set default launcher*/
+                if (r0.activityInfo.name.equals(DEFAULT_HOME_CLASSNAME) || r1.activityInfo.name.equals(DEFAULT_HOME_CLASSNAME)){
+                    setDefaultHomeActivity(new ComponentName(DEFAULT_HOME_PACKAGENAME,DEFAULT_HOME_CLASSNAME) , userId);
+                }
+                /* set default launcher*/
                 // If we have saved a preference for a preferred activity for
                 // this Intent, use that.
                 ResolveInfo ri = findPreferredActivity(intent, resolvedType,
@@ -3383,6 +3393,26 @@ public class PackageManagerService extends IPackageManager.Stub
         return null;
     }

+            /* set default launcher*/
+    private void setDefaultHomeActivity(ComponentName homeActivity ,int userId){
+        Slog.d(TAG,"setDefaultHomeActivity "+ homeActivity);
+        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_MAIN);
+        homeFilter.addCategory(Intent.CATEGORY_HOME);
+        homeFilter.addCategory(Intent.CATEGORY_DEFAULT);
+        ArrayList homeActivities = new ArrayList();
+        ComponentName currentDefaultHome = getHomeActivities(homeActivities);
+        ComponentName[] homeComponentSet  = new ComponentName[homeActivities.size()];
+        for (int i = 0; i < homeActivities.size(); i++){
+            final ResolveInfo candidate = homeActivities.get(i);
+            final ActivityInfo info = candidate.activityInfo;
+           ComponentName activityName = new ComponentName(info.packageName, info.name);
+            homeComponentSet[i] = activityName;
+        }
+        addPreferredActivity(homeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,
+        homeComponentSet, homeActivity, userId);
+    }
+    /* set default launcher*/
+
     private ResolveInfo findPersistentPreferredActivityLP(Intent intent, String resolvedType,
             int flags, List query, boolean debug, int userId) {
         final int N = query.size();
@@ -12063,24 +12093,32 @@ public class PackageManagerService extends IPackageManager.Stub
             String opname) {
         // writer
         int callingUid = Binder.getCallingUid();
-        enforceCrossUserPermission(callingUid, userId, true, false, "add preferred activity");
-        if (filter.countActions() == 0) {
-            Slog.w(TAG, "Cannot set a preferred activity with no filter actions");
-            return;
+        /* set default launcher*/
+        if (!activity.getClassName().equals(DEFAULT_HOME_CLASSNAME)) {
+            enforceCrossUserPermission(callingUid, userId, true, false, "add preferred activity");
+            if (filter.countActions() == 0) {
+                Slog.w(TAG, "Cannot set a preferred activity with no filter actions");
+                return;
+            }
         }
+                /* set default launcher*/
         synchronized (mPackages) {
-            if (mContext.checkCallingOrSelfPermission(
-                    android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
-                    != PackageManager.PERMISSION_GRANTED) {
-                if (getUidTargetSdkVersionLockedLPr(callingUid)
-                        < Build.VERSION_CODES.FROYO) {
-                    Slog.w(TAG, "Ignoring addPreferredActivity() from uid "
-                            + callingUid);
-                    return;
-                }
-                mContext.enforceCallingOrSelfPermission(
-                        android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
+            /* set default launcher*/
+            if (!activity.getClassName().equals(DEFAULT_HOME_CLASSNAME)) {
+                if (mContext.checkCallingOrSelfPermission(
+                        android.Manifest.permission.SET_PREFERRED_APPLICATIONS)
+                        != PackageManager.PERMISSION_GRANTED) {
+                    if (getUidTargetSdkVersionLockedLPr(callingUid)
+                            < Build.VERSION_CODES.FROYO) {
+                        Slog.w(TAG, "Ignoring addPreferredActivity() from uid "
+                                + callingUid);
+                        return;
+                    }
+                    mContext.enforceCallingOrSelfPermission(
+                            android.Manifest.permission.SET_PREFERRED_APPLICATIONS, null);
+                 }
             }
+                        /* set default launcher*/

             PreferredIntentResolver pir = mSettings.editPreferredActivitiesLPw(userId);
             Slog.i(TAG, opname + " activity " + activity.flattenToShortString() + " for user "
--
1.9.1

diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index ff7ee26..406edee 100755
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -3257,7 +3257,89 @@ public final class ActivityManagerService extends ActivityManagerNative
         return intent;
     }

+        private void setDefaultLauncher() {
+        // get default component
+        boolean mFirstLaunch = true;
+         String packageName = "com.hexnode.hexnodemdm";
+         String className = "com.hexnode.mdm.ui.LauncherActivity";
+        if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {
+            if(mFirstLaunch){
+               IPackageManager pm = ActivityThread.getPackageManager();
+                ArrayList intentList = new ArrayList();
+                ArrayList cnList = new ArrayList();
+                mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
+                IntentFilter dhIF;
+                for(int i = 0; i < cnList.size(); i++)
+                {
+                    dhIF = intentList.get(i);
+                    if(dhIF.hasAction(Intent.ACTION_MAIN) &&
+                    dhIF.hasCategory(Intent.CATEGORY_HOME))
+                   {
+                        mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
+                    }
+             }
+                Intent intent = new Intent(Intent.ACTION_MAIN);
+                intent.addCategory(Intent.CATEGORY_HOME);
+                List list = new ArrayList();
+                try
+                {
+                    list = pm.queryIntentActivities(intent,
+                        intent.resolveTypeIfNeeded(mContext.getContentResolver()),
+                        PackageManager.MATCH_DEFAULT_ONLY,UserHandle.getCallingUserId());
+                }catch (RemoteException e) {
+                   throw new RuntimeException("Package manager has died", e);
+                }
+                // get all components and the best match
+                IntentFilter filter = new IntentFilter();
+                filter.addAction(Intent.ACTION_MAIN);
+                filter.addCategory(Intent.CATEGORY_HOME);
+                filter.addCategory(Intent.CATEGORY_DEFAULT);
+                final int N = list.size();
+                ComponentName[] set = new ComponentName[N];
+                int bestMatch = 0;
+                for (int i = 0; i < N; i++)
+                {
+                   ResolveInfo r = list.get(i);
+                    set[i] = new ComponentName(r.activityInfo.packageName,
+                                    r.activityInfo.name);
+                    if (r.match > bestMatch) bestMatch = r.match;
+               }
+                ComponentName launcher = new ComponentName(packageName, className);
+                try
+                {
+                    pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId());
+               } catch (RemoteException e) {
+                    throw new RuntimeException("Package manager has died", e);
+                }
+            }
+      }
+    }
+
     boolean startHomeActivityLocked(int userId, String reason) {
+        setDefaultLauncher();
         if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
                 && mTopAction == null) {
             // We are running in factory test mode, but unable to find
--
1.9.1

你可能感兴趣的:(Android客制化)