Android系统默认Home应用程序(Launcher)的启动过程源代码分析(5)

       Step 19. PackageManagerService.queryIntentActivities

       这个函数定义在frameworks/base/services/java/com/android/server/PackageManagerService.java文件中:

 
 
  1. class PackageManagerService extends IPackageManager.Stub {   
  2.     ......   
  3.    
  4.     public List<ResolveInfo> queryIntentActivities(Intent intent,   
  5.             String resolvedType, int flags) {   
  6.         ......   
  7.    
  8.         synchronized (mPackages) {   
  9.             String pkgName = intent.getPackage();   
  10.             if (pkgName == null) {   
  11.                 return (List<ResolveInfo>)mActivities.queryIntent(intent,   
  12.                         resolvedType, flags);   
  13.             }   
  14.    
  15.             ......   
  16.         }   
  17.    
  18.         ......   
  19.     }   
  20.    
  21.     ......   
  22. }   

     回忆前面一篇文章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.    
  5.     private AllAppsView mAllAppsGrid;   
  6.    
  7.     ......   
  8.    
  9.     public void bindAllApplications(ArrayList<ApplicationInfo> apps) {   
  10.         mAllAppsGrid.setApps(apps);   
  11.     }   
  12.    
  13.     ......   
  14. }   

这里的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.     ......   
  10.    
  11.     public void setApps(ArrayList<ApplicationInfo> list) {   
  12.         mAllAppsList.clear();   
  13.         addApps(list);   
  14.     }   
  15.    
  16.     public void addApps(ArrayList<ApplicationInfo> list) {   
  17.         final int N = list.size();   
  18.    
  19.         for (int i=0; i<N; i++) {   
  20.             final ApplicationInfo item = list.get(i);   
  21.             int index = Collections.binarySearch(mAllAppsList, item,   
  22.                 LauncherModel.APP_NAME_COMPARATOR);   
  23.             if (index < 0) {   
  24.                 index = -(index+1);   
  25.             }   
  26.             mAllAppsList.add(index, item);   
  27.         }   
  28.         mAppsAdapter.notifyDataSetChanged();   
  29.     }   
  30.    
  31.     ......   
  32. }   

    函数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.    
  5.     public void onClick(View v) {   
  6.         Object tag = v.getTag();   
  7.         if (tag instanceof ShortcutInfo) {   
  8.             ......   
  9.         } else if (tag instanceof FolderInfo) {   
  10.             ......   
  11.         } else if (v == mHandleView) {   
  12.             if (isAllAppsVisible()) {   
  13.                 ......   
  14.             } else {   
  15.                 showAllApps(true);   
  16.             }   
  17.         }   
  18.     }   
  19.    
  20.     ......   
  21. }   

   接着就会调用showAllApps函数显示应用程序图标:

 
 
  1. public final class Launcher extends Activity   
  2.         implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks, AllAppsView.Watcher {   
  3.     ......   
  4.    
  5.     void showAllApps(boolean animated) {   
  6.         mAllAppsGrid.zoom(1.0f, animated);   
  7.    
  8.         ((View) mAllAppsGrid).setFocusable(true);   
  9.         ((View) mAllAppsGrid).requestFocus();   
  10.    
  11.         // TODO: fade these two too   
  12.         mDeleteZone.setVisibility(View.GONE);   
  13.     }   
  14.    
  15.     ......   
  16. }   

这样我们就可以看到系统中的应用程序了:

    当点击上面的这些应用程序图标时,便会响应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.     ......   
  10.    
  11.     public void onItemClick(AdapterView parent, View v, int position, long id) {   
  12.         ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);   
  13.         mLauncher.startActivitySafely(app.intent, app);   
  14.     }   
  15.    
  16.    
  17.     ......   
  18. }<span style="font-family:Arial, Verdana, sans-serif;"><span style="white-space: normal;">   
  19. </span></span>   

 这里的成员变量mLauncher的类型为Launcher,于是就调用Launcher.startActivitySafely函数来启动应用程序了,这个过程具体可以参考Android应用程序启动过程源代码分析一文。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值