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 Item(int layoutId, boolean isShortcut)68 Item(int layoutId, boolean isShortcut) { 69 this.layoutId = layoutId; 70 this.isShortcut = isShortcut; 71 } 72 } 73 getItemsToPopulate(@onNull List<String> shortcutIds, @NonNull List<NotificationKeyData> notificationKeys, @NonNull List<SystemShortcut> systemShortcuts)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; 82 } 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; 88 } 89 if (hasNotifications) { 90 // The notification layout is always first. 91 items[0] = Item.NOTIFICATION; 92 } 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; 97 } 98 return items; 99 } 100 reverseItems(Item[] items)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]; 107 } 108 return reversedArray; 109 } 110 111 /** 112 * Sorts shortcuts in rank order, with manifest shortcuts coming before dynamic shortcuts. 113 */ 114 private static final Comparator<ShortcutInfoCompat> SHORTCUT_RANK_COMPARATOR 115 = new Comparator<ShortcutInfoCompat>() { 116 @Override 117 public int compare(ShortcutInfoCompat a, ShortcutInfoCompat b) { 118 if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) { 119 return -1; 120 } 121 if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) { 122 return 1; 123 } 124 return Integer.compare(a.getRank(), b.getRank()); 125 } 126 }; 127 128 /** 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. 132 * 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. 135 */ sortAndFilterShortcuts( List<ShortcutInfoCompat> shortcuts, @Nullable String shortcutIdToRemoveFirst)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(); 144 break; 145 } 146 } 147 } 148 149 Collections.sort(shortcuts, SHORTCUT_RANK_COMPARATOR); 150 if (shortcuts.size() <= MAX_ITEMS) { 151 return shortcuts; 152 } 153 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); 157 int numDynamic = 0; 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()) { 166 numDynamic++; 167 } 168 continue; 169 } 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) { 173 numDynamic++; 174 int lastStaticIndex = filteredSize - numDynamic; 175 filteredShortcuts.remove(lastStaticIndex); 176 filteredShortcuts.add(shortcut); 177 } 178 } 179 return filteredShortcuts; 180 } 181 createUpdateRunnable(final Launcher launcher, final ItemInfo originalInfo, final Handler uiHandler, final PopupContainerWithArrow container, final List<String> shortcutIds, final List<DeepShortcutView> shortcutViews, final List<NotificationKeyData> notificationKeys, final NotificationItemView notificationView, final List<SystemShortcut> systemShortcuts, final List<View> systemShortcutViews)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() { 191 @Override 192 public void run() { 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)); 200 } 201 uiHandler.post(new UpdateNotificationChild(notificationView, infos)); 202 } 203 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 */); 215 si.rank = i; 216 uiHandler.post(new UpdateShortcutChild(container, shortcutViews.get(i), 217 si, shortcut)); 218 } 219 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)); 226 } 227 uiHandler.post(new Runnable() { 228 @Override 229 public void run() { 230 launcher.refreshAndBindWidgetsForPackageUser( 231 PackageUserKey.fromItemInfo(originalInfo)); 232 } 233 }); 234 } 235 }; 236 } 237 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; 244 245 public UpdateShortcutChild(PopupContainerWithArrow container, DeepShortcutView shortcutChild, 246 ShortcutInfo shortcutChildInfo, ShortcutInfoCompat detail) { 247 mContainer = container; 248 mShortcutChild = shortcutChild; 249 mShortcutChildInfo = shortcutChildInfo; 250 mDetail = detail; 251 } 252 253 @Override 254 public void run() { 255 mShortcutChild.applyShortcutInfo(mShortcutChildInfo, mDetail, 256 mContainer.mShortcutsItemView); 257 } 258 } 259 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; 264 265 public UpdateNotificationChild(NotificationItemView notificationView, 266 List<NotificationInfo> notificationInfos) { 267 mNotificationView = notificationView; 268 mNotificationInfos = notificationInfos; 269 } 270 271 @Override 272 public void run() { 273 mNotificationView.applyNotificationInfos(mNotificationInfos); 274 } 275 } 276 277 /** Updates the system shortcut child based on the given shortcut info. */ 278 private static class UpdateSystemShortcutChild implements Runnable { 279 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; 285 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; 293 } 294 295 @Override 296 public void run() { 297 final Context context = mSystemShortcutChild.getContext(); 298 initializeSystemShortcut(context, mSystemShortcutChild, mSystemShortcutInfo); 299 mSystemShortcutChild.setOnClickListener(mSystemShortcutInfo 300 .getOnClickListener(mLauncher, mItemInfo)); 301 } 302 } 303 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)); 317 } 318 view.setTag(info); 319 } 320 } 321