Android系統默認Home應用程序(Launcher)的啓動過程源代碼分析

在前面一篇文章中,我們分析了Android系統在啓動時安裝應用程序的過程,這些應用程序安裝好之後,還需要有一個Home應用程序來負責把它們在桌面上展示出來,在Android系統中,這個默認的Home應用程序就是Launcher了,本文將詳細分析Launcher應用程序的啓動過程。

Android系統的Home應用程序Launcher是由ActivityManagerService啓動的,而ActivityManagerService和PackageManagerService一樣,都是在開機時由SystemServer組件啓動的,SystemServer組件首先是啓動ePackageManagerServic,由它來負責安裝系統的應用程序,具體可以參考前面一篇文章Android應用程序安裝過程源代碼分析,系統中的應用程序安裝好了以後,SystemServer組件接下來就要通過ActivityManagerService來啓動Home應用程序Launcher了,Launcher在啓動的時候便會通過PackageManagerServic把系統中已經安裝好的應用程序以快捷圖標的形式展示在桌面上,這樣用戶就可以使用這些應用程序了,整個過程如下圖所示:


 

點擊查看大圖

下面詳細分析每一個步驟。

Step 1. SystemServer.main

這個函數定義在frameworks/base/services/java/com/android/server/SystemServer.java文件中,具體可以參考前面一篇文章Android應用程序安裝過程源代碼分析的Step 1。

Step 2. SystemServer.init1

這個函數是一個JNI方法,實現在 frameworks/base/services/jni/com_android_server_SystemServer.cpp文件中,具體可以參考前面一篇文章Android應用程序安裝過程源代碼分析的Step 2。

 

Step 3. libsystem_server.system_init

函數system_init實現在libsystem_server庫中,源代碼位於frameworks/base/cmds/system_server/library/system_init.cpp文件中,具體可以參考前面一篇文章Android應用程序安裝過程源代碼分析的Step 3。

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文件中:

 
  1. public final class ActivityManagerService extends ActivityManagerNative
  2. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
  3. ......
  4. public static final Context main(int factoryTest) {
  5. AThread thr = new AThread();
  6. thr.start();
  7. synchronized (thr) {
  8. while (thr.mService == null) {
  9. try {
  10. thr.wait();
  11. } catch (InterruptedException e) {
  12. }
  13. }
  14. }
  15. ActivityManagerService m = thr.mService;
  16. mmSelf = m;
  17. ActivityThread at = ActivityThread.systemMain();
  18. mSystemThread = at;
  19. Context context = at.getSystemContext();
  20. m.mContext = context;
  21. m.mFactoryTest = factoryTest;
  22. m.mMainStack = new ActivityStack(m, context, true);
  23. m.mBatteryStatsService.publish(context);
  24. m.mUsageStatsService.publish(context);
  25. synchronized (thr) {
  26. thr.mReady = true;
  27. thr.notifyAll();
  28. }
  29. m.startRunning(null, null, null, null);
  30. return context;
  31. }
  32. ......
  33. }

