• 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 static com.android.launcher3.icons.cache.CacheLookupFlag.DEFAULT_LOOKUP_FLAG;
19 import static com.android.launcher3.model.ModelUtils.WIDGET_FILTER;
20 
21 import android.content.ComponentName;
22 import android.os.UserHandle;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.launcher3.LauncherModel.ModelUpdateTask;
27 import com.android.launcher3.LauncherSettings;
28 import com.android.launcher3.icons.IconCache;
29 import com.android.launcher3.model.data.ItemInfo;
30 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
31 import com.android.launcher3.model.data.WorkspaceItemInfo;
32 
33 import java.util.ArrayList;
34 import java.util.HashSet;
35 
36 /**
37  * Handles changes due to cache updates.
38  */
39 public class CacheDataUpdatedTask implements ModelUpdateTask {
40 
41     public static final int OP_CACHE_UPDATE = 1;
42     public static final int OP_SESSION_UPDATE = 2;
43 
44     private final int mOp;
45 
46     @NonNull
47     private final UserHandle mUser;
48 
49     @NonNull
50     private final HashSet<String> mPackages;
51 
CacheDataUpdatedTask(final int op, @NonNull final UserHandle user, @NonNull final HashSet<String> packages)52     public CacheDataUpdatedTask(final int op, @NonNull final UserHandle user,
53             @NonNull final HashSet<String> packages) {
54         mOp = op;
55         mUser = user;
56         mPackages = packages;
57     }
58 
59     @Override
execute(@onNull ModelTaskController taskController, @NonNull BgDataModel dataModel, @NonNull AllAppsList apps)60     public void execute(@NonNull ModelTaskController taskController, @NonNull BgDataModel dataModel,
61             @NonNull AllAppsList apps) {
62         IconCache iconCache = taskController.getIconCache();
63         ArrayList<ItemInfo> updatedItems = new ArrayList<>();
64 
65         synchronized (dataModel) {
66             dataModel.forAllWorkspaceItemInfos(mUser, si -> {
67                 ComponentName cn = si.getTargetComponent();
68                 if (si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
69                         && isValidShortcut(si) && cn != null
70                         && mPackages.contains(cn.getPackageName())) {
71                     iconCache.getTitleAndIcon(si, si.getMatchingLookupFlag());
72                     updatedItems.add(si);
73                 }
74             });
75 
76             dataModel.itemsIdMap.stream()
77                     .filter(WIDGET_FILTER)
78                     .filter(item -> mUser.equals(item.user))
79                     .map(item -> (LauncherAppWidgetInfo) item)
80                     .filter(widget -> mPackages.contains(widget.providerName.getPackageName())
81                             && widget.pendingItemInfo != null)
82                     .forEach(widget -> {
83                         iconCache.getTitleAndIconForApp(
84                                 widget.pendingItemInfo, DEFAULT_LOOKUP_FLAG);
85                         updatedItems.add(widget);
86                     });
87 
88             apps.updateIconsAndLabels(mPackages, mUser);
89         }
90         taskController.bindUpdatedWorkspaceItems(updatedItems);
91         taskController.bindApplicationsIfNeeded();
92     }
93 
isValidShortcut(@onNull final WorkspaceItemInfo si)94     public boolean isValidShortcut(@NonNull final WorkspaceItemInfo si) {
95         switch (mOp) {
96             case OP_CACHE_UPDATE:
97                 return true;
98             case OP_SESSION_UPDATE:
99                 return si.hasPromiseIconUi();
100             default:
101                 return false;
102         }
103     }
104 }
105