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.keyguard.data.repository
18 
19 import android.content.Context
20 import android.provider.Settings
21 import android.view.View
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.dagger.qualifiers.Application
24 import com.android.systemui.settings.UserTracker
25 import com.android.systemui.util.settings.SecureSettings
26 import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
27 import javax.inject.Inject
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.flow.MutableStateFlow
30 import kotlinx.coroutines.flow.SharingStarted
31 import kotlinx.coroutines.flow.StateFlow
32 import kotlinx.coroutines.flow.asStateFlow
33 import kotlinx.coroutines.flow.map
34 import kotlinx.coroutines.flow.onStart
35 import kotlinx.coroutines.flow.stateIn
36 
37 interface KeyguardSmartspaceRepository {
38     val bcSmartspaceVisibility: StateFlow<Int>
39     val isWeatherEnabled: StateFlow<Boolean>
setBcSmartspaceVisibilitynull40     fun setBcSmartspaceVisibility(visibility: Int)
41 }
42 
43 @SysUISingleton
44 class KeyguardSmartspaceRepositoryImpl
45 @Inject
46 constructor(
47     context: Context,
48     private val secureSettings: SecureSettings,
49     private val userTracker: UserTracker,
50     @Application private val applicationScope: CoroutineScope,
51 ) : KeyguardSmartspaceRepository {
52     private val _bcSmartspaceVisibility: MutableStateFlow<Int> = MutableStateFlow(View.GONE)
53     override val bcSmartspaceVisibility: StateFlow<Int> = _bcSmartspaceVisibility.asStateFlow()
54     override val isWeatherEnabled: StateFlow<Boolean> =
55         secureSettings
56             .observerFlow(
57                 names = arrayOf(Settings.Secure.LOCK_SCREEN_WEATHER_ENABLED),
58                 userId = userTracker.userId,
59             )
60             .onStart { emit(Unit) }
61             .map { getLockscreenWeatherEnabled() }
62             .stateIn(
63                 scope = applicationScope,
64                 started = SharingStarted.WhileSubscribed(),
65                 initialValue = getLockscreenWeatherEnabled()
66             )
67 
68     override fun setBcSmartspaceVisibility(visibility: Int) {
69         _bcSmartspaceVisibility.value = visibility
70     }
71 
72     private fun getLockscreenWeatherEnabled(): Boolean {
73         return secureSettings.getIntForUser(
74             Settings.Secure.LOCK_SCREEN_WEATHER_ENABLED,
75             1,
76             userTracker.userId
77         ) == 1
78     }
79 }
80