android 設定預設launcher
本部落格基於 Android 7.0,只作為溝通學習使用。
前言
平時使用android手機的時候我們可能會遇到下面的情況,比如:我們有多個瀏覽器,當我們沒有設定哪一個為預設的瀏覽器並點選了一行網址時,就會彈出一個系統選擇框,讓使用者選擇一個瀏覽來開啟這個網址,並且會讓使用者選擇只使用一次還是將你選擇的瀏覽器設為預設的瀏覽。那麼問題就來了,如果我不想出現這個提示框,我就想第一次開機時,就自動幫我設定好預設的瀏覽器可以嗎?答案是可以的。
對於瀏覽器、sms、郵箱等應用我們可以在 frameworks/base/core/res/res/values/config.xml 這個配置檔案裡面直接修改,類似於 <string name="default_browser" translatable="false">com.xxx.yyy.browser</string>
整體介紹
通過檢視launcher的啟動流程我們可以發現, SystemServer 呼叫 ActivityManagerService.systemReady 然後 systemReady 會呼叫 startHomeActivityLocked 去獲取當前系統存在的launcher應用,最後通過 ActivityStarter.startHomeActivityLocked 去啟動當前系統中launcher的主介面(如果有多個就會彈出對話方塊讓使用者選擇)。
主要程式碼如下:
獲取當前的launcher的資訊:
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;
}
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
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.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
在ActivityStarter裡面啟動launcher的主介面:
void startHomeActivityLocked(Intent intent, ActivityInfo aInfo, String reason) {
mSupervisor.moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
startActivityLocked(null /*caller*/, intent, null /*ephemeralIntent*/,
null /*resolvedType*/, aInfo, null /*rInfo*/, null /*voiceSession*/,
null /*voiceInteractor*/, null /*resultTo*/, null /*resultWho*/,
0 /*requestCode*/, 0 /*callingPid*/, 0 /*callingUid*/, null /*callingPackage*/,
0 /*realCallingPid*/, 0 /*realCallingUid*/, 0 /*startFlags*/, null /*options*/,
false /*ignoreTargetSecurity*/, false /*componentSpecified*/, null /*outActivity*/,
null /*container*/, null /*inTask*/);
if (mSupervisor.inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
mSupervisor.scheduleResumeTopActivities();
}
}
解決辦法
我們已經知道系統會在 startHomeActivityLocked 這個方法裡面去拿到當前系統的launcher 的資訊,然後會通過ActivityStarter去啟動介面,如果有多個就會顯示選擇框,如果有預設的那麼就啟動預設的launcher。所以,我們就解決辦法就是 在 startHomeActivityLocked 方法裡面就直接把某個launcher設定成預設的launcher,那麼當開機完成後進入launcher就不會顯示選擇框了,具體程式碼如下:
修改systemReady.startHomeActivityLocked 如下:
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;
}
final PackageManager mPm = mContext.getPackageManager();
Intent homeIntent=new Intent();
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setAction(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
ResolveInfo info = mPm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
if("android".equals(info.activityInfo.packageName)){
ComponentName DefaultLauncher=new ComponentName("com.xxx.xxx.launcher",
"com.xxx.xxx.launcher.activities.MainLauncherActivity");
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 candidate = homeActivities.get(i);
Log.d(TAG,"homeActivitie: candidate = "+candidate);
final ActivityInfo activityInfo= candidate.activityInfo;
ComponentName activityName = new ComponentName(activityInfo.packageName, activityInfo.name);
mHomeComponentSet[i] = activityName;
}
IntentFilter mHomeFilter = new IntentFilter(Intent.ACTION_MAIN);
mHomeFilter.addCategory(Intent.CATEGORY_HOME);
mHomeFilter.addCategory(Intent.CATEGORY_DEFAULT);
List<ComponentName>Activities=new ArrayList();
mPm.replacePreferredActivity(mHomeFilter, IntentFilter.MATCH_CATEGORY_EMPTY,mHomeComponentSet, DefaultLauncher);
}
Intent intent = getHomeIntent();
ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
if (aInfo != null) {
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.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mActivityStarter.startHomeActivityLocked(intent, aInfo, reason);
}
} else {
Slog.wtf(TAG, "No home screen found for " + intent, new Throwable());
}
return true;
}
其它
在MTK online上面搜到了同樣的問題,但是上面多了一步針對預置了GMS套件的修改方案,如下:
在 PackageManagerService.systemReady 的方法的末尾加上如下程式碼:
if(isFirstBoot()) { //如果是刷機後第一次開機就進入
String examplePackageName = "com.xxx.xxx.launcher";
String exampleActivityName = "com.xxx.xxx.launcher.activities.MainLauncherActivity";
Intent intent=new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final int callingUserId = UserHandle.getCallingUserId();
List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,null, PackageManager.GET_META_DATA,callingUserId).getList();
if(resolveInfoList != null){
int size = resolveInfoList.size();
for(int j=0;j<size;){
final ResolveInfo r = resolveInfoList.get(j);
if(!r.activityInfo.packageName.equals(examplePackageName)) {
resolveInfoList.remove(j);
size -= 1;
} else {
j++;
}
}
ComponentName[] set = new ComponentName[size];
ComponentName defaultLauncher=new ComponentName(examplePackageName, exampleActivityName);
int defaultMatch=0;
for(int i=0;i<size;i++){
final ResolveInfo resolveInfo = resolveInfoList.get(i);
Log.d(TAG,"resolveInfo = " + resolveInfo.toString());
set[i] = new ComponentName(resolveInfo.activityInfo.packageName,resolveInfo.activityInfo.name);
if(defaultLauncher.getClassName().equals(resolveInfo.activityInfo.name)){
defaultMatch = resolveInfo.match;
}
}
Log.d(TAG,"defaultMatch="+Integer.toHexString(defaultMatch));
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
addPreferredActivity2(filter, defaultMatch, set, defaultLauncher);
}
}
public void addPreferredActivity2(IntentFilter filter, int match,ComponentName[] set, ComponentName activity) {
synchronized (mPackages) {
filter.dump(new LogPrinter(Log.INFO, TAG), " ");
mSettings.editPreferredActivitiesLPw(0).addFilter(new PreferredActivity(filter, match, set, activity, true));
scheduleWriteSettingsLocked();
}
}
然後再修改 PackageManagerService.findPreferredActivity 中的部分程式碼如下:
/* if (removeMatches) {
pir.removeFilter(pa);
changed = true;
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing match " + pa.mPref.mComponent);
}
break;
}
// Okay we found a previously set preferred or last chosen app.
// If the result set is different from when this
// was created, we need to clear it and re-ask the
// user their preference, if we're looking for an "always" type entry.
if (always && !pa.mPref.sameSet(query)) {
Slog.i(TAG, "Result set changed, dropping preferred activity for "
+ intent + " type " + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing preferred activity since set changed "
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a "last chosen" entry (!always)
PreferredActivity lastChosen = new PreferredActivity(
pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
changed = true;
return null;
} */
if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null &&
intent.getCategories().contains(intent.CATEGORY_HOME))) {
Log.d(TAG,"Home");
}else {
if (removeMatches) {
pir.removeFilter(pa);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing match " + pa.mPref.mComponent);
}
break;
}
}
// Okay we found a previously set preferred or last chosen app.
// If the result set is different from when this
// was created, we need to clear it and re-ask the
// user their preference, if we're looking for an "always" type entry.
if (always && !pa.mPref.sameSet(query)) {
if(!(intent.getAction() != null && intent.getAction().equals(intent.ACTION_MAIN) && intent.getCategories()!=null &&
intent.getCategories().contains(intent.CATEGORY_HOME))) {
Slog.i(TAG, "Result set changed, dropping preferred activity for "
+ intent + " type " + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG, "Removing preferred activity since set changed "
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a "last chosen" entry (!always)
PreferredActivity lastChosen = new PreferredActivity(
pa, pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
mSettings.writePackageRestrictionsLPr(userId);
return null;
}
}
上面的做法可能是防止安裝GMS套件的時候Google Now Launcher(GoogleHome.apk)這個應用會自動設定為預設的,所以在PMS中,如果發現是刷機後第一次開機,那麼我們就會強制把預設的launcher設定成我們想要的哪一個apk。