Home | History | Annotate | Download | only in development
      1 /*
      2  * Copyright (C) 2017 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.settingslib.development;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.Build;
     22 import android.os.UserManager;
     23 import android.provider.Settings;
     24 import android.support.v4.content.LocalBroadcastManager;
     25 
     26 public class DevelopmentSettingsEnabler {
     27 
     28     public static final String DEVELOPMENT_SETTINGS_CHANGED_ACTION =
     29             "com.android.settingslib.development.DevelopmentSettingsEnabler.SETTINGS_CHANGED";
     30 
     31     private DevelopmentSettingsEnabler() {
     32     }
     33 
     34     public static void setDevelopmentSettingsEnabled(Context context, boolean enable) {
     35         Settings.Global.putInt(context.getContentResolver(),
     36                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, enable ? 1 : 0);
     37         LocalBroadcastManager.getInstance(context)
     38                 .sendBroadcast(new Intent(DEVELOPMENT_SETTINGS_CHANGED_ACTION));
     39     }
     40 
     41     public static boolean isDevelopmentSettingsEnabled(Context context) {
     42         final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
     43         final boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(),
     44                 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
     45                 Build.TYPE.equals("eng") ? 1 : 0) != 0;
     46         final boolean hasRestriction = um.hasUserRestriction(
     47                 UserManager.DISALLOW_DEBUGGING_FEATURES);
     48         final boolean isAdminOrDemo = um.isAdminUser() || um.isDemoUser();
     49         return isAdminOrDemo && !hasRestriction && settingEnabled;
     50     }
     51 }
     52