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.settings.spa.app.appinfo
18
19 import android.content.Context
20 import android.content.pm.ApplicationInfo
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.remember
24 import androidx.compose.ui.platform.LocalContext
25 import androidx.compose.ui.res.stringResource
26 import androidx.lifecycle.compose.collectAsStateWithLifecycle
27 import com.android.settings.R
28 import com.android.settings.applications.appinfo.AppInfoDashboardFragment
29 import com.android.settings.applications.specialaccess.interactacrossprofiles.InteractAcrossProfilesDetails
30 import com.android.settingslib.spa.widget.preference.Preference
31 import com.android.settingslib.spa.widget.preference.PreferenceModel
32 import com.android.settingslib.spaprivileged.framework.common.crossProfileApps
33 import kotlinx.coroutines.Dispatchers
34 import kotlinx.coroutines.flow.flow
35 import kotlinx.coroutines.flow.flowOn
36
37 @Composable
InteractAcrossProfilesDetailsPreferencenull38 fun InteractAcrossProfilesDetailsPreference(app: ApplicationInfo) {
39 val context = LocalContext.current
40 val presenter = remember { InteractAcrossProfilesDetailsPresenter(context, app) }
41 if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return
42
43 val summary by presenter.summaryFlow.collectAsStateWithLifecycle(
44 initialValue = stringResource(R.string.summary_placeholder),
45 )
46 Preference(object : PreferenceModel {
47 override val title = stringResource(R.string.interact_across_profiles_title)
48 override val summary = { summary }
49 override val onClick = presenter::startActivity
50 })
51 }
52
53 private class InteractAcrossProfilesDetailsPresenter(
54 private val context: Context,
55 private val app: ApplicationInfo,
56 ) {
57 private val crossProfileApps = context.crossProfileApps
58
<lambda>null59 val isAvailableFlow = flow {
60 emit(crossProfileApps.canUserAttemptToConfigureInteractAcrossProfiles(app.packageName))
61 }.flowOn(Dispatchers.IO)
62
<lambda>null63 val summaryFlow = flow {
64 emit(InteractAcrossProfilesDetails.getPreferenceSummary(context, app.packageName))
65 }.flowOn(Dispatchers.IO)
66
startActivitynull67 fun startActivity() {
68 AppInfoDashboardFragment.startAppInfoFragment(
69 InteractAcrossProfilesDetails::class.java,
70 app,
71 context,
72 AppInfoSettingsProvider.METRICS_CATEGORY,
73 )
74 }
75 }
76