1 /* 2 * Copyright (C) 2016 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.ComponentName; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.os.UserManager; 23 import android.provider.Settings; 24 import android.support.v14.preference.SwitchPreference; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.core.PreferenceController; 29 30 public class BugReportInPowerPreferenceController extends PreferenceController { 31 32 private static final String KEY_BUGREPORT_IN_POWER = "bugreport_in_power"; 33 34 private UserManager mUserManager; 35 private SwitchPreference mPreference; 36 BugReportInPowerPreferenceController(Context context)37 public BugReportInPowerPreferenceController(Context context) { 38 super(context); 39 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 40 } 41 42 @Override handlePreferenceTreeClick(Preference preference)43 public boolean handlePreferenceTreeClick(Preference preference) { 44 if (KEY_BUGREPORT_IN_POWER.equals(preference.getKey())) { 45 final SwitchPreference switchPreference = (SwitchPreference) preference; 46 Settings.Secure.putInt(mContext.getContentResolver(), 47 Settings.Global.BUGREPORT_IN_POWER_MENU, 48 switchPreference.isChecked() ? 1 : 0); 49 setBugreportStorageProviderStatus(); 50 return true; 51 } 52 return false; 53 } 54 55 @Override displayPreference(PreferenceScreen screen)56 public void displayPreference(PreferenceScreen screen) { 57 super.displayPreference(screen); 58 if (isAvailable()) { 59 mPreference = (SwitchPreference) screen.findPreference(KEY_BUGREPORT_IN_POWER); 60 } 61 } 62 63 @Override getPreferenceKey()64 public String getPreferenceKey() { 65 return KEY_BUGREPORT_IN_POWER; 66 } 67 68 @Override isAvailable()69 public boolean isAvailable() { 70 return !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); 71 } 72 73 @Override updateState(Preference preference)74 public void updateState(Preference preference) { 75 updatePreference(); 76 } 77 enablePreference(boolean enabled)78 public void enablePreference(boolean enabled) { 79 if (isAvailable()) { 80 mPreference.setEnabled(enabled); 81 } 82 } 83 resetPreference()84 public void resetPreference() { 85 if (mPreference.isChecked()) { 86 mPreference.setChecked(false); 87 handlePreferenceTreeClick(mPreference); 88 } 89 } 90 updatePreference()91 public boolean updatePreference() { 92 if (!isAvailable()) { 93 return false; 94 } 95 final boolean enabled = Settings.Secure.getInt( 96 mContext.getContentResolver(), Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0; 97 mPreference.setChecked(enabled); 98 return enabled; 99 } 100 updateBugreportOptions()101 public void updateBugreportOptions() { 102 if (!isAvailable()) { 103 return; 104 } 105 mPreference.setEnabled(true); 106 setBugreportStorageProviderStatus(); 107 } 108 setBugreportStorageProviderStatus()109 private void setBugreportStorageProviderStatus() { 110 final ComponentName componentName = new ComponentName("com.android.shell", 111 "com.android.shell.BugreportStorageProvider"); 112 final boolean enabled = mPreference.isChecked(); 113 mContext.getPackageManager().setComponentEnabledSetting(componentName, 114 enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 115 : PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 116 0); 117 } 118 119 } 120