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.settings.development; 18 19 import android.content.Context; 20 import android.os.UserManager; 21 import android.provider.Settings; 22 23 import androidx.annotation.VisibleForTesting; 24 import androidx.preference.Preference; 25 import androidx.preference.SwitchPreference; 26 27 import com.android.settings.core.PreferenceControllerMixin; 28 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 29 30 public class BugReportInPowerPreferenceController extends 31 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, 32 PreferenceControllerMixin { 33 34 private static final String KEY_BUGREPORT_IN_POWER = "bugreport_in_power"; 35 36 @VisibleForTesting 37 static int SETTING_VALUE_ON = 1; 38 @VisibleForTesting 39 static int SETTING_VALUE_OFF = 0; 40 41 private final UserManager mUserManager; 42 BugReportInPowerPreferenceController(Context context)43 public BugReportInPowerPreferenceController(Context context) { 44 super(context); 45 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 46 } 47 48 @Override isAvailable()49 public boolean isAvailable() { 50 return !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); 51 } 52 53 @Override getPreferenceKey()54 public String getPreferenceKey() { 55 return KEY_BUGREPORT_IN_POWER; 56 } 57 58 @Override onPreferenceChange(Preference preference, Object newValue)59 public boolean onPreferenceChange(Preference preference, Object newValue) { 60 final boolean isEnabled = (Boolean) newValue; 61 Settings.Secure.putInt(mContext.getContentResolver(), 62 Settings.Global.BUGREPORT_IN_POWER_MENU, 63 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); 64 return true; 65 } 66 67 @Override updateState(Preference preference)68 public void updateState(Preference preference) { 69 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 70 Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF); 71 ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); 72 } 73 74 @Override onDeveloperOptionsSwitchDisabled()75 protected void onDeveloperOptionsSwitchDisabled() { 76 super.onDeveloperOptionsSwitchDisabled(); 77 Settings.Secure.putInt(mContext.getContentResolver(), 78 Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF); 79 ((SwitchPreference) mPreference).setChecked(false); 80 } 81 } 82