• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.UserHandle;
20 
21 import com.android.launcher3.AllAppsList;
22 import com.android.launcher3.ItemInfo;
23 import com.android.launcher3.LauncherAppState;
24 import com.android.launcher3.LauncherModel;
25 import com.android.launcher3.LauncherSettings;
26 import com.android.launcher3.ShortcutInfo;
27 import com.android.launcher3.graphics.LauncherIcons;
28 import com.android.launcher3.shortcuts.DeepShortcutManager;
29 import com.android.launcher3.shortcuts.ShortcutInfoCompat;
30 import com.android.launcher3.util.MultiHashMap;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /**
36  * Handles changes due to shortcut manager updates (deep shortcut changes)
37  */
38 public class ShortcutsChangedTask extends ExtendedModelTask {
39 
40     private final String mPackageName;
41     private final List<ShortcutInfoCompat> mShortcuts;
42     private final UserHandle mUser;
43     private final boolean mUpdateIdMap;
44 
ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> shortcuts, UserHandle user, boolean updateIdMap)45     public ShortcutsChangedTask(String packageName, List<ShortcutInfoCompat> 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         DeepShortcutManager deepShortcutManager = DeepShortcutManager.getInstance(context);
57         deepShortcutManager.onShortcutsChanged(mShortcuts);
58 
59         // Find ShortcutInfo's that have changed on the workspace.
60         final ArrayList<ShortcutInfo> removedShortcutInfos = new ArrayList<>();
61         MultiHashMap<String, ShortcutInfo> idsToWorkspaceShortcutInfos = new MultiHashMap<>();
62         for (ItemInfo itemInfo : dataModel.itemsIdMap) {
63             if (itemInfo.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
64                 ShortcutInfo si = (ShortcutInfo) itemInfo;
65                 if (si.getIntent().getPackage().equals(mPackageName)
66                         && si.user.equals(mUser)) {
67                     idsToWorkspaceShortcutInfos.addToList(si.getDeepShortcutId(), si);
68                 }
69             }
70         }
71 
72         final ArrayList<ShortcutInfo> updatedShortcutInfos = new ArrayList<>();
73         if (!idsToWorkspaceShortcutInfos.isEmpty()) {
74             // Update the workspace to reflect the changes to updated shortcuts residing on it.
75             List<ShortcutInfoCompat> shortcuts = deepShortcutManager.queryForFullDetails(
76                     mPackageName, new ArrayList<>(idsToWorkspaceShortcutInfos.keySet()), mUser);
77             for (ShortcutInfoCompat fullDetails : shortcuts) {
78                 List<ShortcutInfo> shortcutInfos = idsToWorkspaceShortcutInfos
79                         .remove(fullDetails.getId());
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                     removedShortcutInfos.addAll(shortcutInfos);
87                     continue;
88                 }
89                 for (ShortcutInfo shortcutInfo : shortcutInfos) {
90                     shortcutInfo.updateFromDeepShortcutInfo(fullDetails, context);
91                     shortcutInfo.iconBitmap =
92                             LauncherIcons.createShortcutIcon(fullDetails, context);
93                     updatedShortcutInfos.add(shortcutInfo);
94                 }
95             }
96         }
97 
98         // If there are still entries in idsToWorkspaceShortcutInfos, that means that
99         // the corresponding shortcuts weren't passed in onShortcutsChanged(). This
100         // means they were cleared, so we remove and unpin them now.
101         for (String id : idsToWorkspaceShortcutInfos.keySet()) {
102             removedShortcutInfos.addAll(idsToWorkspaceShortcutInfos.get(id));
103         }
104 
105         bindUpdatedShortcuts(updatedShortcutInfos, removedShortcutInfos, mUser);
106         if (!removedShortcutInfos.isEmpty()) {
107             getModelWriter().deleteItemsFromDatabase(removedShortcutInfos);
108         }
109 
110         if (mUpdateIdMap) {
111             // Update the deep shortcut map if the list of ids has changed for an activity.
112             dataModel.updateDeepShortcutMap(mPackageName, mUser, mShortcuts);
113             bindDeepShortcuts(dataModel);
114         }
115     }
116 }
117