• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.development.data.repository
18 
19 import android.content.pm.UserInfo
20 import android.os.Build
21 import android.os.UserManager
22 import android.provider.Settings
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.dagger.qualifiers.Background
25 import com.android.systemui.util.kotlin.emitOnStart
26 import com.android.systemui.util.settings.GlobalSettings
27 import com.android.systemui.util.settings.SettingsProxyExt.observerFlow
28 import javax.inject.Inject
29 import kotlinx.coroutines.CoroutineDispatcher
30 import kotlinx.coroutines.flow.Flow
31 import kotlinx.coroutines.flow.flowOn
32 import kotlinx.coroutines.flow.map
33 import kotlinx.coroutines.withContext
34 
35 @SysUISingleton
36 class DevelopmentSettingRepository
37 @Inject
38 constructor(
39     private val globalSettings: GlobalSettings,
40     private val userManager: UserManager,
41     @Background private val backgroundDispatcher: CoroutineDispatcher,
42 ) {
43     private val settingFlow = globalSettings.observerFlow(SETTING)
44 
45     /**
46      * Indicates whether development settings is enabled for this user. The conditions are:
47      * * Setting is enabled (defaults to true in eng builds)
48      * * User is an admin
49      * * User is not restricted from Debugging features.
50      */
isDevelopmentSettingEnablednull51     fun isDevelopmentSettingEnabled(userInfo: UserInfo): Flow<Boolean> {
52         return settingFlow
53             .emitOnStart()
54             .map { checkDevelopmentSettingEnabled(userInfo) }
55             .flowOn(backgroundDispatcher)
56     }
57 
checkDevelopmentSettingEnablednull58     private suspend fun checkDevelopmentSettingEnabled(userInfo: UserInfo): Boolean {
59         val hasUserRestriction =
60             withContext(backgroundDispatcher) {
61                 userManager.hasUserRestrictionForUser(
62                     UserManager.DISALLOW_DEBUGGING_FEATURES,
63                     userInfo.userHandle,
64                 )
65             }
66         val isSettingEnabled =
67             withContext(backgroundDispatcher) {
68                 globalSettings.getInt(SETTING, DEFAULT_ENABLED) != 0
69             }
70         val isAdmin = userInfo.isAdmin
71         return isAdmin && !hasUserRestriction && isSettingEnabled
72     }
73 
74     private companion object {
75         val DEFAULT_ENABLED = if (Build.TYPE == "eng") 1 else 0
76 
77         const val SETTING = Settings.Global.DEVELOPMENT_SETTINGS_ENABLED
78     }
79 }
80