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.os.Build; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 import android.provider.Settings; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.Logger; 29 import com.android.settingslib.development.DevelopmentSettingsEnabler; 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 private static final boolean DEBUG = Build.isDebuggable(); 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 boolean shouldEnable = showDeveloperOptions(context) && enable; 50 LOG.i("Enabling developer options module: " 51 + getDeveloperOptionsModule(context).flattenToString() 52 + " Currently enabled: " + isDevelopmentSettingsEnabled(context) 53 + " Requested value: " + enable 54 + " Should enable: " + shouldEnable); 55 DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(context, shouldEnable); 56 } 57 58 /** 59 * Checks that the development settings should be enabled. Returns true if global toggle is set, 60 * debugging is allowed for user, and the user is an admin user. 61 */ isDevelopmentSettingsEnabled(Context context)62 public static boolean isDevelopmentSettingsEnabled(Context context) { 63 return DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(context); 64 } 65 66 /** Checks whether the device is provisioned or not. */ isDeviceProvisioned(Context context)67 public static boolean isDeviceProvisioned(Context context) { 68 return Settings.Global.getInt(context.getContentResolver(), 69 Settings.Global.DEVICE_PROVISIONED, 0) != 0; 70 } 71 getDeveloperOptionsModule(Context context)72 private static ComponentName getDeveloperOptionsModule(Context context) { 73 return ComponentName.unflattenFromString( 74 context.getString(R.string.config_dev_options_module)); 75 } 76 showDeveloperOptions(Context context)77 private static boolean showDeveloperOptions(Context context) { 78 UserManager userManager = UserManager.get(context); 79 boolean showDev = !ActivityManager.isUserAMonkey(); 80 boolean isAdmin = userManager.isAdminUser(); 81 if (UserHandle.MU_ENABLED && !isAdmin) { 82 showDev = false; 83 } 84 85 if (DEBUG) { 86 LOG.d("showDeveloperOptions: " + " isUserAMonkey: " + ActivityManager.isUserAMonkey() 87 + " isAdmin: " + isAdmin + " UserHandle.MU_ENABLED: " + UserHandle.MU_ENABLED 88 + " showDev: " + showDev); 89 } 90 return showDev; 91 } 92 93 } 94