Search in sources :

Example 1 with FLAG_GET_KEY_FIELDS_ONLY

use of android.content.pm.LauncherApps.ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY in project android_packages_apps_Launcher3 by crdroidandroid.

the class BgDataModel method updateShortcutPinnedState.

/**
 * Updates the deep shortucts state in system to match out internal model, pinning any missing
 * shortcuts and unpinning any extra shortcuts.
 */
public synchronized void updateShortcutPinnedState(Context context, UserHandle user) {
    if (GO_DISABLE_WIDGETS) {
        return;
    }
    // Collect all system shortcuts
    QueryResult result = new ShortcutRequest(context, user).query(PINNED | FLAG_GET_KEY_FIELDS_ONLY);
    if (!result.wasSuccess()) {
        return;
    }
    // Map of packageName to shortcutIds that are currently in the system
    Map<String, Set<String>> systemMap = result.stream().collect(groupingBy(ShortcutInfo::getPackage, mapping(ShortcutInfo::getId, Collectors.toSet())));
    // Collect all model shortcuts
    Stream.Builder<WorkspaceItemInfo> itemStream = Stream.builder();
    forAllWorkspaceItemInfos(user, itemStream::accept);
    // Map of packageName to shortcutIds that are currently in our model
    Map<String, Set<String>> modelMap = Stream.concat(// Model shortcuts
    itemStream.build().filter(wi -> wi.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT).map(ShortcutKey::fromItemInfo), // Pending shortcuts
    ItemInstallQueue.INSTANCE.get(context).getPendingShortcuts(user)).collect(groupingBy(ShortcutKey::getPackageName, mapping(ShortcutKey::getId, Collectors.toSet())));
    // Check for diff
    for (Map.Entry<String, Set<String>> entry : modelMap.entrySet()) {
        Set<String> modelShortcuts = entry.getValue();
        Set<String> systemShortcuts = systemMap.remove(entry.getKey());
        if (systemShortcuts == null) {
            systemShortcuts = Collections.emptySet();
        }
        // Do not use .equals as it can vary based on the type of set
        if (systemShortcuts.size() != modelShortcuts.size() || !systemShortcuts.containsAll(modelShortcuts)) {
            // Update system state for this package
            try {
                context.getSystemService(LauncherApps.class).pinShortcuts(entry.getKey(), new ArrayList<>(modelShortcuts), user);
            } catch (SecurityException | IllegalStateException e) {
                Log.w(TAG, "Failed to pin shortcut", e);
            }
        }
    }
    // If there are any extra pinned shortcuts, remove them
    systemMap.keySet().forEach(packageName -> {
        // Update system state
        try {
            context.getSystemService(LauncherApps.class).pinShortcuts(packageName, Collections.emptyList(), user);
        } catch (SecurityException | IllegalStateException e) {
            Log.w(TAG, "Failed to unpin shortcut", e);
        }
    });
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) AppInfo(com.android.launcher3.model.data.AppInfo) ViewOnDrawExecutor(com.android.launcher3.util.ViewOnDrawExecutor) ItemInfo(com.android.launcher3.model.data.ItemInfo) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) LauncherSettings(com.android.launcher3.LauncherSettings) ShortcutKey(com.android.launcher3.shortcuts.ShortcutKey) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo) Collectors.mapping(java.util.stream.Collectors.mapping) UserHandle(android.os.UserHandle) Map(java.util.Map) FolderInfo(com.android.launcher3.model.data.FolderInfo) ArraySet(android.util.ArraySet) Log(android.util.Log) PrintWriter(java.io.PrintWriter) GO_DISABLE_WIDGETS(com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS) IntArray(com.android.launcher3.util.IntArray) PINNED(com.android.launcher3.shortcuts.ShortcutRequest.PINNED) FLAG_GET_KEY_FIELDS_ONLY(android.content.pm.LauncherApps.ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) Favorites(com.android.launcher3.LauncherSettings.Favorites) Iterator(java.util.Iterator) ShortcutInfo(android.content.pm.ShortcutInfo) LauncherAppWidgetInfo(com.android.launcher3.model.data.LauncherAppWidgetInfo) Set(java.util.Set) TextUtils(android.text.TextUtils) FeatureFlags(com.android.launcher3.config.FeatureFlags) UserCache(com.android.launcher3.pm.UserCache) Collectors(java.util.stream.Collectors) LauncherApps(android.content.pm.LauncherApps) Consumer(java.util.function.Consumer) IntSet(com.android.launcher3.util.IntSet) List(java.util.List) QueryResult(com.android.launcher3.shortcuts.ShortcutRequest.QueryResult) WidgetsListBaseEntry(com.android.launcher3.widget.model.WidgetsListBaseEntry) Stream(java.util.stream.Stream) ComponentKey(com.android.launcher3.util.ComponentKey) FileDescriptor(java.io.FileDescriptor) Workspace(com.android.launcher3.Workspace) ShortcutRequest(com.android.launcher3.shortcuts.ShortcutRequest) ItemInfoMatcher(com.android.launcher3.util.ItemInfoMatcher) IntSparseArrayMap(com.android.launcher3.util.IntSparseArrayMap) Collections(java.util.Collections) HashSet(java.util.HashSet) ArraySet(android.util.ArraySet) Set(java.util.Set) IntSet(com.android.launcher3.util.IntSet) ShortcutInfo(android.content.pm.ShortcutInfo) LauncherApps(android.content.pm.LauncherApps) ShortcutRequest(com.android.launcher3.shortcuts.ShortcutRequest) ShortcutKey(com.android.launcher3.shortcuts.ShortcutKey) QueryResult(com.android.launcher3.shortcuts.ShortcutRequest.QueryResult) Stream(java.util.stream.Stream) HashMap(java.util.HashMap) Map(java.util.Map) IntSparseArrayMap(com.android.launcher3.util.IntSparseArrayMap) WorkspaceItemInfo(com.android.launcher3.model.data.WorkspaceItemInfo)

Aggregations

Context (android.content.Context)1 LauncherApps (android.content.pm.LauncherApps)1 FLAG_GET_KEY_FIELDS_ONLY (android.content.pm.LauncherApps.ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY)1 ShortcutInfo (android.content.pm.ShortcutInfo)1 UserHandle (android.os.UserHandle)1 TextUtils (android.text.TextUtils)1 ArraySet (android.util.ArraySet)1 Log (android.util.Log)1 LauncherSettings (com.android.launcher3.LauncherSettings)1 Favorites (com.android.launcher3.LauncherSettings.Favorites)1 Workspace (com.android.launcher3.Workspace)1 FeatureFlags (com.android.launcher3.config.FeatureFlags)1 GO_DISABLE_WIDGETS (com.android.launcher3.model.WidgetsModel.GO_DISABLE_WIDGETS)1 AppInfo (com.android.launcher3.model.data.AppInfo)1 FolderInfo (com.android.launcher3.model.data.FolderInfo)1 ItemInfo (com.android.launcher3.model.data.ItemInfo)1 LauncherAppWidgetInfo (com.android.launcher3.model.data.LauncherAppWidgetInfo)1 WorkspaceItemInfo (com.android.launcher3.model.data.WorkspaceItemInfo)1 UserCache (com.android.launcher3.pm.UserCache)1 ShortcutKey (com.android.launcher3.shortcuts.ShortcutKey)1