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.settings.system
18 
19 import android.app.settings.SettingsEnums
20 import android.content.Context
21 import android.os.Build
22 import android.os.UserManager
23 import android.provider.Settings
24 import androidx.annotation.VisibleForTesting
25 import androidx.compose.runtime.Composable
26 import androidx.compose.runtime.getValue
27 import androidx.compose.ui.graphics.vector.ImageVector
28 import androidx.compose.ui.res.stringResource
29 import androidx.compose.ui.res.vectorResource
30 import androidx.lifecycle.compose.collectAsStateWithLifecycle
31 import com.android.settings.R
32 import com.android.settings.core.SubSettingLauncher
33 import com.android.settings.development.DevelopmentSettingsDashboardFragment
34 import com.android.settings.spa.preference.ComposePreferenceController
35 import com.android.settingslib.spa.widget.preference.PreferenceModel
36 import com.android.settingslib.spa.widget.ui.SettingsIcon
37 import com.android.settingslib.spaprivileged.framework.common.userManager
38 import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
39 import com.android.settingslib.spaprivileged.settingsprovider.settingsGlobalBooleanFlow
40 import com.android.settingslib.spaprivileged.template.preference.RestrictedPreference
41 
42 class DeveloperOptionsController(context: Context, preferenceKey: String) :
43     ComposePreferenceController(context, preferenceKey) {
44 
getAvailabilityStatusnull45     override fun getAvailabilityStatus() =
46         if (mContext.userManager.isAdminUser) AVAILABLE
47         else DISABLED_FOR_USER
48 
49     private val isDevelopmentSettingsEnabledFlow = context.settingsGlobalBooleanFlow(
50         name = Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
51         defaultValue = Build.IS_ENG,
52     )
53 
54     @Composable
55     override fun Content() {
56         val isDevelopmentSettingsEnabled by isDevelopmentSettingsEnabledFlow
57             .collectAsStateWithLifecycle(initialValue = false)
58         if (isDevelopmentSettingsEnabled) {
59             DeveloperOptionsPreference()
60         }
61     }
62 
63     @VisibleForTesting
64     @Composable
DeveloperOptionsPreferencenull65     fun DeveloperOptionsPreference() {
66         RestrictedPreference(
67             model = object : PreferenceModel {
68                 override val title =
69                     stringResource(com.android.settingslib.R.string.development_settings_title)
70                 override val icon = @Composable {
71                     SettingsIcon(ImageVector.vectorResource(R.drawable.ic_settings_development))
72                 }
73                 override val onClick = {
74                     SubSettingLauncher(mContext).apply {
75                         setDestination(DevelopmentSettingsDashboardFragment::class.qualifiedName)
76                         setSourceMetricsCategory(SettingsEnums.SETTINGS_SYSTEM_CATEGORY)
77                     }.launch()
78                 }
79             },
80             restrictions = Restrictions(keys = listOf(UserManager.DISALLOW_DEBUGGING_FEATURES)),
81         )
82     }
83 }
84