1 /*
<lambda>null2  * Copyright (C) 2024 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 
17 package com.android.launcher3.model
18 
19 import android.content.ComponentName
20 import android.os.UserHandle
21 import android.text.TextUtils
22 import com.android.launcher3.LauncherModel.ModelUpdateTask
23 import com.android.launcher3.icons.cache.BaseIconCache
24 import com.android.launcher3.model.data.WorkspaceItemInfo
25 import com.android.launcher3.util.ApplicationInfoWrapper
26 import com.android.launcher3.util.ItemInfoMatcher
27 
28 /** Model task run when there is a package install session failure */
29 class SessionFailureTask(val packageName: String, val user: UserHandle) : ModelUpdateTask {
30 
31     override fun execute(
32         taskController: ModelTaskController,
33         dataModel: BgDataModel,
34         apps: AllAppsList,
35     ) {
36         val iconCache = taskController.app.iconCache
37         val isAppArchived =
38             ApplicationInfoWrapper(taskController.app.context, packageName, user).isArchived()
39         synchronized(dataModel) {
40             if (isAppArchived) {
41                 val updatedItems = mutableListOf<WorkspaceItemInfo>()
42                 // Remove package icon cache entry for archived app in case of a session
43                 // failure.
44                 iconCache.remove(
45                     ComponentName(packageName, packageName + BaseIconCache.EMPTY_CLASS_NAME),
46                     user,
47                 )
48                 for (info in dataModel.itemsIdMap) {
49                     if (info is WorkspaceItemInfo && info.isArchived && user == info.user) {
50                         // Refresh icons on the workspace for archived apps.
51                         iconCache.getTitleAndIcon(info, info.usingLowResIcon())
52                         updatedItems.add(info)
53                     }
54                 }
55 
56                 if (updatedItems.isNotEmpty()) {
57                     taskController.bindUpdatedWorkspaceItems(updatedItems)
58                 }
59                 apps.updateIconsAndLabels(hashSetOf(packageName), user)
60                 taskController.bindApplicationsIfNeeded()
61             } else {
62                 val removedItems =
63                     dataModel.itemsIdMap.filter { info ->
64                         (info is WorkspaceItemInfo && info.hasPromiseIconUi()) &&
65                             user == info.user &&
66                             TextUtils.equals(packageName, info.intent.getPackage())
67                     }
68                 if (removedItems.isNotEmpty()) {
69                     taskController.deleteAndBindComponentsRemoved(
70                         ItemInfoMatcher.ofItems(removedItems),
71                         "removed because install session failed",
72                     )
73                 }
74             }
75         }
76     }
77 }
78