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.qs.panels.data.repository
18 
19 import com.android.systemui.common.shared.model.ContentDescription
20 import com.android.systemui.common.shared.model.Icon
21 import com.android.systemui.common.shared.model.Text
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.dagger.qualifiers.Background
24 import com.android.systemui.qs.panels.shared.model.EditTileData
25 import com.android.systemui.qs.pipeline.data.repository.InstalledTilesComponentRepository
26 import com.android.systemui.qs.pipeline.shared.TileSpec
27 import com.android.systemui.settings.UserTracker
28 import javax.inject.Inject
29 import kotlin.coroutines.CoroutineContext
30 import kotlinx.coroutines.flow.map
31 import kotlinx.coroutines.withContext
32 
33 @SysUISingleton
34 class IconAndNameCustomRepository
35 @Inject
36 constructor(
37     private val installedTilesComponentRepository: InstalledTilesComponentRepository,
38     private val userTracker: UserTracker,
39     @Background private val backgroundContext: CoroutineContext,
40 ) {
41     /**
42      * Returns a list of the icon/labels for all available (installed and enabled) tile services.
43      *
44      * No order is guaranteed.
45      */
getCustomTileDatanull46     suspend fun getCustomTileData(): List<EditTileData> {
47         return withContext(backgroundContext) {
48             val installedTiles =
49                 installedTilesComponentRepository.getInstalledTilesServiceInfos(userTracker.userId)
50             val packageManager = userTracker.userContext.packageManager
51             installedTiles
52                 .map {
53                     val tileSpec = TileSpec.create(it.componentName)
54                     val label = it.loadLabel(packageManager)
55                     val icon = it.loadIcon(packageManager)
56                     val appName = it.applicationInfo.loadLabel(packageManager)
57                     if (icon != null) {
58                         EditTileData(
59                             tileSpec,
60                             Icon.Loaded(icon, ContentDescription.Loaded(label.toString())),
61                             Text.Loaded(label.toString()),
62                             Text.Loaded(appName.toString()),
63                         )
64                     } else {
65                         null
66                     }
67                 }
68                 .filterNotNull()
69         }
70     }
71 }
72