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