1 /*
2  * Copyright (C) 2020 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.os.UserHandle;
19 
20 import androidx.annotation.NonNull;
21 
22 import com.android.launcher3.LauncherModel.ModelUpdateTask;
23 import com.android.launcher3.model.data.AppInfo;
24 import com.android.launcher3.model.data.ItemInfoWithIcon;
25 import com.android.launcher3.model.data.WorkspaceItemInfo;
26 import com.android.launcher3.pm.PackageInstallInfo;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Handles updates due to incremental download progress updates.
33  */
34 public class PackageIncrementalDownloadUpdatedTask implements ModelUpdateTask {
35 
36     @NonNull
37     private final UserHandle mUser;
38 
39     private final int mProgress;
40 
41     @NonNull
42     private final String mPackageName;
43 
PackageIncrementalDownloadUpdatedTask(@onNull final String packageName, @NonNull final UserHandle user, final float progress)44     public PackageIncrementalDownloadUpdatedTask(@NonNull final String packageName,
45             @NonNull final UserHandle user, final float progress) {
46         mUser = user;
47         mProgress = 1 - progress > 0.001 ? (int) (100 * progress) : 100;
48         mPackageName = packageName;
49     }
50 
51     @Override
execute(@onNull ModelTaskController taskController, @NonNull BgDataModel dataModel, @NonNull AllAppsList appsList)52     public void execute(@NonNull ModelTaskController taskController, @NonNull BgDataModel dataModel,
53             @NonNull AllAppsList appsList) {
54         PackageInstallInfo downloadInfo = new PackageInstallInfo(
55                 mPackageName,
56                 PackageInstallInfo.STATUS_INSTALLED_DOWNLOADING,
57                 mProgress,
58                 mUser);
59 
60         synchronized (appsList) {
61             List<AppInfo> updatedAppInfos = appsList.updatePromiseInstallInfo(downloadInfo);
62             if (!updatedAppInfos.isEmpty()) {
63                 for (AppInfo appInfo : updatedAppInfos) {
64                     appInfo.runtimeStatusFlags &= ~ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
65                     taskController.scheduleCallbackTask(
66                             c -> c.bindIncrementalDownloadProgressUpdated(appInfo));
67                 }
68             }
69             taskController.bindApplicationsIfNeeded();
70         }
71 
72         final ArrayList<WorkspaceItemInfo> updatedWorkspaceItems = new ArrayList<>();
73         synchronized (dataModel) {
74             dataModel.forAllWorkspaceItemInfos(mUser, si -> {
75                 if (mPackageName.equals(si.getTargetPackage())) {
76                     si.runtimeStatusFlags &= ~ItemInfoWithIcon.FLAG_INSTALL_SESSION_ACTIVE;
77                     si.setProgressLevel(downloadInfo);
78                     updatedWorkspaceItems.add(si);
79                 }
80             });
81         }
82         taskController.bindUpdatedWorkspaceItems(updatedWorkspaceItems);
83     }
84 }
85