這個函數首先通過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文件中:

 

  1. public final class ActivityManagerService extends ActivityManagerNative
  2. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
  3. ......
  4. public static void setSystemProcess() {
  5. try {
  6. ActivityManagerService m = mSelf;
  7. ServiceManager.addService("activity", m);
  8. ServiceManager.addService("meminfo", new MemBinder(m));
  9. if (MONITOR_CPU_USAGE) {
  10. ServiceManager.addService("cpuinfo", new CpuBinder(m));
  11. }
  12. ServiceManager.addService("permission", new PermissionController(m));
  13. ApplicationInfo info =
  14. mSelf.mContext.getPackageManager().getApplicationInfo(
  15. "android", STOCK_PM_FLAGS);
  16. mSystemThread.installSystemApplicationInfo(info);
  17. synchronized (mSelf) {
  18. Proce***ecord app = mSelf.newProce***ecordLocked(
  19. mSystemThread.getApplicationThread(), info,
  20. info.processName);
  21. app.persistent = true;
  22. app.pid = MY_PID;
  23. app.maxAdj = SYSTEM_ADJ;
  24. mSelf.mProcessNames.put(app.processName, app.info.uid, app);
  25. synchronized (mSelf.mPidsSelfLocked) {
  26. mSelf.mPidsSelfLocked.put(app.pid, app);
  27. }
  28. mSelf.updateLruProcessLocked(app, true, true);
  29. }
  30. } catch (PackageManager.NameNotFoundException e) {
  31. throw new RuntimeException(
  32. "Unable to find android system package", e);
  33. }
  34. }
  35. ......
  36. }
  37. public final class ActivityManagerService extends ActivityManagerNative
  38. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
  39. ......
  40. public static void setSystemProcess() {
  41. try {
  42. ActivityManagerService m = mSelf;
  43. ServiceManager.addService("activity", m);
  44. ServiceManager.addService("meminfo", new MemBinder(m));
  45. if (MONITOR_CPU_USAGE) {
  46. ServiceManager.addService("cpuinfo", new CpuBinder(m));
  47. }
  48. ServiceManager.addService("permission", new PermissionController(m));
  49. ApplicationInfo info =
  50. mSelf.mContext.getPackageManager().getApplicationInfo(
  51. "android", STOCK_PM_FLAGS);
  52. mSystemThread.installSystemApplicationInfo(info);
  53. synchronized (mSelf) {
  54. Proce***ecord app = mSelf.newProce***ecordLocked(
  55. mSystemThread.getApplicationThread(), info,
  56. info.processName);
  57. app.persistent = true;
  58. app.pid = MY_PID;
  59. app.maxAdj = SYSTEM_ADJ;
  60. mSelf.mProcessNames.put(app.processName, app.info.uid, app);
  61. synchronized (mSelf.mPidsSelfLocked) {
  62. mSelf.mPidsSelfLocked.put(app.pid, app);
  63. }
  64. mSelf.updateLruProcessLocked(app, true, true);
  65. }
  66. } catch (PackageManager.NameNotFoundException e) {
  67. throw new RuntimeException(
  68. "Unable to find android system package", e);
  69. }
  70. }
  71. ......
  72. }

這個函數首先是將這個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文件中:

  1. public final class ActivityManagerService extends ActivityManagerNative
  2. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
  3. ......
  4.  
  5. public void systemReady(final Runnable goingCallback) {
  6. ......
  7.  
  8. synchronized (this) {
  9. ......
  10.  
  11. mMainStack.resumeTopActivityLocked(null);
  12. }
  13. }
  14.  
  15. ......
  16. }

這個函數的內容比較多,這裏省去無關的部分,主要關心啓動Home應用程序的邏輯,這裏就是通過mMainStack.resumeTopActivityLocked函數來啓動Home應用程序的了,這裏的mMainStack是一個ActivityStack類型的實例變量。

 

Step 11. ActivityStack.resumeTopActivityLocked

這個函數定義在frameworks/base/services/java/com/android/server/am/ActivityStack.java文件中:

 

  1. public class ActivityStack {
  2. ......
  3. final boolean resumeTopActivityLocked(ActivityRecord prev) {
  4. // Find the first activity that is not finishing.
  5. ActivityRecord next = topRunningActivityLocked(null);
  6. ......
  7. if (next == null) {
  8. // There are no more activities! Let's just start up the
  9. // Launcher...
  10. if (mMainStack) {
  11. return mService.startHomeActivityLocked();
  12. }
  13. }
  14. ......
  15. }
  16. ......
  17. }

