1 /* 2 * Copyright (C) 2019 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.Build; 21 import android.os.UserManager; 22 import android.provider.Settings; 23 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 AutomaticSystemServerHeapDumpPreferenceController extends 31 DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, 32 PreferenceControllerMixin { 33 34 private static final String KEY_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS = 35 "automatic_system_server_heap_dumps"; 36 37 private static final int SETTING_VALUE_OFF = 0; 38 private static final int SETTING_VALUE_ON = 1; 39 40 private final UserManager mUserManager; 41 private final boolean mIsConfigEnabled; 42 AutomaticSystemServerHeapDumpPreferenceController(Context context)43 public AutomaticSystemServerHeapDumpPreferenceController(Context context) { 44 super(context); 45 mIsConfigEnabled = context.getResources().getBoolean( 46 com.android.internal.R.bool.config_debugEnableAutomaticSystemServerHeapDumps); 47 mUserManager = context.getSystemService(UserManager.class); 48 } 49 50 @Override isAvailable()51 public boolean isAvailable() { 52 return Build.IS_DEBUGGABLE && mIsConfigEnabled 53 && !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); 54 } 55 56 @Override getPreferenceKey()57 public String getPreferenceKey() { 58 return KEY_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS; 59 } 60 61 @Override onPreferenceChange(Preference preference, Object newValue)62 public boolean onPreferenceChange(Preference preference, Object newValue) { 63 final boolean isEnabled = (Boolean) newValue; 64 Settings.Secure.putInt(mContext.getContentResolver(), 65 Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, 66 isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); 67 return true; 68 } 69 70 @Override updateState(Preference preference)71 public void updateState(Preference preference) { 72 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 73 Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, SETTING_VALUE_ON); 74 ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); 75 } 76 77 @Override onDeveloperOptionsSwitchDisabled()78 protected void onDeveloperOptionsSwitchDisabled() { 79 super.onDeveloperOptionsSwitchDisabled(); 80 Settings.Secure.putInt(mContext.getContentResolver(), 81 Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, SETTING_VALUE_OFF); 82 ((SwitchPreference) mPreference).setChecked(false); 83 } 84 } 85