1 /*
2  * 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.screenshot.message
18 
19 import android.content.ComponentName
20 import android.content.pm.PackageManager
21 import android.graphics.drawable.Drawable
22 import android.os.UserHandle
23 import javax.inject.Inject
24 
25 data class LabeledIcon(
26     val label: CharSequence,
27     val badgedIcon: Drawable?,
28 )
29 
30 /** An object that can fetch a label and icon for a given component. */
31 interface PackageLabelIconProvider {
32     /**
33      * @return the label and icon for the given component.
34      * @throws PackageManager.NameNotFoundException if the component was not found.
35      */
getPackageLabelIconnull36     suspend fun getPackageLabelIcon(
37         componentName: ComponentName,
38         userHandle: UserHandle
39     ): LabeledIcon
40 }
41 
42 class PackageLabelIconProviderImpl @Inject constructor(private val packageManager: PackageManager) :
43     PackageLabelIconProvider {
44 
45     override suspend fun getPackageLabelIcon(
46         componentName: ComponentName,
47         userHandle: UserHandle
48     ): LabeledIcon {
49         val info =
50             packageManager.getActivityInfo(componentName, PackageManager.ComponentInfoFlags.of(0L))
51         val icon = packageManager.getActivityIcon(componentName)
52         val badgedIcon = packageManager.getUserBadgedIcon(icon, userHandle)
53         val label = info.loadLabel(packageManager)
54         return LabeledIcon(label, badgedIcon)
55     }
56 }
57