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.settings.spa.app.appcompat
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.appcompat.UserAspectRatioDetails
29 import com.android.settings.applications.appcompat.UserAspectRatioManager
30 import com.android.settings.applications.appinfo.AppInfoDashboardFragment
31 import com.android.settings.spa.app.appinfo.AppInfoSettingsProvider
32 import com.android.settingslib.spa.widget.preference.Preference
33 import com.android.settingslib.spa.widget.preference.PreferenceModel
34 import kotlinx.coroutines.Dispatchers
35 import kotlinx.coroutines.flow.flow
36 import kotlinx.coroutines.flow.flowOn
37
38 @Composable
UserAspectRatioAppPreferencenull39 fun UserAspectRatioAppPreference(app: ApplicationInfo) {
40 val context = LocalContext.current
41 val presenter = remember { UserAspectRatioAppPresenter(context, app) }
42 if (!presenter.isAvailableFlow.collectAsStateWithLifecycle(initialValue = false).value) return
43
44 val summary by presenter.summaryFlow.collectAsStateWithLifecycle(
45 initialValue = stringResource(R.string.summary_placeholder),
46 )
47 Preference(object : PreferenceModel {
48 override val title = stringResource(R.string.aspect_ratio_experimental_title)
49 override val summary = { summary }
50 override val onClick = presenter::startActivity
51 })
52 }
53
54 class UserAspectRatioAppPresenter(
55 private val context: Context,
56 private val app: ApplicationInfo,
57 ) {
58 private val manager = UserAspectRatioManager(context)
59
<lambda>null60 val isAvailableFlow = flow {
61 emit(UserAspectRatioManager.isFeatureEnabled(context)
62 && manager.canDisplayAspectRatioUi(app))
63 }.flowOn(Dispatchers.IO)
64
startActivitynull65 fun startActivity() =
66 navigateToAppAspectRatioSettings(context, app, AppInfoSettingsProvider.METRICS_CATEGORY)
67
68 val summaryFlow = flow {
69 emit(manager.getUserMinAspectRatioEntry(app.packageName, context.userId))
70 }.flowOn(Dispatchers.IO)
71 }
72
navigateToAppAspectRatioSettingsnull73 fun navigateToAppAspectRatioSettings(context: Context, app: ApplicationInfo, metricsCategory: Int) {
74 AppInfoDashboardFragment.startAppInfoFragment(
75 UserAspectRatioDetails::class.java,
76 app,
77 context,
78 metricsCategory,
79 )
80 }
81