diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index a9af2d4e29c..908529ef9a9 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -35,6 +35,10 @@ android:title="@*android:string/bugreport_title" android:dialogTitle="@*android:string/bugreport_title" /> + + + + diff --git a/src/com/android/settings/development/AutomaticSystemServerHeapDumpPreferenceController.java b/src/com/android/settings/development/AutomaticSystemServerHeapDumpPreferenceController.java new file mode 100644 index 00000000000..aa76bb8f14a --- /dev/null +++ b/src/com/android/settings/development/AutomaticSystemServerHeapDumpPreferenceController.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development; + +import android.content.Context; +import android.os.Build; +import android.os.UserManager; +import android.provider.Settings; + +import androidx.preference.Preference; +import androidx.preference.SwitchPreference; + +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.development.DeveloperOptionsPreferenceController; + +public class AutomaticSystemServerHeapDumpPreferenceController extends + DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, + PreferenceControllerMixin { + + private static final String KEY_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS = + "automatic_system_server_heap_dumps"; + + private static final int SETTING_VALUE_OFF = 0; + private static final int SETTING_VALUE_ON = 1; + + private final UserManager mUserManager; + private final boolean mIsConfigEnabled; + + public AutomaticSystemServerHeapDumpPreferenceController(Context context) { + super(context); + mIsConfigEnabled = context.getResources().getBoolean( + com.android.internal.R.bool.config_debugEnableAutomaticSystemServerHeapDumps); + mUserManager = context.getSystemService(UserManager.class); + } + + @Override + public boolean isAvailable() { + return Build.IS_DEBUGGABLE && mIsConfigEnabled + && !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); + } + + @Override + public String getPreferenceKey() { + return KEY_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS; + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final boolean isEnabled = (Boolean) newValue; + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, + isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); + return true; + } + + @Override + public void updateState(Preference preference) { + final int mode = Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, SETTING_VALUE_ON); + ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); + } + + @Override + protected void onDeveloperOptionsSwitchDisabled() { + super.onDeveloperOptionsSwitchDisabled(); + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, SETTING_VALUE_OFF); + ((SwitchPreference) mPreference).setChecked(false); + } +} diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java index adc23a811ab..e9fc7596d44 100644 --- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java +++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java @@ -404,6 +404,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra final List controllers = new ArrayList<>(); controllers.add(new MemoryUsagePreferenceController(context)); controllers.add(new BugReportPreferenceController(context)); + controllers.add(new SystemServerHeapDumpPreferenceController(context)); controllers.add(new LocalBackupPasswordPreferenceController(context)); controllers.add(new StayAwakePreferenceController(context, lifecycle)); controllers.add(new HdcpCheckingPreferenceController(context)); @@ -418,6 +419,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra controllers.add(new ClearAdbKeysPreferenceController(context, fragment)); controllers.add(new LocalTerminalPreferenceController(context)); controllers.add(new BugReportInPowerPreferenceController(context)); + controllers.add(new AutomaticSystemServerHeapDumpPreferenceController(context)); controllers.add(new MockLocationAppPreferenceController(context, fragment)); controllers.add(new DebugViewAttributesPreferenceController(context)); controllers.add(new SelectDebugAppPreferenceController(context, fragment)); diff --git a/src/com/android/settings/development/SystemServerHeapDumpPreferenceController.java b/src/com/android/settings/development/SystemServerHeapDumpPreferenceController.java new file mode 100644 index 00000000000..599162e13f4 --- /dev/null +++ b/src/com/android/settings/development/SystemServerHeapDumpPreferenceController.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.development; + +import android.app.ActivityManager; +import android.content.Context; +import android.os.Build; +import android.os.Handler; +import android.os.RemoteException; +import android.os.UserManager; +import android.util.Log; +import android.widget.Toast; + +import androidx.preference.Preference; + +import com.android.settings.R; +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.development.DeveloperOptionsPreferenceController; + +public class SystemServerHeapDumpPreferenceController extends DeveloperOptionsPreferenceController + implements PreferenceControllerMixin { + + private static final String KEY_SYSTEM_SERVER_HEAP_DUMP = "system_server_heap_dump"; + + /** How long to keep the preference disabled before re-enabling. */ + private static final long ENABLE_TIMEOUT_MILLIS = 5000L; + + private final UserManager mUserManager; + + private Handler mHandler; + + public SystemServerHeapDumpPreferenceController(Context context) { + super(context); + + mUserManager = context.getSystemService(UserManager.class); + mHandler = new Handler(); + } + + @Override + public boolean isAvailable() { + return Build.IS_DEBUGGABLE + && !mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES); + } + + @Override + public String getPreferenceKey() { + return KEY_SYSTEM_SERVER_HEAP_DUMP; + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (!KEY_SYSTEM_SERVER_HEAP_DUMP.equals(preference.getKey())) { + return false; + } + try { + // Temporarily disable the preference so the user doesn't start two dumps in a row. + preference.setEnabled(false); + Toast.makeText(mContext, R.string.capturing_system_heap_dump_message, + Toast.LENGTH_SHORT).show(); + ActivityManager.getService().requestSystemServerHeapDump(); + mHandler.postDelayed(() -> preference.setEnabled(true), ENABLE_TIMEOUT_MILLIS); + return true; + } catch (RemoteException e) { + Log.e(TAG, "error taking system heap dump", e); + Toast.makeText(mContext, R.string.error_capturing_system_heap_dump_message, + Toast.LENGTH_SHORT).show(); + } + return false; + } +}