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