Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 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; |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 19 | import android.os.UserHandle; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 20 | |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 21 | import androidx.annotation.NonNull; |
| 22 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 23 | import com.android.launcher3.LauncherAppState; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 24 | import com.android.launcher3.LauncherSettings; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 25 | import com.android.launcher3.icons.IconCache; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 26 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.HashSet; |
| 30 | |
| 31 | /** |
| 32 | * Handles changes due to cache updates. |
| 33 | */ |
Sunny Goyal | e86f11f | 2017-06-06 14:33:18 -0700 | [diff] [blame] | 34 | public class CacheDataUpdatedTask extends BaseModelUpdateTask { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 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; |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 40 | |
| 41 | @NonNull |
Sunny Goyal | 7c74e4a | 2016-12-15 15:53:17 -0800 | [diff] [blame] | 42 | private final UserHandle mUser; |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 43 | |
| 44 | @NonNull |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 45 | private final HashSet<String> mPackages; |
| 46 | |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 47 | public CacheDataUpdatedTask(final int op, @NonNull final UserHandle user, |
| 48 | @NonNull final HashSet<String> packages) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 49 | mOp = op; |
| 50 | mUser = user; |
| 51 | mPackages = packages; |
| 52 | } |
| 53 | |
| 54 | @Override |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 55 | public void execute(@NonNull final LauncherAppState app, @NonNull final BgDataModel dataModel, |
| 56 | @NonNull final AllAppsList apps) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 57 | IconCache iconCache = app.getIconCache(); |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 58 | ArrayList<WorkspaceItemInfo> updatedShortcuts = new ArrayList<>(); |
Sunny Goyal | 87dcde6 | 2019-07-17 20:35:56 -0700 | [diff] [blame] | 59 | |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 60 | synchronized (dataModel) { |
Sunny Goyal | ea600c7 | 2020-07-09 19:31:40 -0700 | [diff] [blame] | 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); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 68 | } |
Sunny Goyal | ea600c7 | 2020-07-09 19:31:40 -0700 | [diff] [blame] | 69 | }); |
Sunny Goyal | 87dcde6 | 2019-07-17 20:35:56 -0700 | [diff] [blame] | 70 | apps.updateIconsAndLabels(mPackages, mUser); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 71 | } |
Sunny Goyal | 9589916 | 2019-03-27 16:03:06 -0700 | [diff] [blame] | 72 | bindUpdatedWorkspaceItems(updatedShortcuts); |
Sunny Goyal | 87dcde6 | 2019-07-17 20:35:56 -0700 | [diff] [blame] | 73 | bindApplicationsIfNeeded(); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Pinyao Ting | 777c13e | 2022-09-16 09:44:26 -0700 | [diff] [blame] | 76 | public boolean isValidShortcut(@NonNull final WorkspaceItemInfo si) { |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 77 | switch (mOp) { |
| 78 | case OP_CACHE_UPDATE: |
Sunny Goyal | 1cd01b0 | 2016-11-09 10:43:58 -0800 | [diff] [blame] | 79 | return true; |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 80 | case OP_SESSION_UPDATE: |
Sunny Goyal | f552392 | 2017-08-28 15:29:18 -0700 | [diff] [blame] | 81 | return si.hasPromiseIconUi(); |
Sunny Goyal | f0ba8b7 | 2016-09-09 15:47:55 -0700 | [diff] [blame] | 82 | default: |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | } |