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