1 /*
<lambda>null2  * 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.brightness.data.repository
18 
19 import android.content.Context
20 import android.os.UserManager
21 import com.android.settingslib.RestrictedLockUtils
22 import com.android.systemui.Flags.enforceBrightnessBaseUserRestriction
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.dagger.qualifiers.Application
25 import com.android.systemui.dagger.qualifiers.Background
26 import com.android.systemui.user.data.repository.UserRepository
27 import com.android.systemui.utils.PolicyRestriction
28 import com.android.systemui.utils.UserRestrictionChecker
29 import javax.inject.Inject
30 import kotlinx.coroutines.CoroutineDispatcher
31 import kotlinx.coroutines.ExperimentalCoroutinesApi
32 import kotlinx.coroutines.flow.Flow
33 import kotlinx.coroutines.flow.flowOn
34 import kotlinx.coroutines.flow.mapLatest
35 
36 /** Checks whether the current user is restricted to change the brightness ([RESTRICTION]) */
37 interface BrightnessPolicyRepository {
38 
39     /**
40      * Indicates whether the current user is restricted to change the brightness. As there is no way
41      * to determine when a restriction has been added/removed. This value may be fetched eagerly and
42      * not updated (unless the user changes) per flow.
43      */
44     val restrictionPolicy: Flow<PolicyRestriction>
45 
46     companion object {
47         const val RESTRICTION = UserManager.DISALLOW_CONFIG_BRIGHTNESS
48     }
49 }
50 
51 @OptIn(ExperimentalCoroutinesApi::class)
52 @SysUISingleton
53 class BrightnessPolicyRepositoryImpl
54 @Inject
55 constructor(
56     userRepository: UserRepository,
57     private val userRestrictionChecker: UserRestrictionChecker,
58     @Application private val applicationContext: Context,
59     @Background private val backgroundDispatcher: CoroutineDispatcher,
60 ) : BrightnessPolicyRepository {
61     override val restrictionPolicy =
62         userRepository.selectedUserInfo
usernull63             .mapLatest { user ->
64                 userRestrictionChecker
65                     .checkIfRestrictionEnforced(
66                         applicationContext,
67                         BrightnessPolicyRepository.RESTRICTION,
68                         user.id
69                     )
70                     ?.let { PolicyRestriction.Restricted(it) }
71                     ?: if (
72                         enforceBrightnessBaseUserRestriction() &&
73                             userRestrictionChecker.hasBaseUserRestriction(
74                                 applicationContext,
75                                 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
76                                 user.id
77                             )
78                     ) {
79                         PolicyRestriction.Restricted(RestrictedLockUtils.EnforcedAdmin())
80                     } else {
81                         PolicyRestriction.NoRestriction
82                     }
83             }
84             .flowOn(backgroundDispatcher)
85 }
86