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