??本文對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,以及上次導航到的應用。此外導航按鈕還可以觸發系統行為,比如系統菜單欄的下拉。