這裏調用函數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文件中:

 
  1. public final class ActivityManagerService extends ActivityManagerNative
  2. implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
  3. ......
  4. boolean startHomeActivityLocked() {
  5. ......
  6. Intent intent = new Intent(
  7. mTopAction,
  8. mTopData != null ? Uri.parse(mTopData) : null);
  9. intent.setComponent(mTopComponent);
  10. if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
  11. intent.addCategory(Intent.CATEGORY_HOME);
  12. }
  13. ActivityInfo aInfo =
  14. intent.resolveActivityInfo(mContext.getPackageManager(),
  15. STOCK_PM_FLAGS);
  16. if (aInfo != null) {
  17. intent.setComponent(new ComponentName(
  18. aInfo.applicationInfo.packageName, aInfo.name));
  19. // Don't do this if the home app is currently being
  20. // instrumented.
  21. Proce***ecord app = getProce***ecordLocked(aInfo.processName,
  22. aInfo.applicationInfo.uid);
  23. if (app == null || app.instrumentationClass == null) {
  24. intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
  25. mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,
  26. null, null, 0, 0, 0, false, false);
  27. }
  28. }
  29. return true;
  30. }
  31. ......

函數首先創建一個CATEGORY_HOME類型的Intent,然後通過Intent.resolveActivityInfo函數向PackageManagerService查詢Category類型爲HOME的Activity,這裏我們假設只有系統自帶的Launcher應用程序註冊了HOME類型的Activity(見packages/apps/Launcher2/AndroidManifest.xml文件):

 

  1. <manifest
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.launcher"
  4. android:sharedUserId="@string/sharedUserId"
  5. >
  6. ......
  7. <application
  8. android:name="com.android.launcher2.LauncherApplication"
  9. android:process="@string/process"
  10. android:label="@string/application_name"
  11. android:icon="@drawable/ic_launcher_home">
  12. <activity
  13. android:name="com.android.launcher2.Launcher"
  14. android:launchMode="singleTask"
  15. android:clearTaskOnLaunch="true"
  16. android:stateNotNeeded="true"
  17. android:theme="@style/Theme"
  18. android:screenOrientation="nosensor"
  19. android:windowSoftInputMode="stateUnspecified|adjustPan">
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22. <category android:name="android.intent.category.HOME" />
  23. <category android:name="android.intent.category.DEFAULT" />
  24. <category android:name="android.intent.category.MONKEY"/>
  25. </intent-filter>
  26. </activity>
  27. ......
  28. </application>
  29. </manifest>
  30. <manifest
  31. xmlns:android="http://schemas.android.com/apk/res/android"
  32. package="com.android.launcher"
  33. android:sharedUserId="@string/sharedUserId"
  34. >
  35. ......
  36. <application
  37. android:name="com.android.launcher2.LauncherApplication"
  38. android:process="@string/process"
  39. android:label="@string/application_name"
  40. android:icon="@drawable/ic_launcher_home">
  41. <activity
  42. android:name="com.android.launcher2.Launcher"
  43. android:launchMode="singleTask"
  44. android:clearTaskOnLaunch="true"
  45. android:stateNotNeeded="true"
  46. android:theme="@style/Theme"
  47. android:screenOrientation="nosensor"
  48. android:windowSoftInputMode="stateUnspecified|adjustPan">
  49. <intent-filter>
  50. <action android:name="android.intent.action.MAIN" />
  51. <category android:name="android.intent.category.HOME" />
  52. <category android:name="android.intent.category.DEFAULT" />
  53. <category android:name="android.intent.category.MONKEY"/>
  54. </intent-filter>
  55. </activity>
  56. ......
  57. </application>
  58. </manifest>

 

因此,這裏就返回com.android.launcher2.Launcher這個Activity了。由於是第一次啓動這個Activity,接下來調用函數getProce***ecordLocked返回來的Proce***ecord值爲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文件中:

  1. public final class Launcher extends Activity
  2. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  3. ......
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. ......
  8.  
  9. if (!mRestoring) {
  10. mModel.startLoader(this, true);
  11. }
  12.  
  13. ......
  14. }
  15.  
  16. ......
  17. }

