2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.launcher3.popup;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.os.Handler;
22 import android.os.UserHandle;
23 import android.service.notification.StatusBarNotification;
24 import android.support.annotation.NonNull;
25 import android.support.annotation.Nullable;
26 import android.support.annotation.VisibleForTesting;
27 import android.view.View;
28 import android.widget.ImageView;
30 import com.android.launcher3.ItemInfo;
31 import com.android.launcher3.Launcher;
32 import com.android.launcher3.R;
33 import com.android.launcher3.ShortcutInfo;
34 import com.android.launcher3.graphics.LauncherIcons;
35 import com.android.launcher3.notification.NotificationInfo;
36 import com.android.launcher3.notification.NotificationItemView;
37 import com.android.launcher3.notification.NotificationKeyData;
38 import com.android.launcher3.shortcuts.DeepShortcutManager;
39 import com.android.launcher3.shortcuts.DeepShortcutView;
40 import com.android.launcher3.shortcuts.ShortcutInfoCompat;
41 import com.android.launcher3.util.PackageUserKey;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.Comparator;
46 import java.util.Iterator;
47 import java.util.List;
50 * Contains logic relevant to populating a {@link PopupContainerWithArrow}. In particular,
51 * this class determines which items appear in the container, and in what order.
53 public class PopupPopulator {
55 public static final int MAX_ITEMS = 4;
56 @VisibleForTesting static final int NUM_DYNAMIC = 2;
57 private static final int MAX_SHORTCUTS_IF_NOTIFICATIONS = 2;
60 SHORTCUT(R.layout.deep_shortcut, true),
61 NOTIFICATION(R.layout.notification, false),
62 SYSTEM_SHORTCUT(R.layout.system_shortcut, true),
63 SYSTEM_SHORTCUT_ICON(R.layout.system_shortcut_icon_only, true);
65 public final int layoutId;
66 public final boolean isShortcut;
68 Item(int layoutId, boolean isShortcut) {
69 this.layoutId = layoutId;
70 this.isShortcut = isShortcut;
74 public static @NonNull Item[] getItemsToPopulate(@NonNull List<String> shortcutIds,
75 @NonNull List<NotificationKeyData> notificationKeys,
76 @NonNull List<SystemShortcut> systemShortcuts) {
77 boolean hasNotifications = notificationKeys.size() > 0;
78 int numNotificationItems = hasNotifications ? 1 : 0;
79 int numShortcuts = shortcutIds.size();
80 if (hasNotifications && numShortcuts > MAX_SHORTCUTS_IF_NOTIFICATIONS) {
81 numShortcuts = MAX_SHORTCUTS_IF_NOTIFICATIONS;
83 int numItems = Math.min(MAX_ITEMS, numShortcuts + numNotificationItems)
84 + systemShortcuts.size();
85 Item[] items = new Item[numItems];
86 for (int i = 0; i < numItems; i++) {
87 items[i] = Item.SHORTCUT;
89 if (hasNotifications) {
90 // The notification layout is always first.
91 items[0] = Item.NOTIFICATION;
93 // The system shortcuts are always last.
94 boolean iconsOnly = !shortcutIds.isEmpty();
95 for (int i = 0; i < systemShortcuts.size(); i++) {
96 items[numItems - 1 - i] = iconsOnly ? Item.SYSTEM_SHORTCUT_ICON : Item.SYSTEM_SHORTCUT;
101 public static Item[] reverseItems(Item[] items) {
102 if (items == null) return null;
103 int numItems = items.length;
104 Item[] reversedArray = new Item[numItems];
105 for (int i = 0; i < numItems; i++) {
106 reversedArray[i] = items[numItems - i - 1];
108 return reversedArray;
112 * Sorts shortcuts in rank order, with manifest shortcuts coming before dynamic shortcuts.
114 private static final Comparator<ShortcutInfoCompat> SHORTCUT_RANK_COMPARATOR
115 = new Comparator<ShortcutInfoCompat>() {
117 public int compare(ShortcutInfoCompat a, ShortcutInfoCompat b) {
118 if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) {
121 if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) {
124 return Integer.compare(a.getRank(), b.getRank());
129 * Filters the shortcuts so that only MAX_ITEMS or fewer shortcuts are retained.
130 * We want the filter to include both static and dynamic shortcuts, so we always
131 * include NUM_DYNAMIC dynamic shortcuts, if at least that many are present.
133 * @param shortcutIdToRemoveFirst An id that should be filtered out first, if any.
134 * @return a subset of shortcuts, in sorted order, with size <= MAX_ITEMS.
136 public static List<ShortcutInfoCompat> sortAndFilterShortcuts(
137 List<ShortcutInfoCompat> shortcuts, @Nullable String shortcutIdToRemoveFirst) {
138 // Remove up to one specific shortcut before sorting and doing somewhat fancy filtering.
139 if (shortcutIdToRemoveFirst != null) {
140 Iterator<ShortcutInfoCompat> shortcutIterator = shortcuts.iterator();
141 while (shortcutIterator.hasNext()) {
142 if (shortcutIterator.next().getId().equals(shortcutIdToRemoveFirst)) {
143 shortcutIterator.remove();
149 Collections.sort(shortcuts, SHORTCUT_RANK_COMPARATOR);
150 if (shortcuts.size() <= MAX_ITEMS) {
154 // The list of shortcuts is now sorted with static shortcuts followed by dynamic
155 // shortcuts. We want to preserve this order, but only keep MAX_ITEMS.
156 List<ShortcutInfoCompat> filteredShortcuts = new ArrayList<>(MAX_ITEMS);
158 int size = shortcuts.size();
159 for (int i = 0; i < size; i++) {
160 ShortcutInfoCompat shortcut = shortcuts.get(i);
161 int filteredSize = filteredShortcuts.size();
162 if (filteredSize < MAX_ITEMS) {
163 // Always add the first MAX_ITEMS to the filtered list.
164 filteredShortcuts.add(shortcut);
165 if (shortcut.isDynamic()) {
170 // At this point, we have MAX_ITEMS already, but they may all be static.
171 // If there are dynamic shortcuts, remove static shortcuts to add them.
172 if (shortcut.isDynamic() && numDynamic < NUM_DYNAMIC) {
174 int lastStaticIndex = filteredSize - numDynamic;
175 filteredShortcuts.remove(lastStaticIndex);
176 filteredShortcuts.add(shortcut);
179 return filteredShortcuts;
182 public static Runnable createUpdateRunnable(final Launcher launcher, final ItemInfo originalInfo,
183 final Handler uiHandler, final PopupContainerWithArrow container,
184 final List<String> shortcutIds, final List<DeepShortcutView> shortcutViews,
185 final List<NotificationKeyData> notificationKeys,
186 final NotificationItemView notificationView, final List<SystemShortcut> systemShortcuts,
187 final List<View> systemShortcutViews) {
188 final ComponentName activity = originalInfo.getTargetComponent();
189 final UserHandle user = originalInfo.user;
190 return new Runnable() {
193 if (notificationView != null) {
194 List<StatusBarNotification> notifications = launcher.getPopupDataProvider()
195 .getStatusBarNotificationsForKeys(notificationKeys);
196 List<NotificationInfo> infos = new ArrayList<>(notifications.size());
197 for (int i = 0; i < notifications.size(); i++) {
198 StatusBarNotification notification = notifications.get(i);
199 infos.add(new NotificationInfo(launcher, notification));
201 uiHandler.post(new UpdateNotificationChild(notificationView, infos));
204 List<ShortcutInfoCompat> shortcuts = DeepShortcutManager.getInstance(launcher)
205 .queryForShortcutsContainer(activity, shortcutIds, user);
206 String shortcutIdToDeDupe = notificationKeys.isEmpty() ? null
207 : notificationKeys.get(0).shortcutId;
208 shortcuts = PopupPopulator.sortAndFilterShortcuts(shortcuts, shortcutIdToDeDupe);
209 for (int i = 0; i < shortcuts.size() && i < shortcutViews.size(); i++) {
210 final ShortcutInfoCompat shortcut = shortcuts.get(i);
211 ShortcutInfo si = new ShortcutInfo(shortcut, launcher);
212 // Use unbadged icon for the menu.
213 si.iconBitmap = LauncherIcons.createShortcutIcon(
214 shortcut, launcher, false /* badged */);
216 uiHandler.post(new UpdateShortcutChild(container, shortcutViews.get(i),
220 // This ensures that mLauncher.getWidgetsForPackageUser()
221 // doesn't return null (it puts all the widgets in memory).
222 for (int i = 0; i < systemShortcuts.size(); i++) {
223 final SystemShortcut systemShortcut = systemShortcuts.get(i);
224 uiHandler.post(new UpdateSystemShortcutChild(container,
225 systemShortcutViews.get(i), systemShortcut, launcher, originalInfo));
227 uiHandler.post(new Runnable() {
230 launcher.refreshAndBindWidgetsForPackageUser(
231 PackageUserKey.fromItemInfo(originalInfo));
238 /** Updates the shortcut child of this container based on the given shortcut info. */
239 private static class UpdateShortcutChild implements Runnable {
240 private final PopupContainerWithArrow mContainer;
241 private final DeepShortcutView mShortcutChild;
242 private final ShortcutInfo mShortcutChildInfo;
243 private final ShortcutInfoCompat mDetail;
245 public UpdateShortcutChild(PopupContainerWithArrow container, DeepShortcutView shortcutChild,
246 ShortcutInfo shortcutChildInfo, ShortcutInfoCompat detail) {
247 mContainer = container;
248 mShortcutChild = shortcutChild;
249 mShortcutChildInfo = shortcutChildInfo;
255 mShortcutChild.applyShortcutInfo(mShortcutChildInfo, mDetail,
256 mContainer.mShortcutsItemView);
260 /** Updates the notification child based on the given notification info. */
261 private static class UpdateNotificationChild implements Runnable {
262 private NotificationItemView mNotificationView;
263 private List<NotificationInfo> mNotificationInfos;
265 public UpdateNotificationChild(NotificationItemView notificationView,
266 List<NotificationInfo> notificationInfos) {
267 mNotificationView = notificationView;
268 mNotificationInfos = notificationInfos;
273 mNotificationView.applyNotificationInfos(mNotificationInfos);
277 /** Updates the system shortcut child based on the given shortcut info. */
278 private static class UpdateSystemShortcutChild implements Runnable {
280 private final PopupContainerWithArrow mContainer;
281 private final View mSystemShortcutChild;
282 private final SystemShortcut mSystemShortcutInfo;
283 private final Launcher mLauncher;
284 private final ItemInfo mItemInfo;
286 public UpdateSystemShortcutChild(PopupContainerWithArrow container, View systemShortcutChild,
287 SystemShortcut systemShortcut, Launcher launcher, ItemInfo originalInfo) {
288 mContainer = container;
289 mSystemShortcutChild = systemShortcutChild;
290 mSystemShortcutInfo = systemShortcut;
291 mLauncher = launcher;
292 mItemInfo = originalInfo;
297 final Context context = mSystemShortcutChild.getContext();
298 initializeSystemShortcut(context, mSystemShortcutChild, mSystemShortcutInfo);
299 mSystemShortcutChild.setOnClickListener(mSystemShortcutInfo
300 .getOnClickListener(mLauncher, mItemInfo));
304 public static void initializeSystemShortcut(Context context, View view, SystemShortcut info) {
305 if (view instanceof DeepShortcutView) {
306 // Expanded system shortcut, with both icon and text shown on white background.
307 final DeepShortcutView shortcutView = (DeepShortcutView) view;
308 shortcutView.getIconView().setBackground(info.getIcon(context,
309 android.R.attr.textColorTertiary));
310 shortcutView.getBubbleText().setText(info.getLabel(context));
311 } else if (view instanceof ImageView) {
312 // Only the system shortcut icon shows on a gray background header.
313 final ImageView shortcutIcon = (ImageView) view;
314 shortcutIcon.setImageDrawable(info.getIcon(context,
315 android.R.attr.textColorHint));
316 shortcutIcon.setContentDescription(info.getLabel(context));