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

Commit 3703b15c authored by Marco Ballesio's avatar Marco Ballesio Committed by Automerger Merge Worker
Browse files

Merge "Developer option for the app freezer" into rvc-dev am: 97483a97

Change-Id: I6d9de0fbf8fe0bd0b5af3402943aaa97ed481b75
parents 60567839 97483a97
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12066,4 +12066,7 @@
    <string name="no_5g_in_dsds_text" product="device">When using 2 SIMs, this device will be limited to 4G. <annotation id="url">Learn more</annotation>.</string>
    <!-- Help URI, 5G limitation in DSDS condition. [DO NOT TRANSLATE] -->
    <string name="help_uri_5g_dsds" translatable="false"></string>
    <!-- Developer settings: app freezer title [CHAR LIMIT=50]-->
    <string name="cached_apps_freezer">Suspend execution for cached apps</string>
</resources>
+6 −0
Original line number Diff line number Diff line
@@ -584,6 +584,12 @@
            android:title="@string/show_all_anrs"
            android:summary="@string/show_all_anrs_summary" />

        <ListPreference
            android:key="cached_apps_freezer"
            android:title="@string/cached_apps_freezer"
            android:entries="@array/cached_apps_freezer_entries"
            android:entryValues="@array/cached_apps_freezer_values" />

        <SwitchPreference
            android:key="show_notification_channel_warnings"
            android:title="@string/show_notification_channel_warnings"
+134 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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.content.DialogInterface;
import android.os.PowerManager;
import android.os.RemoteException;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.ListPreference;
import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController;


public class CachedAppsFreezerPreferenceController extends DeveloperOptionsPreferenceController
        implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {

    @VisibleForTesting
    private static final String CACHED_APPS_FREEZER_KEY = "cached_apps_freezer";

    private final String[] mListValues;
    private final String[] mListSummaries;

    public CachedAppsFreezerPreferenceController(Context context) {
        super(context);

        mListValues = context.getResources().getStringArray(R.array.cached_apps_freezer_values);
        mListSummaries = context.getResources().getStringArray(
                R.array.cached_apps_freezer_entries);
    }

    @Override
    public boolean isAvailable() {
        boolean available = false;

        try {
            available = ActivityManager.getService().isAppFreezerSupported();
        } catch (RemoteException e) {
            Log.w(TAG, "Unable to obtain freezer support status from ActivityManager");
        }

        return available;
    }

    @Override
    public String getPreferenceKey() {
        return CACHED_APPS_FREEZER_KEY;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        final String currentValue = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.CACHED_APPS_FREEZER_ENABLED);

        if (!newValue.equals(currentValue)) {
            final AlertDialog dialog = new AlertDialog.Builder(mContext)
                    .setMessage(R.string.cached_apps_freezer_reboot_dialog_text)
                    .setPositiveButton(android.R.string.ok, getRebootDialogOkListener(newValue))
                    .setNegativeButton(android.R.string.cancel, getRebootDialogCancelListener())
                    .create();
            dialog.show();
        }

        return true;
    }

    private DialogInterface.OnClickListener getRebootDialogOkListener(Object newValue) {
        return (dialog, which) -> {
            Settings.Global.putString(mContext.getContentResolver(),
                    Settings.Global.CACHED_APPS_FREEZER_ENABLED,
                    newValue.toString());

            updateState(mPreference);

            PowerManager pm = mContext.getSystemService(PowerManager.class);
            pm.reboot(null);
        };
    }

    private DialogInterface.OnClickListener getRebootDialogCancelListener() {
        return (dialog, which) -> {
            updateState(mPreference);
        };
    }

    @Override
    public void updateState(Preference preference) {
        final ListPreference listPreference = (ListPreference) preference;
        final String currentValue = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.CACHED_APPS_FREEZER_ENABLED);

        int index = 0; // Defaults to device default
        for (int i = 0; i < mListValues.length; i++) {
            if (TextUtils.equals(currentValue, mListValues[i])) {
                index = i;
                break;
            }
        }

        listPreference.setValue(mListValues[index]);
        listPreference.setSummary(mListSummaries[index]);
    }

    @Override
    public void onDeveloperOptionsDisabled() {
        super.onDeveloperOptionsDisabled();

        Settings.Global.putString(mContext.getContentResolver(),
                Settings.Global.CACHED_APPS_FREEZER_ENABLED,
                mListValues[0].toString());
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -505,6 +505,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
        controllers.add(new ProfileGpuRenderingPreferenceController(context));
        controllers.add(new KeepActivitiesPreferenceController(context));
        controllers.add(new BackgroundProcessLimitPreferenceController(context));
        controllers.add(new CachedAppsFreezerPreferenceController(context));
        controllers.add(new ShowFirstCrashDialogPreferenceController(context));
        controllers.add(new AppsNotRespondingPreferenceController(context));
        controllers.add(new NotificationChannelWarningsPreferenceController(context));