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;
19
20import com.android.launcher3.AllAppsList;
21import com.android.launcher3.ItemInfo;
22import com.android.launcher3.LauncherAppState;
23import com.android.launcher3.LauncherAppWidgetInfo;
24import com.android.launcher3.LauncherModel.CallbackTask;
25import com.android.launcher3.LauncherModel.Callbacks;
26import com.android.launcher3.ShortcutInfo;
27import com.android.launcher3.compat.PackageInstallerCompat;
28import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
29
30import java.util.HashSet;
31
32/**
33 * Handles changes due to a sessions updates for a currently installing app.
34 */
35public class PackageInstallStateChangedTask extends ExtendedModelTask {
36
37    private final PackageInstallInfo mInstallInfo;
38
39    public PackageInstallStateChangedTask(PackageInstallInfo installInfo) {
40        mInstallInfo = installInfo;
41    }
42
43    @Override
44    public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
45        if (mInstallInfo.state == PackageInstallerCompat.STATUS_INSTALLED) {
46            // Ignore install success events as they are handled by Package add events.
47            return;
48        }
49
50        synchronized (dataModel) {
51            final HashSet<ItemInfo> updates = new HashSet<>();
52            for (ItemInfo info : dataModel.itemsIdMap) {
53                if (info instanceof ShortcutInfo) {
54                    ShortcutInfo si = (ShortcutInfo) info;
55                    ComponentName cn = si.getTargetComponent();
56                    if (si.isPromise() && (cn != null)
57                            && mInstallInfo.packageName.equals(cn.getPackageName())) {
58                        si.setInstallProgress(mInstallInfo.progress);
59
60                        if (mInstallInfo.state == PackageInstallerCompat.STATUS_FAILED) {
61                            // Mark this info as broken.
62                            si.status &= ~ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE;
63                        }
64                        updates.add(si);
65                    }
66                }
67            }
68
69            for (LauncherAppWidgetInfo widget : dataModel.appWidgets) {
70                if (widget.providerName.getPackageName().equals(mInstallInfo.packageName)) {
71                    widget.installProgress = mInstallInfo.progress;
72                    updates.add(widget);
73                }
74            }
75
76            if (!updates.isEmpty()) {
77                scheduleCallbackTask(new CallbackTask() {
78                    @Override
79                    public void execute(Callbacks callbacks) {
80                        callbacks.bindRestoreItemsChange(updates);
81                    }
82                });
83            }
84        }
85    }
86}
87