在前面一篇文章中,咱們分析了Android系統在啓動時安裝應用程序的過程,這些應用程序安裝好以後,還需要有一個Home應用程序來負責把它們在桌面上展現出來,在Android系統中,這個默認的Home應用程序就是Launcher了,本文將詳細分析Launcher應用程序的啓動過程。html
Android系統的Home應用程序Launcher是由ActivityManagerService啓動的,而ActivityManagerService和PackageManagerService同樣,都是在開機時由SystemServer組件啓動的,SystemServer組件首先是啓動ePackageManagerServic,由它來負責安裝系統的應用程序,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析,系統中的應用程序安裝好了之後,SystemServer組件接下來就要經過ActivityManagerService來啓動Home應用程序Launcher了,Launcher在啓動的時候便會經過PackageManagerServic把系統中已經安裝好的應用程序以快捷圖標的形式展現在桌面上,這樣用戶就可以使用這些應用程序了,整個步驟例如如下圖所看到的:java
點擊查看大圖android
如下詳細分析每一個步驟。app
Step 1. SystemServer.main框架
這個函數定義在frameworks/base/services/java/com/android/server/SystemServer.java文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 1。ide
Step 2. SystemServer.init1函數
這個函數是一個JNI方法,實現在 frameworks/base/services/jni/com_android_server_SystemServer.cpp文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 2。源碼分析
Step 3. libsystem_server.system_initpost
函數system_init實現在libsystem_server庫中,源碼位於frameworks/base/cmds/system_server/library/system_init.cpp文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 3。ui
Step 4. AndroidRuntime.callStatic
這個函數定義在frameworks/base/core/jni/AndroidRuntime.cpp文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 4。
Step 5. SystemServer.init2
這個函數定義在frameworks/base/services/java/com/android/server/SystemServer.java文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 5。
Step 6. ServerThread.run
這個函數定義在frameworks/base/services/java/com/android/server/SystemServer.java文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 6。
Step 7. ActivityManagerService.main這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件裏:
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ...... public static final Context main(int factoryTest) { AThread thr = new AThread(); thr.start(); synchronized (thr) { while (thr.mService == null) { try { thr.wait(); } catch (InterruptedException e) { } } } ActivityManagerService m = thr.mService; mSelf = m; ActivityThread at = ActivityThread.systemMain(); mSystemThread = at; Context context = at.getSystemContext(); m.mContext = context; m.mFactoryTest = factoryTest; m.mMainStack = new ActivityStack(m, context, true); m.mBatteryStatsService.publish(context); m.mUsageStatsService.publish(context); synchronized (thr) { thr.mReady = true; thr.notifyAll(); } m.startRunning(null, null, null, null); return context; } ...... }這個函數首先經過AThread線程對象來內部建立了一個ActivityManagerService實例,而後將這個實例保存其成員變量mService中,接着又把這個ActivityManagerService實例保存在ActivityManagerService類的靜態成員變量mSelf中,最後初始化其餘成員變量,就結束了。
Step 8. PackageManagerService.main
這個函數定義在frameworks/base/services/java/com/android/server/PackageManagerService.java文件裏,詳細可以參考前面一篇文章Android應用程序安裝過程源碼分析的Step 7。運行完這一步以後,系統中的應用程序的所有信息都保存在PackageManagerService中了,後面Home應用程序Launcher啓動起來後,就會把PackageManagerService中的應用程序信息取出來,而後以快捷圖標的形式展現在桌面上,後面咱們將會看到這個過程。
Step 9. ActivityManagerService.setSystemProcess
這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件裏:
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ...... public static void setSystemProcess() { try { ActivityManagerService m = mSelf; ServiceManager.addService("activity", m); ServiceManager.addService("meminfo", new MemBinder(m)); if (MONITOR_CPU_USAGE) { ServiceManager.addService("cpuinfo", new CpuBinder(m)); } ServiceManager.addService("permission", new PermissionController(m)); ApplicationInfo info = mSelf.mContext.getPackageManager().getApplicationInfo( "android", STOCK_PM_FLAGS); mSystemThread.installSystemApplicationInfo(info); synchronized (mSelf) { ProcessRecord app = mSelf.newProcessRecordLocked( mSystemThread.getApplicationThread(), info, info.processName); app.persistent = true; app.pid = MY_PID; app.maxAdj = SYSTEM_ADJ; mSelf.mProcessNames.put(app.processName, app.info.uid, app); synchronized (mSelf.mPidsSelfLocked) { mSelf.mPidsSelfLocked.put(app.pid, app); } mSelf.updateLruProcessLocked(app, true, true); } } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException( "Unable to find android system package", e); } } ...... }這個函數首先是將這個ActivityManagerService實例加入到ServiceManager中去託管,這樣其餘地方就可以經過ServiceManager.getService接口來訪問這個全局惟一的ActivityManagerService實例了,接着又經過調用mSystemThread.installSystemApplicationInfo函數來把應用程序框架層如下的android包載入進來 ,這裏的mSystemThread是一個ActivityThread類型的實例變量,它是在上面的Step 7中建立的,後面就是一些其餘的初始化工做了。
Step 10. ActivityManagerService.systemReady
這個函數是在上面的Step 6中的ServerThread.run函數在將系統中的一系列服務都初始化完成以後才調用的,它定義在frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件裏:
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ...... public void systemReady(final Runnable goingCallback) { ...... synchronized (this) { ...... mMainStack.resumeTopActivityLocked(null); } } ...... }這個函數的內容比較多,這裏省去無關的部分,主要關心啓動Home應用程序的邏輯,這裏就是經過mMainStack.resumeTopActivityLocked函數來啓動Home應用程序的了,這裏的mMainStack是一個ActivityStack類型的實例變量。
Step 11. ActivityStack.resumeTopActivityLocked
這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件裏:
public class ActivityStack { ...... final boolean resumeTopActivityLocked(ActivityRecord prev) { // Find the first activity that is not finishing. ActivityRecord next = topRunningActivityLocked(null); ...... if (next == null) { // There are no more activities! Let's just start up the // Launcher... if (mMainStack) { return mService.startHomeActivityLocked(); } } ...... } ...... }這裏調用函數topRunningActivityLocked返回的是當前系統Activity堆棧最頂端的Activity,由於此時尚未Activity被啓動過,所以,返回值爲null,即next變量的值爲null,因而就調用mService.startHomeActivityLocked語句,這裏的mService就是前面在Step 7中建立的ActivityManagerService實例了。
Step 12. ActivityManagerService.startHomeActivityLocked
這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityManagerServcie.java文件裏:
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback { ...... boolean startHomeActivityLocked() { ...... Intent intent = new Intent( mTopAction, mTopData != null ? Uri.parse(mTopData) : null); intent.setComponent(mTopComponent); if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { intent.addCategory(Intent.CATEGORY_HOME); } ActivityInfo aInfo = intent.resolveActivityInfo(mContext.getPackageManager(), STOCK_PM_FLAGS); if (aInfo != null) { intent.setComponent(new ComponentName( aInfo.applicationInfo.packageName, aInfo.name)); // Don't do this if the home app is currently being // instrumented. ProcessRecord app = getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid); if (app == null || app.instrumentationClass == null) { intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo, null, null, 0, 0, 0, false, false); } } return true; } ...... }函數首先建立一個CATEGORY_HOME類型的Intent,而後經過Intent.resolveActivityInfo函數向PackageManagerService查詢Category類型爲HOME的Activity,這裏咱們若是僅僅有系統自帶的Launcher應用程序註冊了HOME類型的Activity(見packages/apps/Launcher2/AndroidManifest.xml文件):
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.launcher" android:sharedUserId="@string/sharedUserId" > ...... <application android:name="com.android.launcher2.LauncherApplication" android:process="@string/process" android:label="@string/application_name" android:icon="@drawable/ic_launcher_home"> <activity android:name="com.android.launcher2.Launcher" android:launchMode="singleTask" android:clearTaskOnLaunch="true" android:stateNotNeeded="true" android:theme="@style/Theme" android:screenOrientation="nosensor" android:windowSoftInputMode="stateUnspecified|adjustPan"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.MONKEY"/> </intent-filter> </activity> ...... </application> </manifest>
所以,這裏就返回com.android.launcher2.Launcher這個Activity了。由於是第一次啓動這個Activity,接下來調用函數getProcessRecordLocked返回來的ProcessRecord值爲null,因而,就調用mMainStack.startActivityLocked函數啓動com.android.launcher2.Launcher這個Activity了,這裏的mMainStack是一個ActivityStack類型的成員變量。
Step 13. ActivityStack.startActivityLocked
這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件裏,詳細可以參考Android應用程序啓動過程源碼分析一文,這裏就不詳述了,在咱們這個場景中,調用這個函數的最後結果就是把com.android.launcher2.Launcher啓動起來,接着調用它的onCreate函數。
Step 14. Launcher.onCreate
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/Launcher.java文件裏:
public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { ...... @Override protected void onCreate(Bundle savedInstanceState) { ...... if (!mRestoring) { mModel.startLoader(this, true); } ...... } ...... }這裏的mModel是一個LauncherModel類型的成員變量,這裏經過調用它的startLoader成員函數來運行加應用程序的操做。
Step 15. LauncherModel.startLoader
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件裏:
public class LauncherModel extends BroadcastReceiver { ...... public void startLoader(Context context, boolean isLaunching) { ...... synchronized (mLock) { ...... // Don't bother to start the thread if we know it's not going to do anything if (mCallbacks != null && mCallbacks.get() != null) { // If there is already one running, tell it to stop. LoaderTask oldTask = mLoaderTask; if (oldTask != null) { if (oldTask.isLaunching()) { // don't downgrade isLaunching if we're already running isLaunching = true; } oldTask.stopLocked(); } mLoaderTask = new LoaderTask(context, isLaunching); sWorker.post(mLoaderTask); } } } ...... }這裏不是直接載入應用程序,而是把載入應用程序的操做做爲一個消息來處理。這裏的sWorker是一個Handler,經過它的post方式把一個消息放在消息隊列中去,而後系統就會調用傳進去的參數mLoaderTask的run函數來處理這個消息,這個mLoaderTask是LoaderTask類型的實例,因而,如下就會運行LoaderTask類的run函數了。
Step 16. LoaderTask.run
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件裏:
public class LauncherModel extends BroadcastReceiver { ...... private class LoaderTask implements Runnable { ...... public void run() { ...... keep_running: { ...... // second step if (loadWorkspaceFirst) { ...... loadAndBindAllApps(); } else { ...... } ...... } ...... } ...... } ...... }這裏調用loadAndBindAllApps成員函數來進一步操做。
Step 17. LoaderTask.loadAndBindAllApps
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件裏:
public class LauncherModel extends BroadcastReceiver { ...... private class LoaderTask implements Runnable { ...... private void loadAndBindAllApps() { ...... 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文件裏:
public class LauncherModel extends BroadcastReceiver { ...... private class LoaderTask implements Runnable { ...... private void loadAllAppsByBatch() { ...... final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); final PackageManager packageManager = mContext.getPackageManager(); List<ResolveInfo> apps = null; int N = Integer.MAX_VALUE; int startIndex; int i=0; int batchSize = -1; while (i < N && !mStopped) { if (i == 0) { mAllAppsList.clear(); ...... apps = packageManager.queryIntentActivities(mainIntent, 0); ...... N = apps.size(); ...... if (mBatchSize == 0) { batchSize = N; } else { batchSize = mBatchSize; } ...... Collections.sort(apps, new ResolveInfo.DisplayNameComparator(packageManager)); } startIndex = i; for (int j=0; i<N && j<batchSize; j++) { // This builds the icon bitmaps. mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache)); i++; } final boolean first = i <= batchSize; final Callbacks callbacks = tryGetCallbacks(oldCallbacks); final ArrayList<ApplicationInfo> added = mAllAppsList.added; mAllAppsList.added = new ArrayList<ApplicationInfo>(); mHandler.post(new Runnable() { public void run() { final long t = SystemClock.uptimeMillis(); if (callbacks != null) { if (first) { callbacks.bindAllApplications(added); } else { callbacks.bindAppsAdded(added); } ...... } else { ...... } } }); ...... } ...... } ...... } ...... }函數首先構造一個CATEGORY_LAUNCHER類型的Intent:
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);接着從mContext變量中得到PackageManagerService的接口:
final PackageManager packageManager = mContext.getPackageManager();
下一步就是經過這個PackageManagerService.queryIntentActivities接口來取回所有Action類型爲Intent.ACTION_MAIN,並且Category類型爲Intent.CATEGORY_LAUNCHER的Activity了。
咱們先進入到PackageManagerService.queryIntentActivities函數中看看是怎樣得到這些Activity的,而後再回到這個函數中來看其他操做。
Step 19. PackageManagerService.queryIntentActivities
這個函數定義在frameworks/base/services/java/com/android/server/PackageManagerService.java文件裏:
class PackageManagerService extends IPackageManager.Stub { ...... public List<ResolveInfo> queryIntentActivities(Intent intent, String resolvedType, int flags) { ...... synchronized (mPackages) { String pkgName = intent.getPackage(); if (pkgName == null) { return (List<ResolveInfo>)mActivities.queryIntent(intent, resolvedType, flags); } ...... } ...... } ...... }
回顧前面一篇文章Android應用程序安裝過程源碼分析,系統在前面的Step 8中啓動PackageManagerService時,會把系統中的應用程序都解析一遍,而後把解析獲得的Activity都保存在mActivities變量中,這裏經過這個mActivities變量的queryIntent函數返回符合條件intent的Activity,這裏要返回的即是Action類型爲Intent.ACTION_MAIN,並且Category類型爲Intent.CATEGORY_LAUNCHER的Activity了。
回到Step 18中的 LoaderTask.loadAllAppsByBatch函數中,從queryIntentActivities函數調用處返回所要求的Activity後,便調用函數tryGetCallbacks(oldCallbacks)獲得一個返CallBack接口,這個接口是由Launcher類實現的,接着調用這個接口的.bindAllApplications函數來進一步操做。注意,這裏又是經過消息來處理載入應用程序的操做的。
Step 20. Launcher.bindAllApplications
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/Launcher.java文件裏:
public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { ...... private AllAppsView mAllAppsGrid; ...... public void bindAllApplications(ArrayList<ApplicationInfo> apps) { mAllAppsGrid.setApps(apps); } ...... }這裏的mAllAppsGrid是一個AllAppsView類型的變量,它的實際類型通常就是AllApps2D了。
Step 21. AllApps2D.setApps
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/AllApps2D.java文件裏:
public class AllApps2D extends RelativeLayout implements AllAppsView, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnKeyListener, DragSource { ...... public void setApps(ArrayList<ApplicationInfo> list) { mAllAppsList.clear(); addApps(list); } public void addApps(ArrayList<ApplicationInfo> list) { final int N = list.size(); for (int i=0; i<N; i++) { final ApplicationInfo item = list.get(i); int index = Collections.binarySearch(mAllAppsList, item, LauncherModel.APP_NAME_COMPARATOR); if (index < 0) { index = -(index+1); } mAllAppsList.add(index, item); } mAppsAdapter.notifyDataSetChanged(); } ...... }函數setApps首先清空mAllAppsList列表,而後調用addApps函數來爲上一步獲得的每一個應用程序建立一個ApplicationInfo實例了,有了這些ApplicationInfo實例以後,就可以在桌面上展現系統中所有的應用程序了。
到了這裏,系統默認的Home應用程序Launcher就把PackageManagerService中的應用程序載入進來了,當咱們在屏幕上點擊如下這個圖標時,就會把剛纔載入好的應用程序以圖標的形式展現出來了:
點擊這個button時,便會響應Launcher.onClick函數:
public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { ...... public void onClick(View v) { Object tag = v.getTag(); if (tag instanceof ShortcutInfo) { ...... } else if (tag instanceof FolderInfo) { ...... } else if (v == mHandleView) { if (isAllAppsVisible()) { ...... } else { showAllApps(true); } } } ...... }接着就會調用showAllApps函數顯示應用程序圖標:
public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher { ...... void showAllApps(boolean animated) { mAllAppsGrid.zoom(1.0f, animated); ((View) mAllAppsGrid).setFocusable(true); ((View) mAllAppsGrid).requestFocus(); // TODO: fade these two too mDeleteZone.setVisibility(View.GONE); } ...... }這樣咱們就可以看到系統中的應用程序了:
當點擊上面的這些應用程序圖標時,便會響應AllApps2D.onItemClick函數:
public class AllApps2D
extends RelativeLayout
implements AllAppsView,
AdapterView.OnItemClickListener,
AdapterView.OnItemLongClickListener,
View.OnKeyListener,
DragSource {
......
public void onItemClick(AdapterView parent, View v, int position, long id) {
ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
mLauncher.startActivitySafely(app.intent, app);
}
......
}
這裏的成員變量mLauncher的類型爲Launcher,因而就調用Launcher.startActivitySafely函數來啓動應用程序了,這個過程詳細可以參考Android應用程序啓動過程源碼分析一文。
老羅的新浪微博:http://weibo.com/shengyangluo,歡迎關注!