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.notification.app.AppNotificationSettings
30 import com.android.settings.spa.notification.AppNotificationRepository
31 import com.android.settings.spa.notification.IAppNotificationRepository
32 import com.android.settingslib.spa.framework.compose.rememberContext
33 import com.android.settingslib.spa.widget.preference.Preference
34 import com.android.settingslib.spa.widget.preference.PreferenceModel
35 import com.android.settingslib.spaprivileged.model.app.installed
36 import kotlinx.coroutines.Dispatchers
37 import kotlinx.coroutines.flow.flow
38 import kotlinx.coroutines.flow.flowOn
39 
40 @Composable
AppNotificationPreferencenull41 fun AppNotificationPreference(
42     app: ApplicationInfo,
43     repository: IAppNotificationRepository = rememberContext(::AppNotificationRepository),
44 ) {
45     val context = LocalContext.current
46     val summary by remember(app) {
47         flow {
48             emit(repository.getNotificationSummary(app))
49         }.flowOn(Dispatchers.Default)
50     }.collectAsStateWithLifecycle(
51         initialValue = stringResource(R.string.summary_placeholder)
52     )
53     Preference(object : PreferenceModel {
54         override val title = stringResource(R.string.notifications_label)
55         override val summary = { summary }
56         override val enabled = { app.installed }
57         override val onClick = { navigateToAppNotificationSettings(context, app) }
58     })
59 }
60 
navigateToAppNotificationSettingsnull61 private fun navigateToAppNotificationSettings(context: Context, app: ApplicationInfo) {
62     AppInfoDashboardFragment.startAppInfoFragment(
63         AppNotificationSettings::class.java,
64         app,
65         context,
66         AppInfoSettingsProvider.METRICS_CATEGORY,
67     )
68 }
69