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.pm.ApplicationInfo;
19 import android.content.pm.PackageManager;
20 
21 import com.android.launcher3.LauncherAppState;
22 import com.android.launcher3.model.data.AppInfo;
23 import com.android.launcher3.model.data.ItemInfo;
24 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
25 import com.android.launcher3.pm.PackageInstallInfo;
26 import com.android.launcher3.util.InstantAppResolver;
27 
28 import java.util.HashSet;
29 import java.util.List;
30 
31 /**
32  * Handles changes due to a sessions updates for a currently installing app.
33  */
34 public class PackageInstallStateChangedTask extends BaseModelUpdateTask {
35 
36     private final PackageInstallInfo mInstallInfo;
37 
PackageInstallStateChangedTask(PackageInstallInfo installInfo)38     public PackageInstallStateChangedTask(PackageInstallInfo installInfo) {
39         mInstallInfo = installInfo;
40     }
41 
42     @Override
execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps)43     public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
44         if (mInstallInfo.state == PackageInstallInfo.STATUS_INSTALLED) {
45             try {
46                 // For instant apps we do not get package-add. Use setting events to update
47                 // any pinned icons.
48                 ApplicationInfo ai = app.getContext()
49                         .getPackageManager().getApplicationInfo(mInstallInfo.packageName, 0);
50                 if (InstantAppResolver.newInstance(app.getContext()).isInstantApp(ai)) {
51                     app.getModel().onPackageAdded(ai.packageName, mInstallInfo.user);
52                 }
53             } catch (PackageManager.NameNotFoundException e) {
54                 // Ignore
55             }
56             // Ignore install success events as they are handled by Package add events.
57             return;
58         }
59 
60         synchronized (apps) {
61             List<AppInfo> updatedAppInfos = apps.updatePromiseInstallInfo(mInstallInfo);
62             if (!updatedAppInfos.isEmpty()) {
63                 for (AppInfo appInfo : updatedAppInfos) {
64                     scheduleCallbackTask(c -> c.bindIncrementalDownloadProgressUpdated(appInfo));
65                 }
66             }
67             bindApplicationsIfNeeded();
68         }
69 
70         synchronized (dataModel) {
71             final HashSet<ItemInfo> updates = new HashSet<>();
72             dataModel.forAllWorkspaceItemInfos(mInstallInfo.user, si -> {
73                 if (si.hasPromiseIconUi()
74                         && mInstallInfo.packageName.equals(si.getTargetPackage())) {
75                     si.setProgressLevel(mInstallInfo);
76                     updates.add(si);
77                 }
78             });
79 
80             for (LauncherAppWidgetInfo widget : dataModel.appWidgets) {
81                 if (widget.providerName.getPackageName().equals(mInstallInfo.packageName)) {
82                     widget.installProgress = mInstallInfo.progress;
83                     updates.add(widget);
84                 }
85             }
86 
87             if (!updates.isEmpty()) {
88                 scheduleCallbackTask(callbacks -> callbacks.bindRestoreItemsChange(updates));
89             }
90         }
91     }
92 }
93