Step 17. LoaderTask.loadAndBindAllApps
这个函数定义在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
publicclassLauncherModelextendsBroadcastReceiver {
......
privateclassLoaderTaskimplementsRunnable {
......
privatevoidloadAndBindAllApps() {
......
if(!mAllAppsLoaded) {
loadAllAppsByBatch();
if(mStopped) {
return;
}
mAllAppsLoaded = true;
} else{
onlyBindAllApps();
}
}
......
}
......
}
由于还没有加载过应用程序,这里的mAllAppsLoaded为false,于是就继续调用loadAllAppsByBatch函数来进一步操作了。
Step 18. LoaderTask.loadAllAppsByBatch
这个函数定义在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
publicclassLauncherModelextendsBroadcastReceiver {
......
privateclassLoaderTaskimplementsRunnable {
......
privatevoidloadAllAppsByBatch() {
......
finalIntent mainIntent =newIntent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
finalPackageManager packageManager = mContext.getPackageManager();
List apps = null;
intN = Integer.MAX_VALUE;
intstartIndex;
inti=0;
intbatchSize = -1;
while(i
if(i ==0) {
mAllAppsList.clear();
......
apps = packageManager.queryIntentActivities(mainIntent, 0);
......
N = apps.size();
......
if(mBatchSize ==0) {
batchSize = N;
} else{
batchSize = mBatchSize;
}
......
Collections.sort(apps,
newResolveInfo.DisplayNameComparator(packageManager));
}
startIndex = i;
for(intj=0; i
// This builds the icon bitmaps.
mAllAppsList.add(newApplicationInfo(apps.get(i), mIconCache));
i++;
}
finalbooleanfirst = i <= batchSize;
finalCallbacks callbacks = tryGetCallbacks(oldCallbacks);
finalArrayList added = mAllAppsList.added;
mAllAppsList.added = newArrayList();
mHandler.post(newRunnable() {
publicvoidrun() {
finallongt = SystemClock.uptimeMillis();
if(callbacks !=null) {
if(first) {
callbacks.bindAllApplications(added);
} else{
callbacks.bindAppsAdded(added);
}
......
} else{
......
}
}
});
......
}
......
}
......
}
......
}
函数首先构造一个CATEGORY_LAUNCHER类型的Intent:
finalIntent mainIntent =newIntent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
接着从mContext变量中获得PackageManagerService的接口:
finalPackageManager packageManager = mContext.getPackageManager();
下一步就是通过这个PackageManagerService.queryIntentActivities接口来取回所有Action类型为Intent.ACTION_MAIN,并且Category类型为Intent.CATEGORY_LAUNCHER的Activity了。
我们先进入到PackageManagerService.queryIntentActivities函数中看看是如何获得这些Activity的,然后再回到这个函数中来看其余操作。