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.ComponentName;
19 import android.os.UserHandle;
20 
21 import com.android.launcher3.LauncherAppState;
22 import com.android.launcher3.LauncherSettings;
23 import com.android.launcher3.icons.IconCache;
24 import com.android.launcher3.model.data.WorkspaceItemInfo;
25 
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 
29 /**
30  * Handles changes due to cache updates.
31  */
32 public class CacheDataUpdatedTask extends BaseModelUpdateTask {
33 
34     public static final int OP_CACHE_UPDATE = 1;
35     public static final int OP_SESSION_UPDATE = 2;
36 
37     private final int mOp;
38     private final UserHandle mUser;
39     private final HashSet<String> mPackages;
40 
CacheDataUpdatedTask(int op, UserHandle user, HashSet<String> packages)41     public CacheDataUpdatedTask(int op, UserHandle user, HashSet<String> packages) {
42         mOp = op;
43         mUser = user;
44         mPackages = packages;
45     }
46 
47     @Override
execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps)48     public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
49         IconCache iconCache = app.getIconCache();
50         ArrayList<WorkspaceItemInfo> updatedShortcuts = new ArrayList<>();
51 
52         synchronized (dataModel) {
53             dataModel.forAllWorkspaceItemInfos(mUser, si -> {
54                 ComponentName cn = si.getTargetComponent();
55                 if (si.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION
56                         && isValidShortcut(si) && cn != null
57                         && mPackages.contains(cn.getPackageName())) {
58                     iconCache.getTitleAndIcon(si, si.usingLowResIcon());
59                     updatedShortcuts.add(si);
60                 }
61             });
62             apps.updateIconsAndLabels(mPackages, mUser);
63         }
64         bindUpdatedWorkspaceItems(updatedShortcuts);
65         bindApplicationsIfNeeded();
66     }
67 
isValidShortcut(WorkspaceItemInfo si)68     public boolean isValidShortcut(WorkspaceItemInfo si) {
69         switch (mOp) {
70             case OP_CACHE_UPDATE:
71                 return true;
72             case OP_SESSION_UPDATE:
73                 return si.hasPromiseIconUi();
74             default:
75                 return false;
76         }
77     }
78 }
79