1 /*
2  * Copyright (C) 2022 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.settingslib.spaprivileged.model.app
18 
19 import android.content.Context
20 import android.content.pm.ApplicationInfo
21 import android.graphics.drawable.Drawable
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.State
24 import androidx.compose.runtime.produceState
25 import androidx.compose.ui.platform.LocalContext
26 import androidx.compose.ui.res.stringResource
27 import com.android.settingslib.Utils
28 import com.android.settingslib.spa.framework.compose.rememberContext
29 import com.android.settingslib.spaprivileged.R
30 import com.android.settingslib.spaprivileged.framework.common.userManager
31 import kotlinx.coroutines.Dispatchers
32 import kotlinx.coroutines.withContext
33 
34 @Composable
35 fun rememberAppRepository(): AppRepository = rememberContext(::AppRepositoryImpl)
36 
37 interface AppRepository {
38     fun loadLabel(app: ApplicationInfo): String
39 
40     @Composable
41     fun produceLabel(app: ApplicationInfo, isClonedAppPage: Boolean = false): State<String> {
42         val context = LocalContext.current
43         return produceState(initialValue = stringResource(R.string.summary_placeholder), app) {
44             withContext(Dispatchers.IO) {
45                 value = if (isClonedAppPage || isCloneApp(context, app)) {
46                     context.getString(R.string.cloned_app_info_label, loadLabel(app))
47                 } else {
48                     loadLabel(app)
49                 }
50             }
51         }
52     }
53 
54     private fun isCloneApp(context: Context, app: ApplicationInfo): Boolean {
55         val userInfo = context.userManager.getUserInfo(app.userId)
56         return userInfo != null && userInfo.isCloneProfile
57     }
58 
59     @Composable
60     fun produceIcon(app: ApplicationInfo): State<Drawable?>
61 
62     @Composable
63     fun produceIconContentDescription(app: ApplicationInfo): State<String?>
64 }
65 
66 internal class AppRepositoryImpl(private val context: Context) : AppRepository {
67     private val packageManager = context.packageManager
68 
69     override fun loadLabel(app: ApplicationInfo): String = app.loadLabel(packageManager).toString()
70 
71     @Composable
72     override fun produceIcon(app: ApplicationInfo) =
73         produceState<Drawable?>(initialValue = null, app) {
74             withContext(Dispatchers.IO) {
75                 value = Utils.getBadgedIcon(context, app)
76             }
77         }
78 
79     @Composable
80     override fun produceIconContentDescription(app: ApplicationInfo) =
81         produceState<String?>(initialValue = null, app) {
82             withContext(Dispatchers.IO) {
83                 value = when {
84                     context.userManager.isManagedProfile(app.userId) -> {
85                         context.getString(R.string.category_work)
86                     }
87 
88                     else -> null
89                 }
90             }
91         }
92 }
93