本文对Android O(8.0) Car Launcher进行全面解析。
Car Launcher的界面
下图为Car Launcher的界面,
跟普通的Launcher不同,Car Launcher没有应用抽屉,也没有左右滑动的应用页面;导航按钮也不是home,back,recent按钮,而是不同类型的应用按钮。下面先来看看导航按钮是如何构造的。
Car status bar / navigation bar 构造流程
下图为Navigation bar的构造流程,它是 SystemUI 的一部分。
Car status bar / navigation bar 构造是在 SystemUI 模块完成的,最终的执行是在 CarNavigationBarController 类的bind方法中,代码如下:
139 /**
140 * Iterates through the items in arrays_car.xml and sets up the facet bar buttons to
141 * perform the task in that configuration file when clicked or long-pressed.
142 */
143 private void bind() {
144 Resources res = mContext.getResources();
145
146 TypedArray icons = res.obtainTypedArray(R.array.car_facet_icons);
147 TypedArray intents = res.obtainTypedArray(R.array.car_facet_intent_uris);
148 TypedArray longPressIntents = res.obtainTypedArray(R.array.car_facet_longpress_intent_uris);
149 TypedArray facetPackageNames = res.obtainTypedArray(R.array.car_facet_package_filters);
150 TypedArray facetCategories = res.obtainTypedArray(R.array.car_facet_category_filters);
151
152 try {
153 if (icons.length() != intents.length()
154 || icons.length() != longPressIntents.length()
155 || icons.length() != facetPackageNames.length()
156 || icons.length() != facetCategories.length()) {
157 throw new RuntimeException("car_facet array lengths do not match");
158 }
159
160 for (int i = 0, size = icons.length(); i < size; i++) {
161 Drawable icon = icons.getDrawable(i);
162 CarNavigationButton button = createNavButton(icon);
163 initClickListeners(button, i, intents.getString(i), longPressIntents.getString(i));
164
165 mNavButtons.add(button);
166 mNavBar.addButton(button, createNavButton(icon) /* lightsOutButton */);
167
168 initFacetFilterMaps(i, facetPackageNames.getString(i).split(FACET_FILTER_DELIMITER),
169 facetCategories.getString(i).split(FACET_FILTER_DELIMITER));
170 mFacetHasMultipleAppsCache.put(i, facetHasMultiplePackages(i));
171 }
172 } finally {
173 // Clean up all the TypedArrays.
174 icons.recycle();
175 intents.recycle();
176 longPressIntents.recycle();
177 facetPackageNames.recycle();
178 facetCategories.recycle();
179 }
180 }
该方法遍历 arrays_car.xml文件的内容来构建导航栏按钮。我们来看 arrays_car.xml的定义,路径是packages/services/Car/car_product/overlay/frameworks/base/packages/SystemUI/
res/values/arrays_car.xml:
20 <resources>
21 <!-- There needs to be correspondence per index between these arrays, which means that if there
22 isn't a longpress action associated with a shortcut item, put in an empty item to make
23 sure everything lines up.
24 -->
25 <array name="car_facet_icons">
26 <item>@drawable/car_ic_navigation</item>
27 <item>@drawable/car_ic_phone</item>
28 <item>@drawable/car_ic_overview</item>
29 <item>@drawable/car_ic_music</item>
30 <item>@drawable/car_ic_car</item>
31 </array>
32 <array name="car_facet_intent_uris">
33 <!-- Launch the lenspicker for all the facets. The lens picker will trampoline into the last run app or display a list of valid apps -->
34 <item>intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x14000000;package=com.android.support.car.lenspicker;end</item>
35 <item>intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x14000000;package=com.android.support.car.lenspicker;end</item>
36 <item>intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x14000000;package=com.android.support.car.lenspicker;S.system_command=show_notifications;end</item>
37 <item>intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x14000000;package=com.android.support.car.lenspicker;end</item>
38 <item>intent:#Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x14000000;package=com.android.support.car.lenspicker;end</item>
39 </array>
40 <array name="car_facet_longpress_intent_uris">
41 <item></item>
42 <item></item>
43 <item></item>
44 <item></item>
45 <!-- Long pressing the overflow triggers a bug report -->
46 <item>intent:#Intent;component=com.google.android.car.bugreport/.BugReportActivity;end</item>
47 </array>
48 <array name="car_facet_category_filters">
49 <item>android.intent.category.APP_MAPS</item>
50 <item>android.intent.category.APP_MESSAGING</item>
51 <item></item>
52 <item>android.intent.category.APP_MUSIC</item>
53 <item></item>
54 </array>
55 <array name="car_facet_package_filters">
56 <item>com.android.car.mapsplaceholder</item>
57 <item>com.android.car.dialer</item>
58 <item>com.android.car.overview</item>
59 <item></item>
60 <item>com.android.car.hvac;com.android.settings;com.android.car.settings;com.android.vending;com.google.android.car.bugreport;com.google.android.car.kitchensink;com.android.car.systemupdater;org.chromium.webview_shell;com.android.contacts;org.codeaurora.bluetooth.bttestapp;com.google.android.projection.sink</item>
61 </array>
62 </resources>
在 arrays_car.xml 中定义了一系列数组,它们对应的含义如下:
array id | Comment |
---|---|
car_facet_icons | 导航按钮图标 |
car_facet_intent_uris | 导航按钮单击响应intent |
car_facet_longpress_intent_uris | 导航按钮长按响应intent |
car_facet_category_filters | 导航按钮类别过滤器 |
car_facet_package_filters | 导航按钮包过滤器 |
有些 item 定义了多个值,用英文分号将每个值分开。
导航按钮单击响应
我们来看导航按钮单击响应方法 CarNavigationBarController.onFacetClicked:
356 /**
357 * Handles a click on a facet. A click will trigger the given Intent.
358 *
359 * @param index The index of the facet that was clicked.
360 */
361 private void onFacetClicked(Intent intent, int index) {
362 String packageName = intent.getPackage();
363
364 if (packageName == null) {
365 return;
366 }
367
368 intent.putExtra(EXTRA_FACET_CATEGORIES, mFacetCategories.get(index));
369 intent.putExtra(EXTRA_FACET_PACKAGES, mFacetPackages.get(index));
370 // The facet is identified by the index in which it was added to the nav bar.
371 // This value can be used to determine which facet was selected
372 intent.putExtra(EXTRA_FACET_ID, Integer.toString(index));
373
374 // If the current facet is clicked, we want to launch the picker by default
375 // rather than the "preferred/last run" app.
376 intent.putExtra(EXTRA_FACET_LAUNCH_PICKER, index == mCurrentFacetIndex);
377
378 int stackId = StackId.FULLSCREEN_WORKSPACE_STACK_ID;
379 if (intent.getCategories().contains(Intent.CATEGORY_HOME)) {
380 stackId = StackId.HOME_STACK_ID;
381 }
382
383 setCurrentFacet(index);
384 mStatusBar.startActivityOnStack(intent, stackId);
385 }
参数 intent 是从 car_facet_intent_uris 数组解析而来,我们发现数组所有的条目对应的 package 都是 com.android.support.car.lenspicker, action 和 category 也都是一致的,可以推测出所有的条目都对应着同一个 activity,也就是 com.android.support.car.lenspicker 包中的 main activity。在LensPicker工程中,我们找到这个activity是 LensPickerTrampolineActivity。
LensPickerTrampolineActivity 顺序图如下:
我们先来看看从 SystemUI 传过来的数据有哪些。
key | key value | comment |
---|---|---|
EXTRA_PACKAGE_RESOLVE_INFO | resolve_info | 要展示应用的resolve info |
EXTRA_FACET_CATEGORIES | categories | 要展示应用的类别 |
EXTRA_FACET_PACKAGES | packages | 要展示应用的包名 |
EXTRA_FACET_ID | filter_id | 要展示导航页的id |
EXTRA_FACET_LAUNCH_PICKER | launch_picker | 指明是否展示lens picker即使有存储的偏好应用 |
EXTRA_FACET_SYSTEM_COMMAND | system_command | 指明LensPicker应该运行的系统命令,可用的命令常亮必须以”SYSTEM_COMMAND”开头 |
SYSTEM_COMMAND_SHOW_NOTIFICATIONS | show_notifications | 可以作为EXTRA_FACET_SYSTEM_COMMAND值的硬编码字符串。这个命令会触发通知栏下拉 |
可以发现除了限定要展示的应用类型,最后两个还指明了是否执行系统命令,目前只发现触发系统栏下拉这一个系统命令。对应的执行代码在上面序列图的第二步executeSystemCommand。
LensPickerTrampolineActivity 的 onCreate 方法主要执行了下面的逻辑:
LensPickerTrampolineActivity 会触发五种操作:
- 执行 SystemCommand;
- 启动LensPickerActivity;
- 启动StreamOverviewActivity;
- 启动保存的app;
- 启动上次的intent;
应用列表展示
我们再来看看 LensPickerActivity 是如何展示应用列表的。使用PagedListView展示列表,adapter为LensPickerAdapter,数据在getComponent方法中构造,
133 private ArrayList<ResolveInfo> getComponents(String[] packages, String[] categories) {
134 List<ResolveInfo> packageList = new ArrayList<>();
135 if (packages != null) {
136 for (int i = 0; i < packages.length; i++) {
137 packageList.addAll(resolvePackage(packages[i]));
138 }
139 }
140
141 if (categories != null) {
142 for (int i = 0; i < categories.length; i++) {
143 packageList.addAll(resolveCategory(categories[i]));
144
145 if (categories[i].equals(Intent.CATEGORY_APP_MUSIC)) {
146 packageList.addAll(resolveMediaBrowserServices());
147 }
148 }
149 }
150
151 // De-dupe the list based on package names
152 HashMap<String, ResolveInfo> dedupeList = new HashMap<>();
153 for (ResolveInfo pkg : packageList) {
154 String packageName = LensPickerUtils.getPackageName(pkg);
155 if (!dedupeList.containsKey(packageName) || LensPickerUtils.isMediaService(pkg)) {
156 dedupeList.put(packageName, pkg);
157 }
158 }
159
160 ArrayList<ResolveInfo> filteredPackageList = new ArrayList<>(dedupeList.values());
161 if (Log.isLoggable(TAG, Log.DEBUG)) {
162 printResolveInfo("before dedupe", packageList);
163 printResolveInfo("after dedupe", filteredPackageList);
164 }
165
166 return filteredPackageList;
167 }
其中resolveCategory 使用PackageManager 的 queryIntentActivities 方法查询对应 category 的 intent。
总结
Android O Car Launcher 跟手机等设备的Launcher 实现差别较大,并没有 CarLauncher这个应用,而是由 SystemUI 的 CarStatusBar 构造的导航栏来完成 Launcher 的功能。每个导航按钮对应一个应用列表(由LensPickerActivity实现)。除了应用列表,也可以导航到单个的应用,比如默认应用Overview,以及上次导航到的应用。此外导航按钮还可以触发系统行为,比如系统菜单栏的下拉。