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.systemui.shared.notifications.data.repository
18 
19 import android.provider.Settings
20 import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository
21 import kotlinx.coroutines.CoroutineDispatcher
22 import kotlinx.coroutines.CoroutineScope
23 import kotlinx.coroutines.flow.Flow
24 import kotlinx.coroutines.flow.StateFlow
25 import kotlinx.coroutines.flow.distinctUntilChanged
26 import kotlinx.coroutines.flow.flowOn
27 import kotlinx.coroutines.flow.map
28 import kotlinx.coroutines.flow.stateIn
29 import kotlinx.coroutines.withContext
30 
31 /** Provides access to state related to notification settings. */
32 class NotificationSettingsRepository(
33     private val scope: CoroutineScope,
34     private val backgroundDispatcher: CoroutineDispatcher,
35     private val secureSettingsRepository: SecureSettingsRepository,
36 ) {
37     val isNotificationHistoryEnabled: Flow<Boolean> =
38         secureSettingsRepository
39             .intSetting(name = Settings.Secure.NOTIFICATION_HISTORY_ENABLED)
<lambda>null40             .map { it == 1 }
41             .distinctUntilChanged()
42 
43     /** The current state of the notification setting. */
isShowNotificationsOnLockScreenEnablednull44     suspend fun isShowNotificationsOnLockScreenEnabled(): StateFlow<Boolean> =
45         secureSettingsRepository
46             .intSetting(
47                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
48             )
49             .map { it == 1 }
50             .flowOn(backgroundDispatcher)
51             .stateIn(
52                 scope = scope,
53             )
54 
setShowNotificationsOnLockscreenEnablednull55     suspend fun setShowNotificationsOnLockscreenEnabled(enabled: Boolean) {
56         withContext(backgroundDispatcher) {
57             secureSettingsRepository.setInt(
58                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
59                 value = if (enabled) 1 else 0,
60             )
61         }
62     }
63 }
64