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