1 /*
2  * Copyright (C) 2023 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.pipeline.data.repository
18 
19 import android.content.ComponentName
20 import android.content.pm.ApplicationInfo
21 import android.content.pm.PackageManager
22 import android.content.pm.ServiceInfo
23 import android.graphics.drawable.Drawable
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.MutableStateFlow
26 import kotlinx.coroutines.flow.map
27 
28 class FakeInstalledTilesComponentRepository : InstalledTilesComponentRepository {
29 
30     private val installedServicesPerUser = mutableMapOf<Int, MutableStateFlow<List<ServiceInfo>>>()
31 
getInstalledTilesComponentsnull32     override fun getInstalledTilesComponents(userId: Int): Flow<Set<ComponentName>> {
33         return getFlow(userId).map { it.map { it.componentName }.toSet() }
34     }
35 
getInstalledTilesServiceInfosnull36     override fun getInstalledTilesServiceInfos(userId: Int): List<ServiceInfo> {
37         return getFlow(userId).value
38     }
39 
setInstalledPackagesForUsernull40     fun setInstalledPackagesForUser(userId: Int, components: Set<ComponentName>) {
41         getFlow(userId).value =
42             components.map {
43                 ServiceInfo().apply {
44                     packageName = it.packageName
45                     name = it.className
46                     applicationInfo = ApplicationInfo()
47                 }
48             }
49     }
50 
setInstalledServicesForUsernull51     fun setInstalledServicesForUser(userId: Int, services: List<ServiceInfo>) {
52         getFlow(userId).value = services.toList()
53     }
54 
getFlownull55     private fun getFlow(userId: Int): MutableStateFlow<List<ServiceInfo>> =
56         installedServicesPerUser.getOrPut(userId) { MutableStateFlow(emptyList()) }
57 
58     companion object {
ServiceInfonull59         fun ServiceInfo(
60             componentName: ComponentName,
61             serviceName: String,
62             serviceIcon: Drawable? = null,
63             appName: String = "",
64             appIcon: Drawable? = null
65         ): ServiceInfo {
66             val appInfo =
67                 object : ApplicationInfo() {
68                     override fun loadLabel(pm: PackageManager): CharSequence {
69                         return appName
70                     }
71 
72                     override fun loadIcon(pm: PackageManager?): Drawable? {
73                         return appIcon
74                     }
75                 }
76             val serviceInfo =
77                 object : ServiceInfo() {
78                         override fun loadLabel(pm: PackageManager): CharSequence {
79                             return serviceName
80                         }
81 
82                         override fun loadIcon(pm: PackageManager?): Drawable? {
83                             return serviceIcon ?: getApplicationInfo().loadIcon(pm)
84                         }
85                     }
86                     .apply {
87                         packageName = componentName.packageName
88                         name = componentName.className
89                         applicationInfo = appInfo
90                     }
91             return serviceInfo
92         }
93     }
94 }
95