Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit afd71d16 authored by Kweku Adams's avatar Kweku Adams
Browse files

Add heap dump options to Developer options.

1. Add a button to capture a system heap dump on demand.
2. Add a toggle to enable/disable automatic system heap dump captures.

Bug: 77490269
Test: manual
Change-Id: I12b41de82f641ae239ea8e48f0180392aca5dbe8
parent ed8b0aeb
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -35,6 +35,10 @@
            android:title="@*android:string/bugreport_title"
            android:dialogTitle="@*android:string/bugreport_title" />

        <Preference
            android:key="system_server_heap_dump"
            android:title="@string/capture_system_heap_dump_title" />

        <Preference
            android:key="local_backup_password"
            android:title="@string/local_backup_password_title"
@@ -151,6 +155,11 @@
            android:title="@string/bugreport_in_power"
            android:summary="@string/bugreport_in_power_summary" />

        <SwitchPreference
            android:key="automatic_system_server_heap_dumps"
            android:title="@string/automatic_system_heap_dump_title"
            android:summary="@string/automatic_system_heap_dump_summary" />

        <Preference android:key="mock_location_app"
                    android:title="@string/mock_location_app" />

+84 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -404,6 +404,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        final List<AbstractPreferenceController> 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));
+84 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}