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