• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.development;
18 
19 import android.app.ActivityManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.os.Build;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.provider.Settings;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.Logger;
30 
31 /**
32  * A utility to set/check development settings mode.
33  *
34  * <p>Shared logic with {@link com.android.settingslib.development.DevelopmentSettingsEnabler} with
35  * modifications to use CarUserManagerHelper instead of UserManager.
36  */
37 public class DevelopmentSettingsUtil {
38 
39     private static final Logger LOG = new Logger(DevelopmentSettingsUtil.class);
40 
DevelopmentSettingsUtil()41     private DevelopmentSettingsUtil() {
42     }
43 
44     /**
45      * Sets the global toggle for developer settings and sends out a local broadcast to notify other
46      * of this change.
47      */
setDevelopmentSettingsEnabled(Context context, boolean enable)48     public static void setDevelopmentSettingsEnabled(Context context, boolean enable) {
49         Settings.Global.putInt(context.getContentResolver(),
50                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, enable ? 1 : 0);
51 
52         // Enable developer options module.
53         setDeveloperOptionsEnabledState(context, showDeveloperOptions(context));
54     }
55 
56     /**
57      * Checks that the development settings should be enabled. Returns true if global toggle is set,
58      * debugging is allowed for user, and the user is an admin user.
59      */
isDevelopmentSettingsEnabled(Context context, UserManager userManager)60     public static boolean isDevelopmentSettingsEnabled(Context context, UserManager userManager) {
61         boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(),
62                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, Build.IS_ENG ? 1 : 0) != 0;
63         boolean hasRestriction = userManager.hasUserRestriction(
64                 UserManager.DISALLOW_DEBUGGING_FEATURES);
65         boolean isAdmin = userManager.isAdminUser();
66         return isAdmin && !hasRestriction && settingEnabled;
67     }
68 
69     /** Checks whether the device is provisioned or not. */
isDeviceProvisioned(Context context)70     public static boolean isDeviceProvisioned(Context context) {
71         return Settings.Global.getInt(context.getContentResolver(),
72                 Settings.Global.DEVICE_PROVISIONED, 0) != 0;
73     }
74 
75     /** Checks whether the developer options module is enabled. */
isDeveloperOptionsModuleEnabled(Context context)76     public static boolean isDeveloperOptionsModuleEnabled(Context context) {
77         PackageManager pm = context.getPackageManager();
78         ComponentName component = getDeveloperOptionsModule(context);
79         int state = pm.getComponentEnabledSetting(component);
80         return state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
81     }
82 
getDeveloperOptionsModule(Context context)83     private static ComponentName getDeveloperOptionsModule(Context context) {
84         return ComponentName.unflattenFromString(
85                 context.getString(R.string.config_dev_options_module));
86     }
87 
showDeveloperOptions(Context context)88     private static boolean showDeveloperOptions(Context context) {
89         UserManager userManager = UserManager.get(context);
90         boolean showDev = isDevelopmentSettingsEnabled(context, userManager)
91                 && !ActivityManager.isUserAMonkey();
92         boolean isAdmin = userManager.isAdminUser();
93         if (UserHandle.MU_ENABLED && !isAdmin) {
94             showDev = false;
95         }
96 
97         return showDev;
98     }
99 
setDeveloperOptionsEnabledState(Context context, boolean enabled)100     private static void setDeveloperOptionsEnabledState(Context context, boolean enabled) {
101         PackageManager pm = context.getPackageManager();
102         ComponentName component = getDeveloperOptionsModule(context);
103         int state = pm.getComponentEnabledSetting(component);
104         boolean isEnabled = isDeveloperOptionsModuleEnabled(context);
105         LOG.i("Enabling developer options module: " + component.flattenToString()
106                 + " Current state: " + state
107                 + " Currently enabled: " + isEnabled
108                 + " Should enable: " + enabled);
109         if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
110             pm.setComponentEnabledSetting(component, enabled
111                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
112                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
113                     PackageManager.DONT_KILL_APP);
114         }
115     }
116 }
117