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.systemui.mediaprojection.appselector.data
18 
19 import android.content.ComponentName
20 import android.content.Context
21 import android.graphics.drawable.Drawable
22 import android.os.UserHandle
23 import com.android.launcher3.icons.BaseIconFactory
24 import com.android.launcher3.icons.IconFactory
25 import com.android.launcher3.util.UserIconInfo
26 import com.android.systemui.dagger.qualifiers.Background
27 import javax.inject.Inject
28 import javax.inject.Provider
29 import kotlinx.coroutines.CoroutineDispatcher
30 import kotlinx.coroutines.withContext
31 
32 class BadgedAppIconLoader
33 @Inject
34 constructor(
35     private val basicAppIconLoader: BasicAppIconLoader,
36     @Background private val backgroundDispatcher: CoroutineDispatcher,
37     private val context: Context,
38     private val iconFactoryProvider: Provider<IconFactory>,
39 ) {
40 
41     suspend fun loadIcon(
42         userId: Int,
43         userType: RecentTask.UserType,
44         componentName: ComponentName
45     ): Drawable? =
46         withContext(backgroundDispatcher) {
47             iconFactoryProvider.get().use<IconFactory, Drawable?> { iconFactory ->
48                 val icon =
49                     basicAppIconLoader.loadIcon(userId, componentName) ?: return@withContext null
50                 val userHandler = UserHandle.of(userId)
51                 val iconType = getIconType(userType)
52                 val options =
53                     BaseIconFactory.IconOptions().apply {
54                         setUser(UserIconInfo(userHandler, iconType))
55                     }
56                 val badgedIcon = iconFactory.createBadgedIconBitmap(icon, options)
57                 badgedIcon.newIcon(context)
58             }
59         }
60 
61     private fun getIconType(userType: RecentTask.UserType): Int =
62         when (userType) {
63             RecentTask.UserType.CLONED -> UserIconInfo.TYPE_CLONED
64             RecentTask.UserType.WORK -> UserIconInfo.TYPE_WORK
65             RecentTask.UserType.PRIVATE -> UserIconInfo.TYPE_PRIVATE
66             RecentTask.UserType.STANDARD -> UserIconInfo.TYPE_MAIN
67         }
68 }
69