• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.flow.Flow
32 import kotlinx.coroutines.flow.flowOn
33 import kotlinx.coroutines.flow.mapLatest
34 
35 /** Checks whether the current user is restricted to change the brightness ([RESTRICTION]) */
36 interface BrightnessPolicyRepository {
37 
38     /**
39      * Indicates whether the current user is restricted to change the brightness. As there is no way
40      * to determine when a restriction has been added/removed. This value may be fetched eagerly and
41      * not updated (unless the user changes) per flow.
42      */
43     val restrictionPolicy: Flow<PolicyRestriction>
44 
45     companion object {
46         const val RESTRICTION = UserManager.DISALLOW_CONFIG_BRIGHTNESS
47     }
48 }
49 
50 @SysUISingleton
51 class BrightnessPolicyRepositoryImpl
52 @Inject
53 constructor(
54     userRepository: UserRepository,
55     private val userRestrictionChecker: UserRestrictionChecker,
56     @Application private val applicationContext: Context,
57     @Background private val backgroundDispatcher: CoroutineDispatcher,
58 ) : BrightnessPolicyRepository {
59     override val restrictionPolicy =
60         userRepository.selectedUserInfo
usernull61             .mapLatest { user ->
62                 userRestrictionChecker
63                     .checkIfRestrictionEnforced(
64                         applicationContext,
65                         BrightnessPolicyRepository.RESTRICTION,
66                         user.id
67                     )
68                     ?.let { PolicyRestriction.Restricted(it) }
69                     ?: if (
70                         enforceBrightnessBaseUserRestriction() &&
71                             userRestrictionChecker.hasBaseUserRestriction(
72                                 applicationContext,
73                                 UserManager.DISALLOW_CONFIG_BRIGHTNESS,
74                                 user.id
75                             )
76                     ) {
77                         PolicyRestriction.Restricted(RestrictedLockUtils.EnforcedAdmin())
78                     } else {
79                         PolicyRestriction.NoRestriction
80                     }
81             }
82             .flowOn(backgroundDispatcher)
83 }
84