• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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 
18 package com.android.customization.picker.notifications.data.repository
19 
20 import android.provider.Settings
21 import com.android.customization.picker.notifications.shared.model.NotificationSettingsModel
22 import com.android.wallpaper.settings.data.repository.SecureSettingsRepository
23 import kotlinx.coroutines.CoroutineDispatcher
24 import kotlinx.coroutines.CoroutineScope
25 import kotlinx.coroutines.flow.SharedFlow
26 import kotlinx.coroutines.flow.SharingStarted
27 import kotlinx.coroutines.flow.map
28 import kotlinx.coroutines.flow.shareIn
29 import kotlinx.coroutines.withContext
30 
31 /** Provides access to state related to notifications. */
32 class NotificationsRepository(
33     scope: CoroutineScope,
34     private val backgroundDispatcher: CoroutineDispatcher,
35     private val secureSettingsRepository: SecureSettingsRepository,
36 ) {
37     /** The current state of the notification setting. */
38     val settings: SharedFlow<NotificationSettingsModel> =
39         secureSettingsRepository
40             .intSetting(
41                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
42             )
43             .map { lockScreenShowNotificationsInt ->
44                 NotificationSettingsModel(
45                     isShowNotificationsOnLockScreenEnabled = lockScreenShowNotificationsInt == 1,
46                 )
47             }
48             .shareIn(
49                 scope = scope,
50                 started = SharingStarted.WhileSubscribed(),
51                 replay = 1,
52             )
53 
54     suspend fun getSettings(): NotificationSettingsModel {
55         return withContext(backgroundDispatcher) {
56             NotificationSettingsModel(
57                 isShowNotificationsOnLockScreenEnabled =
58                     secureSettingsRepository.get(
59                         name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
60                         defaultValue = 0,
61                     ) == 1
62             )
63         }
64     }
65 
66     suspend fun setSettings(model: NotificationSettingsModel) {
67         withContext(backgroundDispatcher) {
68             secureSettingsRepository.set(
69                 name = Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
70                 value = if (model.isShowNotificationsOnLockScreenEnabled) 1 else 0,
71             )
72         }
73     }
74 }
75