這裏的mModel是一個LauncherModel類型的成員變量,這裏通過調用它的startLoader成員函數來執行加應用程序的操作。

 

Step 15. LauncherModel.startLoader

這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:

 

  1. public class LauncherModel extends BroadcastReceiver {
  2. ......
  3. public void startLoader(Context context, boolean isLaunching) {
  4. ......
  5. synchronized (mLock) {
  6. ......
  7. // Don't bother to start the thread if we know it's not going to do anything
  8. if (mCallbacks != null && mCallbacks.get() != null) {
  9. // If there is already one running, tell it to stop.
  10. LoaderTask oldTask = mLoaderTask;
  11. if (oldTask != null) {
  12. if (oldTask.isLaunching()) {
  13. // don't downgrade isLaunching if we're already running
  14. isLaunching = true;
  15. }
  16. oldTask.stopLocked();
  17. }
  18. mLoaderTask = new LoaderTask(context, isLaunching);
  19. sWorker.post(mLoaderTask);
  20. }
  21. }
  22. }
  23. ......
  24. }

這裏不是直接加載應用程序,而是把加載應用程序的操作作爲一個消息來處理。這裏的sWorker是一個Handler,通過它的post方式把一個消息放在消息隊列中去,然後系統就會調用傳進去的參數mLoaderTask的run函數來處理這個消息,這個mLoaderTask是LoaderTask類型的實例,於是,下面就會執行LoaderTask類的run函數了。

 

Step 16. LoaderTask.run

這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:

 

  1. public class LauncherModel extends BroadcastReceiver {
  2. ......
  3. private class LoaderTask implements Runnable {
  4. ......
  5. public void run() {
  6. ......
  7. keep_running: {
  8. ......
  9. // second step
  10. if (loadWorkspaceFirst) {
  11. ......
  12. loadAndBindAllApps();
  13. } else {
  14. ......
  15. }
  16. ......
  17. }
  18. ......
  19. }
  20. ......
  21. }
  22. ......
  23. }

這裏調用loadAndBindAllApps成員函數來進一步操作。

 

Step 17. LoaderTask.loadAndBindAllApps
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
 

 

  1. public class LauncherModel extends BroadcastReceiver {
  2. ......
  3. private class LoaderTask implements Runnable {
  4. ......
  5. private void loadAndBindAllApps() {
  6. ......
  7. if (!mAllAppsLoaded) {
  8. loadAllAppsByBatch();
  9. if (mStopped) {
  10. return;
  11. }
  12. mAllAppsLoaded = true;
  13. } else {
  14. onlyBindAllApps();
  15. }
  16. }
  17. ......
  18. }
  19. ......
  20. }

由於還沒有加載過應用程序,這裏的mAllAppsLoaded爲false,於是就繼續調用loadAllAppsByBatch函數來進一步操作了。

 

Step 18. LoaderTask.loadAllAppsByBatch
這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/LauncherModel.java文件中:
 

 

  1. public class LauncherModel extends BroadcastReceiver {
  2. ......
  3. private class LoaderTask implements Runnable {
  4. ......
  5. private void loadAllAppsByBatch() {
  6. ......
  7. final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  8. mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  9. final PackageManager packageManager = mContext.getPackageManager();
  10. List<ResolveInfo> apps = null;
  11. int N = Integer.MAX_VALUE;
  12. int startIndex;
  13. int i=0;
  14. int batchSize = -1;
  15. while (i < N && !mStopped) {
  16. if (i == 0) {
  17. mAllAppsList.clear();
  18. ......
  19. apps = packageManager.queryIntentActivities(mainIntent, 0);
  20. ......
  21. N = apps.size();
  22. ......
  23. if (mBatchSize == 0) {
  24. batchSize = N;
  25. } else {
  26. batchSize = mBatchSize;
  27. }
  28. ......
  29. Collections.sort(apps,
  30. new ResolveInfo.DisplayNameComparator(packageManager));
  31. }
  32. startIndex = i;
  33. for (int j=0; i<N && j<batchSize; j++) {
  34. // This builds the icon bitmaps.
  35. mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache));
  36. i++;
  37. }
  38. final boolean first = i <= batchSize;
  39. final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
  40. final ArrayList<ApplicationInfo> added = mAllAppsList.added;
  41. mAllAppsList.added = new ArrayList<ApplicationInfo>();
  42. mHandler.post(new Runnable() {
  43. public void run() {
  44. final long t = SystemClock.uptimeMillis();
  45. if (callbacks != null) {
  46. if (first) {
  47. callbacks.bindAllApplications(added);
  48. } else {
  49. callbacks.bindAppsAdded(added);
  50. }
  51. ......
  52. } else {
  53. ......
  54. }
  55. }
  56. });
  57. ......
  58. }
  59. ......
  60. }
  61. ......
  62. }
  63. ......
  64. }

