1 /*
2  * Copyright (C) 2016 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 package com.android.launcher3.model;
17 
18 import android.content.Context;
19 import android.content.pm.ShortcutInfo;
20 import android.os.UserHandle;
21 
22 import com.android.launcher3.LauncherAppState;
23 import com.android.launcher3.LauncherSettings;
24 import com.android.launcher3.model.data.ItemInfo;
25 import com.android.launcher3.model.data.WorkspaceItemInfo;
26 import com.android.launcher3.shortcuts.ShortcutKey;
27 import com.android.launcher3.shortcuts.ShortcutRequest;
28 import com.android.launcher3.util.ItemInfoMatcher;
29 import com.android.launcher3.util.MultiHashMap;
30 
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.List;
34 
35 /**
36  * Handles changes due to shortcut manager updates (deep shortcut changes)
37  */
38 public class ShortcutsChangedTask extends BaseModelUpdateTask {
39 
40     private final String mPackageName;
41     private final List<ShortcutInfo> mShortcuts;
42     private final UserHandle mUser;
43     private final boolean mUpdateIdMap;
44 
ShortcutsChangedTask(String packageName, List<ShortcutInfo> shortcuts, UserHandle user, boolean updateIdMap)45     public ShortcutsChangedTask(String packageName, List<ShortcutInfo> shortcuts,
46             UserHandle user, boolean updateIdMap) {
47         mPackageName = packageName;
48         mShortcuts = shortcuts;
49         mUser = user;
50         mUpdateIdMap = updateIdMap;
51     }
52 
53     @Override
execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps)54     public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
55         final Context context = app.getContext();
56         // Find WorkspaceItemInfo's that have changed on the workspace.
57         HashSet<ShortcutKey> removedKeys = new HashSet<>();
58         MultiHashMap<ShortcutKey, WorkspaceItemInfo> keyToShortcutInfo = new MultiHashMap<>();
59         HashSet<String> allIds = new HashSet<>();
60 
61         for (ItemInfo itemInfo : dataModel.itemsIdMap) {
62             if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
63                 WorkspaceItemInfo si = (WorkspaceItemInfo) itemInfo;
64                 if (mPackageName.equals(si.getIntent().getPackage()) && si.user.equals(mUser)) {
65                     keyToShortcutInfo.addToList(ShortcutKey.fromItemInfo(si), si);
66                     allIds.add(si.getDeepShortcutId());
67                 }
68             }
69         }
70 
71         final ArrayList<WorkspaceItemInfo> updatedWorkspaceItemInfos = new ArrayList<>();
72         if (!keyToShortcutInfo.isEmpty()) {
73             // Update the workspace to reflect the changes to updated shortcuts residing on it.
74             List<ShortcutInfo> shortcuts = new ShortcutRequest(context, mUser)
75                     .forPackage(mPackageName, new ArrayList<>(allIds))
76                     .query(ShortcutRequest.ALL);
77             for (ShortcutInfo fullDetails : shortcuts) {
78                 ShortcutKey key = ShortcutKey.fromInfo(fullDetails);
79                 List<WorkspaceItemInfo> workspaceItemInfos = keyToShortcutInfo.remove(key);
80                 if (!fullDetails.isPinned()) {
81                     // The shortcut was previously pinned but is no longer, so remove it from
82                     // the workspace and our pinned shortcut counts.
83                     // Note that we put this check here, after querying for full details,
84                     // because there's a possible race condition between pinning and
85                     // receiving this callback.
86                     removedKeys.add(key);
87                     continue;
88                 }
89                 for (final WorkspaceItemInfo workspaceItemInfo : workspaceItemInfos) {
90                     workspaceItemInfo.updateFromDeepShortcutInfo(fullDetails, context);
91                     app.getIconCache().getShortcutIcon(workspaceItemInfo, fullDetails);
92                     updatedWorkspaceItemInfos.add(workspaceItemInfo);
93                 }
94             }
95         }
96 
97         // If there are still entries in keyToShortcutInfo, that means that
98         // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
99         // means they were cleared, so we remove and unpin them now.
100         removedKeys.addAll(keyToShortcutInfo.keySet());
101 
102         bindUpdatedWorkspaceItems(updatedWorkspaceItemInfos);
103         if (!keyToShortcutInfo.isEmpty()) {
104             deleteAndBindComponentsRemoved(ItemInfoMatcher.ofShortcutKeys(removedKeys));
105         }
106 
107         if (mUpdateIdMap) {
108             // Update the deep shortcut map if the list of ids has changed for an activity.
109             dataModel.updateDeepShortcutCounts(mPackageName, mUser, mShortcuts);
110             bindDeepShortcuts(dataModel);
111         }
112     }
113 }
114