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 static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_SHORTCUTS; 20 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.pm.ShortcutInfo; 24 import android.os.Handler; 25 import android.os.UserHandle; 26 27 import androidx.annotation.VisibleForTesting; 28 29 import com.android.launcher3.LauncherAppState; 30 import com.android.launcher3.icons.IconCache; 31 import com.android.launcher3.model.data.ItemInfo; 32 import com.android.launcher3.model.data.WorkspaceItemInfo; 33 import com.android.launcher3.shortcuts.DeepShortcutView; 34 import com.android.launcher3.shortcuts.ShortcutRequest; 35 import com.android.launcher3.views.ActivityContext; 36 37 import java.util.ArrayList; 38 import java.util.Comparator; 39 import java.util.List; 40 41 /** 42 * Contains logic relevant to populating a {@link PopupContainerWithArrow}. In particular, 43 * this class determines which items appear in the container, and in what order. 44 */ 45 public class PopupPopulator { 46 47 public static final int MAX_SHORTCUTS = 4; 48 @VisibleForTesting 49 static final int NUM_DYNAMIC = 2; 50 51 /** 52 * Sorts shortcuts in rank order, with manifest shortcuts coming before dynamic shortcuts. 53 */ 54 private static final Comparator<ShortcutInfo> SHORTCUT_RANK_COMPARATOR = (a, b) -> { 55 if (a.isDeclaredInManifest() && !b.isDeclaredInManifest()) { 56 return -1; 57 } 58 if (!a.isDeclaredInManifest() && b.isDeclaredInManifest()) { 59 return 1; 60 } 61 return Integer.compare(a.getRank(), b.getRank()); 62 }; 63 64 /** 65 * Filters the shortcuts so that only MAX_SHORTCUTS or fewer shortcuts are retained. 66 * We want the filter to include both static and dynamic shortcuts, so we always 67 * include NUM_DYNAMIC dynamic shortcuts, if at least that many are present. 68 * 69 * @return a subset of shortcuts, in sorted order, with size <= MAX_SHORTCUTS. 70 */ sortAndFilterShortcuts(List<ShortcutInfo> shortcuts)71 public static List<ShortcutInfo> sortAndFilterShortcuts(List<ShortcutInfo> shortcuts) { 72 shortcuts.sort(SHORTCUT_RANK_COMPARATOR); 73 if (shortcuts.size() <= MAX_SHORTCUTS) { 74 return shortcuts; 75 } 76 77 // The list of shortcuts is now sorted with static shortcuts followed by dynamic 78 // shortcuts. We want to preserve this order, but only keep MAX_SHORTCUTS. 79 List<ShortcutInfo> filteredShortcuts = new ArrayList<>(MAX_SHORTCUTS); 80 int numDynamic = 0; 81 int size = shortcuts.size(); 82 for (int i = 0; i < size; i++) { 83 ShortcutInfo shortcut = shortcuts.get(i); 84 int filteredSize = filteredShortcuts.size(); 85 if (filteredSize < MAX_SHORTCUTS) { 86 // Always add the first MAX_SHORTCUTS to the filtered list. 87 filteredShortcuts.add(shortcut); 88 if (shortcut.isDynamic()) { 89 numDynamic++; 90 } 91 continue; 92 } 93 // At this point, we have MAX_SHORTCUTS already, but they may all be static. 94 // If there are dynamic shortcuts, remove static shortcuts to add them. 95 if (shortcut.isDynamic() && numDynamic < NUM_DYNAMIC) { 96 numDynamic++; 97 int lastStaticIndex = filteredSize - numDynamic; 98 filteredShortcuts.remove(lastStaticIndex); 99 filteredShortcuts.add(shortcut); 100 } 101 } 102 return filteredShortcuts; 103 } 104 105 /** 106 * Returns a runnable to update the provided shortcuts 107 */ createUpdateRunnable( final T context, final ItemInfo originalInfo, final Handler uiHandler, final PopupContainerWithArrow container, final List<DeepShortcutView> shortcutViews)108 public static <T extends Context & ActivityContext> Runnable createUpdateRunnable( 109 final T context, 110 final ItemInfo originalInfo, 111 final Handler uiHandler, final PopupContainerWithArrow container, 112 final List<DeepShortcutView> shortcutViews) { 113 final ComponentName activity = originalInfo.getTargetComponent(); 114 final UserHandle user = originalInfo.user; 115 return () -> { 116 List<ShortcutInfo> shortcuts = new ShortcutRequest(context, user) 117 .withContainer(activity) 118 .query(ShortcutRequest.PUBLISHED); 119 shortcuts = PopupPopulator.sortAndFilterShortcuts(shortcuts); 120 IconCache cache = LauncherAppState.getInstance(context).getIconCache(); 121 for (int i = 0; i < shortcuts.size() && i < shortcutViews.size(); i++) { 122 final ShortcutInfo shortcut = shortcuts.get(i); 123 final WorkspaceItemInfo si = new WorkspaceItemInfo(shortcut, context); 124 cache.getShortcutIcon(si, shortcut); 125 si.rank = i; 126 si.container = CONTAINER_SHORTCUTS; 127 128 final DeepShortcutView view = shortcutViews.get(i); 129 uiHandler.post(() -> view.applyShortcutInfo(si, shortcut, container)); 130 } 131 }; 132 } 133 } 134