函數首先構造一個CATEGORY_LAUNCHER類型的Intent:
 

 

 

  1. final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
  2. mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

接着從mContext變量中獲得PackageManagerService的接口

 

  1. final PackageManager packageManager = mContext.getPackageManager();
  2. final PackageManager packageManager = mContext.getPackageManager();
  3. 下一步就是通過這個PackageManagerService.queryIntentActivities接口來取回所有Action類型爲Intent.ACTION_MAIN,並且Category類型爲Intent.CATEGORY_LAUNCHER的Activity了。
  4. 我們先進入到PackageManagerService.queryIntentActivities函數中看看是如何獲得這些Activity的,然後再回到這個函數中來看其餘操作。
  5. Step 19. PackageManagerService.queryIntentActivities
  6. 這個函數定義在frameworks/base/services/java/com/android/server/PackageManagerService.java文件中:
  7. view plaincopy to clipboardprint?
  8. class PackageManagerService extends IPackageManager.Stub {
  9. ......
  10. public List<ResolveInfo> queryIntentActivities(Intent intent,
  11. String resolvedType, int flags) {
  12. ......
  13. synchronized (mPackages) {
  14. String pkgName = intent.getPackage();
  15. if (pkgName == null) {
  16. return (List<ResolveInfo>)mActivities.queryIntent(intent,
  17. resolvedType, flags);
  18. }
  19. ......
  20. }
  21. ......
  22. }
  23. ......
  24. }
  25. class PackageManagerService extends IPackageManager.Stub {
  26. ......
  27. public List<ResolveInfo> queryIntentActivities(Intent intent,
  28. String resolvedType, int flags) {
  29. ......
  30. synchronized (mPackages) {
  31. String pkgName = intent.getPackage();
  32. if (pkgName == null) {
  33. return (List<ResolveInfo>)mActivities.queryIntent(intent,
  34. resolvedType, flags);
  35. }
  36. ......
  37. }
  38. ......
  39. }
  40. ......
  41. }

回憶前面一篇文章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文件中:

 

 

  1. public final class Launcher extends Activity
  2. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  3. ......
  4. private AllAppsView mAllAppsGrid;
  5. ......
  6. public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
  7. mAllAppsGrid.setApps(apps);
  8. }
  9. ......
  10. }
  11. public final class Launcher extends Activity
  12. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  13. ......
  14. private AllAppsView mAllAppsGrid;
  15. ......
  16. public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
  17. mAllAppsGrid.setApps(apps);
  18. }
  19. ......
  20. }

這裏的mAllAppsGrid是一個AllAppsView類型的變量,它的實際類型一般就是AllApps2D了。

 

Step 21. AllApps2D.setApps

