1 /*
2  * Copyright (C) 2024 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.print
18 
19 import android.app.settings.SettingsEnums
20 import android.os.Bundle
21 import androidx.annotation.VisibleForTesting
22 import androidx.compose.foundation.Image
23 import androidx.compose.foundation.layout.size
24 import androidx.compose.runtime.Composable
25 import androidx.compose.runtime.getValue
26 import androidx.compose.runtime.remember
27 import androidx.compose.ui.Modifier
28 import androidx.compose.ui.platform.LocalContext
29 import androidx.compose.ui.res.stringResource
30 import androidx.core.os.bundleOf
31 import androidx.lifecycle.compose.collectAsStateWithLifecycle
32 import com.android.settings.R
33 import com.android.settings.core.SubSettingLauncher
34 import com.android.settings.print.PrintRepository.PrintServiceDisplayInfo
35 import com.android.settings.print.PrintSettingsFragment.EXTRA_CHECKED
36 import com.android.settings.print.PrintSettingsFragment.EXTRA_SERVICE_COMPONENT_NAME
37 import com.android.settings.print.PrintSettingsFragment.EXTRA_TITLE
38 import com.android.settingslib.spa.framework.common.SettingsPageProvider
39 import com.android.settingslib.spa.framework.compose.rememberDrawablePainter
40 import com.android.settingslib.spa.framework.theme.SettingsDimension
41 import com.android.settingslib.spa.widget.preference.Preference
42 import com.android.settingslib.spa.widget.preference.PreferenceModel
43 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
44 import com.android.settingslib.spa.widget.ui.Category
45 import com.android.settingslib.spaprivileged.template.common.UserProfilePager
46 
47 object PrintSettingsPageProvider : SettingsPageProvider {
48     override val name = "PrintSettings"
49 
50     @Composable
Pagenull51     override fun Page(arguments: Bundle?) {
52         RegularScaffold(title = stringResource(R.string.print_settings)) {
53             val context = LocalContext.current
54             val printRepository = remember(context) { PrintRepository(context) }
55             UserProfilePager {
56                 PrintServices(printRepository)
57             }
58         }
59     }
60 
61     @Composable
PrintServicesnull62     private fun PrintServices(printRepository: PrintRepository) {
63         val printServiceDisplayInfos by remember {
64             printRepository.printServiceDisplayInfosFlow()
65         }.collectAsStateWithLifecycle(initialValue = emptyList())
66         Category(title = stringResource(R.string.print_settings_title)) {
67             for (printServiceDisplayInfo in printServiceDisplayInfos) {
68                 PrintService(printServiceDisplayInfo)
69             }
70         }
71     }
72 
73     @VisibleForTesting
74     @Composable
PrintServicenull75     fun PrintService(displayInfo: PrintServiceDisplayInfo) {
76         val context = LocalContext.current
77         Preference(model = object : PreferenceModel {
78             override val title = displayInfo.title
79             override val summary = { displayInfo.summary }
80             override val icon: @Composable () -> Unit = {
81                 Image(
82                     painter = rememberDrawablePainter(displayInfo.icon),
83                     contentDescription = null,
84                     modifier = Modifier.size(SettingsDimension.appIconItemSize),
85                 )
86             }
87             override val onClick = {
88                 SubSettingLauncher(context).apply {
89                     setDestination(PrintServiceSettingsFragment::class.qualifiedName)
90                     setArguments(
91                         bundleOf(
92                             EXTRA_CHECKED to displayInfo.isEnabled,
93                             EXTRA_TITLE to displayInfo.title,
94                             EXTRA_SERVICE_COMPONENT_NAME to displayInfo.componentName
95                         )
96                     )
97                     setSourceMetricsCategory(SettingsEnums.PRINT_SETTINGS)
98                 }.launch()
99             }
100         })
101     }
102 }
103