OSDN Git Service

Update icon badges to match spec
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / popup / PopupPopulator.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.launcher3.popup;
18
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;
29
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;
42
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;
48
49 /**
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.
52  */
53 public class PopupPopulator {
54
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;
58
59     public enum Item {
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);
64
65         public final int layoutId;
66         public final boolean isShortcut;
67
68         Item(int layoutId, boolean isShortcut) {
69             this.layoutId = layoutId;
70             this.isShortcut = isShortcut;
71         }
72     }
73
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;
81         }
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;
87         }
88         if (hasNotifications) {
89             // The notification layout is always first.
90             items[0] = Item.NOTIFICATION;
91         }
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;
96         }
97         return items;
98     }
99
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];
106         }
107         return reversedArray;
108     }
109
110     /**
111      * Sorts shortcuts in rank order, with manifest shortcuts coming before dynamic shortcuts.
112      */
113     private static final Comparator<ShortcutInfoCompat> SHORTCUT_RANK_COMPARATOR
114             = new Comparator<ShortcutInfoCompat>() {
115         @Override
116         public int compare(ShortcutInfoCompat a, ShortcutInfoCompat b) {
117             if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) {
118                 return -1;
119             }
120             if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) {
121                 return 1;
122             }
123             return Integer.compare(a.getRank(), b.getRank());
124         }
125     };
126
127     /**
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.
131      *
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.
134      */
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();
143                     break;
144                 }
145             }
146         }
147
148         Collections.sort(shortcuts, SHORTCUT_RANK_COMPARATOR);
149         if (shortcuts.size() <= MAX_ITEMS) {
150             return shortcuts;
151         }
152
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);
156         int numDynamic = 0;
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()) {
165                     numDynamic++;
166                 }
167                 continue;
168             }
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) {
172                 numDynamic++;
173                 int lastStaticIndex = filteredSize - numDynamic;
174                 filteredShortcuts.remove(lastStaticIndex);
175                 filteredShortcuts.add(shortcut);
176             }
177         }
178         return filteredShortcuts;
179     }
180
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() {
189             @Override
190             public void run() {
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));
198                     }
199                     uiHandler.post(new UpdateNotificationChild(notificationView, infos));
200                 }
201
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 */);
213                     si.rank = i;
214                     uiHandler.post(new UpdateShortcutChild(container, shortcutViews.get(i),
215                             si, shortcut));
216                 }
217
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));
224                 }
225                 uiHandler.post(new Runnable() {
226                     @Override
227                     public void run() {
228                         launcher.refreshAndBindWidgetsForPackageUser(
229                                 PackageUserKey.fromItemInfo(originalInfo));
230                     }
231                 });
232             }
233         };
234     }
235
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;
242
243         public UpdateShortcutChild(PopupContainerWithArrow container, DeepShortcutView shortcutChild,
244                 ShortcutInfo shortcutChildInfo, ShortcutInfoCompat detail) {
245             mContainer = container;
246             mShortcutChild = shortcutChild;
247             mShortcutChildInfo = shortcutChildInfo;
248             mDetail = detail;
249         }
250
251         @Override
252         public void run() {
253             mShortcutChild.applyShortcutInfo(mShortcutChildInfo, mDetail,
254                     mContainer.mShortcutsItemView);
255         }
256     }
257
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;
262
263         public UpdateNotificationChild(NotificationItemView notificationView,
264                 List<NotificationInfo> notificationInfos) {
265             mNotificationView = notificationView;
266             mNotificationInfos = notificationInfos;
267         }
268
269         @Override
270         public void run() {
271             mNotificationView.applyNotificationInfos(mNotificationInfos);
272         }
273     }
274
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;
278
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;
284
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;
292         }
293
294         @Override
295         public void run() {
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));
309             }
310             if (!(mSystemShortcutInfo instanceof SystemShortcut.Widgets)) {
311                 mSystemShortcutChild.setOnClickListener(mSystemShortcutInfo
312                         .getOnClickListener(mLauncher, mItemInfo));
313             } else {
314                 mSystemShortcutChild.setTag(mSystemShortcutInfo);
315                 // Widgets might not be enabled right away.
316                 if (mContainer.enableWidgets()) {
317                     return;
318                 }
319                 // Disable Widgets (we might be able to re-enable when widgets are bound).
320                 mSystemShortcutChild.setAlpha(DISABLED_ALPHA);
321             }
322         }
323     }
324 }