這個函數定義在packages/apps/Launcher2/src/com/android/launcher2/AllApps2D.java文件中:

 

  1. public class AllApps2D
  2. extends RelativeLayout
  3. implements AllAppsView,
  4. AdapterView.OnItemClickListener,
  5. AdapterView.OnItemLongClickListener,
  6. View.OnKeyListener,
  7. DragSource {
  8. ......
  9. public void setApps(ArrayList<ApplicationInfo> list) {
  10. mAllAppsList.clear();
  11. addApps(list);
  12. }
  13. public void addApps(ArrayList<ApplicationInfo> list) {
  14. final int N = list.size();
  15. for (int i=0; i<N; i++) {
  16. final ApplicationInfo item = list.get(i);
  17. int index = Collections.binarySearch(mAllAppsList, item,
  18. LauncherModel.APP_NAME_COMPARATOR);
  19. if (index < 0) {
  20. index = -(index+1);
  21. }
  22. mAllAppsList.add(index, item);
  23. }
  24. mAppsAdapter.notifyDataSetChanged();
  25. }
  26. ......
  27. }

函數setApps首先清空mAllAppsList列表,然後調用addApps函數來爲上一步得到的每一個應用程序創建一個ApplicationInfo實例了,有了這些ApplicationInfo實例之後,就可以在桌面上展示系統中所有的應用程序了。

 

到了這裏,系統默認的Home應用程序Launcher就把PackageManagerService中的應用程序加載進來了,當我們在屏幕上點擊下面這個圖標時,就會把剛纔加載好的應用程序以圖標的形式展示出來了:

點擊這個按鈕時,便會響應Launcher.onClick函數:

 

 

  1. public final class Launcher extends Activity
  2. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  3. ......
  4. public void onClick(View v) {
  5. Object tag = v.getTag();
  6. if (tag instanceof ShortcutInfo) {
  7. ......
  8. } else if (tag instanceof FolderInfo) {
  9. ......
  10. } else if (v == mHandleView) {
  11. if (isAllAppsVisible()) {
  12. ......
  13. } else {
  14. showAllApps(true);
  15. }
  16. }
  17. }
  18. ......
  19. }
  20. public final class Launcher extends Activity
  21. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  22. ......
  23. public void onClick(View v) {
  24. Object tag = v.getTag();
  25. if (tag instanceof ShortcutInfo) {
  26. ......
  27. } else if (tag instanceof FolderInfo) {
  28. ......
  29. } else if (v == mHandleView) {
  30. if (isAllAppsVisible()) {
  31. ......
  32. } else {
  33. showAllApps(true);
  34. }
  35. }
  36. }
  37. ......
  38. } 接着就會調用showAllApps函數顯示應用程序圖標:
  39. view plaincopy to clipboardprint?
  40. public final class Launcher extends Activity
  41. implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {
  42. ......
  43. void showAllApps(boolean animated) {
  44. mAllAppsGrid.zoom(1.0f, animated);
  45. ((View) mAllAppsGrid).setFocusable(true);
  46. ((View) mAllAppsGrid).requestFocus();
  47. // TODO: fade these two too
  48. mDeleteZone.setVisibility(View.GONE);
  49. }
  50. ......
  51. }

這樣我們就可以看到系統中的應用程序了:

 



當點擊上面的這些應用程序圖標時,便會響應AllApps2D.onItemClick函數:

 

  1. public class AllApps2D
  2. extends RelativeLayout
  3. implements AllAppsView,
  4. AdapterView.OnItemClickListener,
  5. AdapterView.OnItemLongClickListener,
  6. View.OnKeyListener,
  7. DragSource {
  8. ......
  9. public void onItemClick(AdapterView parent, View v, int position, long id) {
  10. ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
  11. mLauncher.startActivitySafely(app.intent, app);
  12. }
  13. ......
  14. }
  15. public class AllApps2D
  16. extends RelativeLayout
  17. implements AllAppsView,
  18. AdapterView.OnItemClickListener,
  19. AdapterView.OnItemLongClickListener,
  20. View.OnKeyListener,
  21. DragSource {
  22. ......
  23. public void onItemClick(AdapterView parent, View v, int position, long id) {
  24. ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
  25. mLauncher.startActivitySafely(app.intent, app);
  26. }
  27. ......
  28. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章