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 boolean hasNotifications = notificationKeys.size() > 0;
77 int numNotificationItems = hasNotifications ? 1 : 0;
78 int numShortcuts = shortcutIds.size();
79 if (hasNotifications && numShortcuts > MAX_SHORTCUTS_IF_NOTIFICATIONS) {
80 numShortcuts = MAX_SHORTCUTS_IF_NOTIFICATIONS;
82 int numItems = Math.min(MAX_ITEMS, numShortcuts + numNotificationItems)
83 + PopupDataProvider.SYSTEM_SHORTCUTS.length;
84 Item[] items = new Item[numItems];
85 for (int i = 0; i < numItems; i++) {
86 items[i] = Item.SHORTCUT;
88 if (hasNotifications) {
89 // The notification layout is always first.
90 items[0] = Item.NOTIFICATION;
92 // The system shortcuts are always last.
93 boolean iconsOnly = !shortcutIds.isEmpty();
94 for (int i = 0; i < PopupDataProvider.SYSTEM_SHORTCUTS.length; i++) {
95 items[numItems - 1 - i] = iconsOnly ? Item.SYSTEM_SHORTCUT_ICON : Item.SYSTEM_SHORTCUT;
100 public static Item[] reverseItems(Item[] items) {
101 if (items == null) return null;
102 int numItems = items.length;
103 Item[] reversedArray = new Item[numItems];
104 for (int i = 0; i < numItems; i++) {
105 reversedArray[i] = items[numItems - i - 1];
107 return reversedArray;
111 * Sorts shortcuts in rank order, with manifest shortcuts coming before dynamic shortcuts.
113 private static final Comparator<ShortcutInfoCompat> SHORTCUT_RANK_COMPARATOR
114 = new Comparator<ShortcutInfoCompat>() {
116 public int compare(ShortcutInfoCompat a, ShortcutInfoCompat b) {
117 if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) {
120 if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) {
123 return Integer.compare(a.getRank(), b.getRank());
128 * Filters the shortcuts so that only MAX_ITEMS or fewer shortcuts are retained.
129 * We want the filter to include both static and dynamic shortcuts, so we always
130 * include NUM_DYNAMIC dynamic shortcuts, if at least that many are present.
132 * @param shortcutIdToRemoveFirst An id that should be filtered out first, if any.
133 * @return a subset of shortcuts, in sorted order, with size <= MAX_ITEMS.
135 public static List<ShortcutInfoCompat> sortAndFilterShortcuts(
136 List<ShortcutInfoCompat> shortcuts, @Nullable String shortcutIdToRemoveFirst) {
137 // Remove up to one specific shortcut before sorting and doing somewhat fancy filtering.
138 if (shortcutIdToRemoveFirst != null) {
139 Iterator<ShortcutInfoCompat> shortcutIterator = shortcuts.iterator();
140 while (shortcutIterator.hasNext()) {
141 if (shortcutIterator.next().getId().equals(shortcutIdToRemoveFirst)) {
142 shortcutIterator.remove();
148 Collections.sort(shortcuts, SHORTCUT_RANK_COMPARATOR);
149 if (shortcuts.size() <= MAX_ITEMS) {
153 // The list of shortcuts is now sorted with static shortcuts followed by dynamic
154 // shortcuts. We want to preserve this order, but only keep MAX_ITEMS.
155 List<ShortcutInfoCompat> filteredShortcuts = new ArrayList<>(MAX_ITEMS);
157 int size = shortcuts.size();
158 for (int i = 0; i < size; i++) {
159 ShortcutInfoCompat shortcut = shortcuts.get(i);
160 int filteredSize = filteredShortcuts.size();
161 if (filteredSize < MAX_ITEMS) {
162 // Always add the first MAX_ITEMS to the filtered list.
163 filteredShortcuts.add(shortcut);
164 if (shortcut.isDynamic()) {
169 // At this point, we have MAX_ITEMS already, but they may all be static.
170 // If there are dynamic shortcuts, remove static shortcuts to add them.
171 if (shortcut.isDynamic() && numDynamic < NUM_DYNAMIC) {
173 int lastStaticIndex = filteredSize - numDynamic;
174 filteredShortcuts.remove(lastStaticIndex);
175 filteredShortcuts.add(shortcut);
178 return filteredShortcuts;
181 public static Runnable createUpdateRunnable(final Launcher launcher, final ItemInfo originalInfo,
182 final Handler uiHandler, final PopupContainerWithArrow container,
183 final List<String> shortcutIds, final List<DeepShortcutView> shortcutViews,
184 final List<NotificationKeyData> notificationKeys,
185 final NotificationItemView notificationView, final List<View> systemShortcutViews) {
186 final ComponentName activity = originalInfo.getTargetComponent();
187 final UserHandle user = originalInfo.user;
188 return new Runnable() {
191 if (notificationView != null) {
192 List<StatusBarNotification> notifications = launcher.getPopupDataProvider()
193 .getStatusBarNotificationsForKeys(notificationKeys);
194 List<NotificationInfo> infos = new ArrayList<>(notifications.size());
195 for (int i = 0; i < notifications.size(); i++) {
196 StatusBarNotification notification = notifications.get(i);
197 infos.add(new NotificationInfo(launcher, notification));
199 uiHandler.post(new UpdateNotificationChild(notificationView, infos));
202 List<ShortcutInfoCompat> shortcuts = DeepShortcutManager.getInstance(launcher)
203 .queryForShortcutsContainer(activity, shortcutIds, user);
204 String shortcutIdToDeDupe = notificationKeys.isEmpty() ? null
205 : notificationKeys.get(0).shortcutId;
206 shortcuts = PopupPopulator.sortAndFilterShortcuts(shortcuts, shortcutIdToDeDupe);
207 for (int i = 0; i < shortcuts.size() && i < shortcutViews.size(); i++) {
208 final ShortcutInfoCompat shortcut = shortcuts.get(i);
209 ShortcutInfo si = new ShortcutInfo(shortcut, launcher);
210 // Use unbadged icon for the menu.
211 si.iconBitmap = LauncherIcons.createShortcutIcon(
212 shortcut, launcher, false /* badged */);
214 uiHandler.post(new UpdateShortcutChild(container, shortcutViews.get(i),
218 // This ensures that mLauncher.getWidgetsForPackageUser()
219 // doesn't return null (it puts all the widgets in memory).
220 for (int i = 0; i < PopupDataProvider.SYSTEM_SHORTCUTS.length; i++) {
221 final SystemShortcut systemShortcut = PopupDataProvider.SYSTEM_SHORTCUTS[i];
222 uiHandler.post(new UpdateSystemShortcutChild(container,
223 systemShortcutViews.get(i), systemShortcut, launcher, originalInfo));
225 uiHandler.post(new Runnable() {
228 launcher.refreshAndBindWidgetsForPackageUser(
229 PackageUserKey.fromItemInfo(originalInfo));
236 /** Updates the shortcut child of this container based on the given shortcut info. */
237 private static class UpdateShortcutChild implements Runnable {
238 private final PopupContainerWithArrow mContainer;
239 private final DeepShortcutView mShortcutChild;
240 private final ShortcutInfo mShortcutChildInfo;
241 private final ShortcutInfoCompat mDetail;
243 public UpdateShortcutChild(PopupContainerWithArrow container, DeepShortcutView shortcutChild,
244 ShortcutInfo shortcutChildInfo, ShortcutInfoCompat detail) {
245 mContainer = container;
246 mShortcutChild = shortcutChild;
247 mShortcutChildInfo = shortcutChildInfo;
253 mShortcutChild.applyShortcutInfo(mShortcutChildInfo, mDetail,
254 mContainer.mShortcutsItemView);
258 /** Updates the notification child based on the given notification info. */
259 private static class UpdateNotificationChild implements Runnable {
260 private NotificationItemView mNotificationView;
261 private List<NotificationInfo> mNotificationInfos;
263 public UpdateNotificationChild(NotificationItemView notificationView,
264 List<NotificationInfo> notificationInfos) {
265 mNotificationView = notificationView;
266 mNotificationInfos = notificationInfos;
271 mNotificationView.applyNotificationInfos(mNotificationInfos);
275 /** Updates the system shortcut child based on the given shortcut info. */
276 private static class UpdateSystemShortcutChild implements Runnable {
277 private static final float DISABLED_ALPHA = 0.38f;
279 private final PopupContainerWithArrow mContainer;
280 private final View mSystemShortcutChild;
281 private final SystemShortcut mSystemShortcutInfo;
282 private final Launcher mLauncher;
283 private final ItemInfo mItemInfo;
285 public UpdateSystemShortcutChild(PopupContainerWithArrow container, View systemShortcutChild,
286 SystemShortcut systemShortcut, Launcher launcher, ItemInfo originalInfo) {
287 mContainer = container;
288 mSystemShortcutChild = systemShortcutChild;
289 mSystemShortcutInfo = systemShortcut;
290 mLauncher = launcher;
291 mItemInfo = originalInfo;
296 final Context context = mSystemShortcutChild.getContext();
297 if (mSystemShortcutChild instanceof DeepShortcutView) {
298 // Expanded system shortcut, with both icon and text shown on white background.
299 final DeepShortcutView shortcutView = (DeepShortcutView) mSystemShortcutChild;
300 shortcutView.getIconView().setBackground(mSystemShortcutInfo.getIcon(context,
301 android.R.attr.textColorTertiary));
302 shortcutView.getBubbleText().setText(mSystemShortcutInfo.getLabel(context));
303 } else if (mSystemShortcutChild instanceof ImageView) {
304 // Only the system shortcut icon shows on a gray background header.
305 final ImageView shortcutIcon = (ImageView) mSystemShortcutChild;
306 shortcutIcon.setImageDrawable(mSystemShortcutInfo.getIcon(context,
307 android.R.attr.textColorHint));
308 shortcutIcon.setContentDescription(mSystemShortcutInfo.getLabel(context));
310 if (!(mSystemShortcutInfo instanceof SystemShortcut.Widgets)) {
311 mSystemShortcutChild.setOnClickListener(mSystemShortcutInfo
312 .getOnClickListener(mLauncher, mItemInfo));
314 mSystemShortcutChild.setTag(mSystemShortcutInfo);
315 // Widgets might not be enabled right away.
316 if (mContainer.enableWidgets()) {
319 // Disable Widgets (we might be able to re-enable when widgets are bound).
320 mSystemShortcutChild.setAlpha(DISABLED_ALPHA);