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 android.provider.Settings.Secure.ZEN_DURATION_PROMPT 21 import com.android.systemui.shared.settings.data.repository.SecureSettingsRepository 22 import com.android.systemui.shared.settings.data.repository.SystemSettingsRepository 23 import kotlinx.coroutines.CoroutineDispatcher 24 import kotlinx.coroutines.CoroutineScope 25 import kotlinx.coroutines.flow.Flow 26 import kotlinx.coroutines.flow.SharingStarted 27 import kotlinx.coroutines.flow.StateFlow 28 import kotlinx.coroutines.flow.distinctUntilChanged 29 import kotlinx.coroutines.flow.flowOn 30 import kotlinx.coroutines.flow.map 31 import kotlinx.coroutines.flow.stateIn 32 import kotlinx.coroutines.withContext 33 34 /** Provides access to state related to notification settings. */ 35 class NotificationSettingsRepository( 36 private val backgroundScope: CoroutineScope, 37 private val backgroundDispatcher: CoroutineDispatcher, 38 private val secureSettingsRepository: SecureSettingsRepository, 39 systemSettingsRepository: SystemSettingsRepository, 40 ) { 41 val isNotificationHistoryEnabled: Flow<Boolean> = 42 secureSettingsRepository 43 .intSetting(name = Settings.Secure.NOTIFICATION_HISTORY_ENABLED) <lambda>null44 .map { it == 1 } 45 .distinctUntilChanged() 46 47 /** The current state of the notification setting. */ isShowNotificationsOnLockScreenEnablednull48 suspend fun isShowNotificationsOnLockScreenEnabled(): StateFlow<Boolean> = 49 secureSettingsRepository 50 .intSetting( 51 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 52 ) 53 .map { it == 1 } 54 .flowOn(backgroundDispatcher) 55 .stateIn(scope = backgroundScope) 56 setShowNotificationsOnLockscreenEnablednull57 suspend fun setShowNotificationsOnLockscreenEnabled(enabled: Boolean) { 58 withContext(backgroundDispatcher) { 59 secureSettingsRepository.setInt( 60 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 61 value = if (enabled) 1 else 0, 62 ) 63 } 64 } 65 66 val isCooldownEnabled: StateFlow<Boolean> = 67 systemSettingsRepository 68 .intSetting(name = Settings.System.NOTIFICATION_COOLDOWN_ENABLED) <lambda>null69 .map { it == 1 } 70 .flowOn(backgroundDispatcher) 71 .stateIn( 72 scope = backgroundScope, 73 started = SharingStarted.Eagerly, 74 initialValue = true, 75 ) 76 77 /** The default duration for DND mode when enabled. See [Settings.Secure.ZEN_DURATION]. */ 78 val zenDuration: StateFlow<Int> = 79 secureSettingsRepository 80 .intSetting(name = Settings.Secure.ZEN_DURATION) 81 .distinctUntilChanged() 82 .flowOn(backgroundDispatcher) 83 .stateIn( 84 backgroundScope, 85 started = SharingStarted.Eagerly, 86 initialValue = ZEN_DURATION_PROMPT, 87 ) 88 } 89