blob: 57fefaa8e1e165069625d9def8423a6c87543083 [file] [log] [blame]
Sunny Goyalf0ba8b72016-09-09 15:47:55 -07001/*
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 */
16package com.android.launcher3.model;
17
18import android.content.ComponentName;
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080019import android.os.UserHandle;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070020
Pinyao Ting777c13e2022-09-16 09:44:26 -070021import androidx.annotation.NonNull;
22
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070023import com.android.launcher3.LauncherAppState;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070024import com.android.launcher3.LauncherSettings;
Sunny Goyale396abf2020-04-06 15:11:17 -070025import com.android.launcher3.icons.IconCache;
Sunny Goyale396abf2020-04-06 15:11:17 -070026import com.android.launcher3.model.data.WorkspaceItemInfo;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070027
28import java.util.ArrayList;
29import java.util.HashSet;
30
31/**
32 * Handles changes due to cache updates.
33 */
Sunny Goyale86f11f2017-06-06 14:33:18 -070034public class CacheDataUpdatedTask extends BaseModelUpdateTask {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070035
36 public static final int OP_CACHE_UPDATE = 1;
37 public static final int OP_SESSION_UPDATE = 2;
38
39 private final int mOp;
Pinyao Ting777c13e2022-09-16 09:44:26 -070040
41 @NonNull
Sunny Goyal7c74e4a2016-12-15 15:53:17 -080042 private final UserHandle mUser;
Pinyao Ting777c13e2022-09-16 09:44:26 -070043
44 @NonNull
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070045 private final HashSet<String> mPackages;
46
Pinyao Ting777c13e2022-09-16 09:44:26 -070047 public CacheDataUpdatedTask(final int op, @NonNull final UserHandle user,
48 @NonNull final HashSet<String> packages) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070049 mOp = op;
50 mUser = user;
51 mPackages = packages;
52 }
53
54 @Override
Pinyao Ting777c13e2022-09-16 09:44:26 -070055 public void execute(@NonNull final LauncherAppState app, @NonNull final BgDataModel dataModel,
56 @NonNull final AllAppsList apps) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070057 IconCache iconCache = app.getIconCache();
Sunny Goyal95899162019-03-27 16:03:06 -070058 ArrayList<WorkspaceItemInfo> updatedShortcuts = new ArrayList<>();
Sunny Goyal87dcde62019-07-17 20:35:56 -070059
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070060 synchronized (dataModel) {
Sunny Goyalea600c72020-07-09 19:31:40 -070061 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);
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070068 }
Sunny Goyalea600c72020-07-09 19:31:40 -070069 });
Sunny Goyal87dcde62019-07-17 20:35:56 -070070 apps.updateIconsAndLabels(mPackages, mUser);
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070071 }
Sunny Goyal95899162019-03-27 16:03:06 -070072 bindUpdatedWorkspaceItems(updatedShortcuts);
Sunny Goyal87dcde62019-07-17 20:35:56 -070073 bindApplicationsIfNeeded();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070074 }
75
Pinyao Ting777c13e2022-09-16 09:44:26 -070076 public boolean isValidShortcut(@NonNull final WorkspaceItemInfo si) {
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070077 switch (mOp) {
78 case OP_CACHE_UPDATE:
Sunny Goyal1cd01b02016-11-09 10:43:58 -080079 return true;
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070080 case OP_SESSION_UPDATE:
Sunny Goyalf5523922017-08-28 15:29:18 -070081 return si.hasPromiseIconUi();
Sunny Goyalf0ba8b72016-09-09 15:47:55 -070082 default:
83 return false;
84 }
85 }
86}