1 /*
2  * Copyright (C) 2024 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.camera.data.repository
18 
19 import android.os.UserHandle
20 import android.provider.Settings
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.dagger.qualifiers.Background
24 import com.android.systemui.util.settings.SecureSettings
25 import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
26 import javax.inject.Inject
27 import kotlin.coroutines.CoroutineContext
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.flow.SharingStarted
30 import kotlinx.coroutines.flow.StateFlow
31 import kotlinx.coroutines.flow.flowOn
32 import kotlinx.coroutines.flow.map
33 import kotlinx.coroutines.flow.onStart
34 import kotlinx.coroutines.flow.stateIn
35 
36 interface CameraAutoRotateRepository {
37     /** @return true if camera auto rotate setting is enabled */
isCameraAutoRotateSettingEnablednull38     fun isCameraAutoRotateSettingEnabled(userHandle: UserHandle): StateFlow<Boolean>
39 }
40 
41 @SysUISingleton
42 class CameraAutoRotateRepositoryImpl
43 @Inject
44 constructor(
45     private val secureSettings: SecureSettings,
46     @Background private val bgCoroutineContext: CoroutineContext,
47     @Application private val applicationScope: CoroutineScope,
48 ) : CameraAutoRotateRepository {
49     private val userMap = mutableMapOf<Int, StateFlow<Boolean>>()
50 
51     override fun isCameraAutoRotateSettingEnabled(userHandle: UserHandle): StateFlow<Boolean> {
52         return userMap.getOrPut(userHandle.identifier) {
53             secureSettings
54                 .observerFlow(userHandle.identifier, Settings.Secure.CAMERA_AUTOROTATE)
55                 .map { isAutoRotateSettingEnabled(userHandle.identifier) }
56                 .onStart { emit(isAutoRotateSettingEnabled(userHandle.identifier)) }
57                 .flowOn(bgCoroutineContext)
58                 .stateIn(applicationScope, SharingStarted.WhileSubscribed(), false)
59         }
60     }
61 
62     private fun isAutoRotateSettingEnabled(userId: Int) =
63         secureSettings.getIntForUser(SETTING_NAME, DISABLED, userId) == ENABLED
64 
65     private companion object {
66         const val SETTING_NAME = Settings.Secure.CAMERA_AUTOROTATE
67         const val DISABLED = 0
68         const val ENABLED = 1
69     }
70 }
71