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

Commit 160fb766 authored by Jing Ji's avatar Jing Ji Committed by Android (Google) Code Review
Browse files

Merge "Revert "Kill process if it's cached & idle and in forced-app-standby mode"" into sc-dev

parents d41a4940 45360168
Loading
Loading
Loading
Loading
+8 −18
Original line number Diff line number Diff line
@@ -25,29 +25,19 @@ public interface AppStateTracker {
    String TAG = "AppStateTracker";

    /**
     * Register a {@link ForcedAppStandbyListener} to listen for forced-app-standby changes that
     * should affect services etc.
     * Register a {@link ServiceStateListener} to listen for forced-app-standby changes that should
     * affect services.
     */
    void addForcedAppStandbyListener(@NonNull ForcedAppStandbyListener listener);
    void addServiceStateListener(@NonNull ServiceStateListener listener);

    /**
     * @return {code true} if the given UID/package has been in forced app standby mode.
     * A listener to listen to forced-app-standby changes that should affect services.
     */
    boolean isAppInForcedAppStandby(int uid, @NonNull String packageName);

    /**
     * A listener to listen to forced-app-standby changes that should affect services etc.
     */
    interface ForcedAppStandbyListener {
        /**
         * Called when an app goes in/out of forced app standby.
         */
        void updateForceAppStandbyForUidPackage(int uid, String packageName, boolean standby);

    interface ServiceStateListener {
        /**
         * Called when all apps' forced-app-standby states need to be re-evaluated, due to
         * enable/disable certain feature flags.
         * Called when an app goes into forced app standby and its foreground
         * services need to be removed from that state.
         */
        void updateForcedAppStandbyForAllApps();
        void stopForegroundServicesForUidPackage(int uid, String packageName);
    }
}
+10 −85
Original line number Diff line number Diff line
@@ -60,10 +60,8 @@ import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * Class to keep track of the information related to "force app standby", which includes:
@@ -162,46 +160,16 @@ public class AppStateTrackerImpl implements AppStateTracker {
    @GuardedBy("mLock")
    boolean mForcedAppStandbyEnabled;

    /**
     * A lock-free set of (uid, packageName) pairs in forced app standby mode.
     *
     * <p>
     * It's bascially shadowing the {@link #mRunAnyRestrictedPackages} together with
     * the {@link #mForcedAppStandbyEnabled} and the {@link #mForceAllAppsStandby} - mutations on
     * them would result in copy-on-write.
     *
     * Note: when {@link #mForcedAppStandbyEnabled} is {@code false}, it'll be set to an empty set.
     *       when {@link #mForceAllAppsStandby} is {@code true}, it'll be set to null;
     * </p>
     */
    volatile Set<Pair<Integer, String>> mForcedAppStandbyUidPackages = Collections.emptySet();

    @Override
    public void addForcedAppStandbyListener(@NonNull ForcedAppStandbyListener listener) {
    public void addServiceStateListener(@NonNull ServiceStateListener listener) {
        addListener(new Listener() {
            @Override
            public void updateForceAppStandbyForUidPackage(int uid, String packageName,
                    boolean standby) {
                listener.updateForceAppStandbyForUidPackage(uid, packageName, standby);
            }

            @Override
            public void updateForcedAppStandbyForAllApps() {
                listener.updateForcedAppStandbyForAllApps();
            public void stopForegroundServicesForUidPackage(int uid, String packageName) {
                listener.stopForegroundServicesForUidPackage(uid, packageName);
            }
        });
    }

    @Override
    public boolean isAppInForcedAppStandby(int uid, @NonNull String packageName) {
        final Set<Pair<Integer, String>> fasUidPkgs = mForcedAppStandbyUidPackages;
        if (fasUidPkgs == null) {
            // Meaning the mForceAllAppsStandby is true.
            return true;
        }
        return fasUidPkgs.contains(Pair.create(uid, packageName));
    }

    interface Stats {
        int UID_FG_STATE_CHANGED = 0;
        int UID_ACTIVE_STATE_CHANGED = 1;
@@ -265,7 +233,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
                        return;
                    }
                    mForcedAppStandbyEnabled = enabled;
                    updateForcedAppStandbyUidPackagesLocked();
                    if (DEBUG) {
                        Slog.d(TAG, "Forced app standby feature flag changed: "
                                + mForcedAppStandbyEnabled);
@@ -310,11 +277,7 @@ public class AppStateTrackerImpl implements AppStateTracker {
            if (!sender.isRunAnyInBackgroundAppOpsAllowed(uid, packageName)) {
                Slog.v(TAG, "Package " + packageName + "/" + uid
                        + " toggled into fg service restriction");
                updateForceAppStandbyForUidPackage(uid, packageName, true);
            } else {
                Slog.v(TAG, "Package " + packageName + "/" + uid
                        + " toggled out of fg service restriction");
                updateForceAppStandbyForUidPackage(uid, packageName, false);
                stopForegroundServicesForUidPackage(uid, packageName);
            }
        }

@@ -379,7 +342,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
        private void onForceAllAppsStandbyChanged(AppStateTrackerImpl sender) {
            updateAllJobs();
            updateAllAlarms();
            updateForcedAppStandbyForAllApps();
        }

        /**
@@ -404,17 +366,10 @@ public class AppStateTrackerImpl implements AppStateTracker {
        }

        /**
         * Called when an app goes in/out of forced app standby.
         * Called when an app goes into forced app standby and its foreground
         * services need to be removed from that state.
         */
        public void updateForceAppStandbyForUidPackage(int uid, String packageName,
                boolean standby) {
        }

        /**
         * Called when all apps' forced-app-standby states need to be re-evaluated due to changes of
         * feature flags such as {@link #mForcedAppStandbyEnabled} or {@link #mForceAllAppsStandby}.
         */
        public void updateForcedAppStandbyForAllApps() {
        public void stopForegroundServicesForUidPackage(int uid, String packageName) {
        }

        /**
@@ -483,13 +438,10 @@ public class AppStateTrackerImpl implements AppStateTracker {
                        final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
                        // No need to notify for state change as all the alarms and jobs should be
                        // removed too.
                        synchronized (mLock) {
                        mExemptedBucketPackages.remove(userId, pkgName);
                        mRunAnyRestrictedPackages.remove(Pair.create(uid, pkgName));
                            updateForcedAppStandbyUidPackagesLocked();
                        mActiveUids.delete(uid);
                    }
                    }
                    break;
            }
        }
@@ -628,29 +580,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
                }
            }
        }
        updateForcedAppStandbyUidPackagesLocked();
    }

    /**
     * Update the {@link #mForcedAppStandbyUidPackages} upon mutations on
     * {@link #mRunAnyRestrictedPackages}, {@link #mForcedAppStandbyEnabled} or
     * {@link #mForceAllAppsStandby}.
     */
    @GuardedBy("mLock")
    private void updateForcedAppStandbyUidPackagesLocked() {
        if (!mForcedAppStandbyEnabled) {
            mForcedAppStandbyUidPackages = Collections.emptySet();
            return;
        }
        if (mForceAllAppsStandby) {
            mForcedAppStandbyUidPackages = null;
            return;
        }
        Set<Pair<Integer, String>> fasUidPkgs = new ArraySet<>();
        for (int i = 0, size = mRunAnyRestrictedPackages.size(); i < size; i++) {
            fasUidPkgs.add(mRunAnyRestrictedPackages.valueAt(i));
        }
        mForcedAppStandbyUidPackages = Collections.unmodifiableSet(fasUidPkgs);
    }

    private void updateForceAllAppStandbyState() {
@@ -672,7 +601,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
            return;
        }
        mForceAllAppsStandby = enable;
        updateForcedAppStandbyUidPackagesLocked();

        mHandler.notifyForceAllAppsStandbyChanged();
    }
@@ -717,7 +645,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
        } else {
            mRunAnyRestrictedPackages.removeAt(index);
        }
        updateForcedAppStandbyUidPackagesLocked();
        return true;
    }

@@ -969,7 +896,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
                        if (unblockAlarms) {
                            l.unblockAllUnrestrictedAlarms();
                        }
                        l.updateForcedAppStandbyForAllApps();
                    }
                    mStatLogger.logDurationStat(
                            Stats.FORCE_APP_STANDBY_FEATURE_FLAG_CHANGED, start);
@@ -1040,7 +966,6 @@ public class AppStateTrackerImpl implements AppStateTracker {
                    mRunAnyRestrictedPackages.removeAt(i);
                }
            }
            updateForcedAppStandbyUidPackagesLocked();
            cleanUpArrayForUser(mActiveUids, removedUserId);
            mExemptedBucketPackages.remove(removedUserId);
        }
+4 −16
Original line number Diff line number Diff line
@@ -328,24 +328,12 @@ public final class ActiveServices {
     * Watch for apps being put into forced app standby, so we can step their fg
     * services down.
     */
    class ForcedStandbyListener implements AppStateTracker.ForcedAppStandbyListener {
    class ForcedStandbyListener implements AppStateTracker.ServiceStateListener {
        @Override
        public void updateForceAppStandbyForUidPackage(int uid, String packageName,
                boolean standby) {
        public void stopForegroundServicesForUidPackage(final int uid, final String packageName) {
            synchronized (mAm) {
                if (standby) {
                stopAllForegroundServicesLocked(uid, packageName);
            }
                mAm.mProcessList.updateForceAppStandbyForUidPackageLocked(
                        uid, packageName, standby);
            }
        }

        @Override
        public void updateForcedAppStandbyForAllApps() {
            synchronized (mAm) {
                mAm.mProcessList.updateForcedAppStandbyForAllAppsLocked();
            }
        }
    }

@@ -530,7 +518,7 @@ public final class ActiveServices {

    void systemServicesReady() {
        AppStateTracker ast = LocalServices.getService(AppStateTracker.class);
        ast.addForcedAppStandbyListener(new ForcedStandbyListener());
        ast.addServiceStateListener(new ForcedStandbyListener());
        mAppWidgetManagerInternal = LocalServices.getService(AppWidgetManagerInternal.class);
        setAllowListWhileInUsePermissionInFgs();
    }
+2 −23
Original line number Diff line number Diff line
@@ -52,8 +52,7 @@ final class ActivityManagerConstants extends ContentObserver {
    private static final String TAG = "ActivityManagerConstants";

    // Key names stored in the settings value.
    static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";

    private static final String KEY_BACKGROUND_SETTLE_TIME = "background_settle_time";
    private static final String KEY_FGSERVICE_MIN_SHOWN_TIME
            = "fgservice_min_shown_time";
    private static final String KEY_FGSERVICE_MIN_REPORT_TIME
@@ -109,10 +108,10 @@ final class ActivityManagerConstants extends ContentObserver {
    static final String KEY_FG_TO_BG_FGS_GRACE_DURATION = "fg_to_bg_fgs_grace_duration";
    static final String KEY_FGS_START_FOREGROUND_TIMEOUT = "fgs_start_foreground_timeout";
    static final String KEY_FGS_ATOM_SAMPLE_RATE = "fgs_atom_sample_rate";
    static final String KEY_KILL_FAS_CACHED_IDLE = "kill_fas_cached_idle";
    static final String KEY_FGS_ALLOW_OPT_OUT = "fgs_allow_opt_out";

    private static final int DEFAULT_MAX_CACHED_PROCESSES = 32;
    private static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60*1000;
    private static final long DEFAULT_FGSERVICE_MIN_SHOWN_TIME = 2*1000;
    private static final long DEFAULT_FGSERVICE_MIN_REPORT_TIME = 3*1000;
    private static final long DEFAULT_FGSERVICE_SCREEN_ON_BEFORE_TIME = 1*1000;
@@ -153,10 +152,6 @@ final class ActivityManagerConstants extends ContentObserver {
    private static final long DEFAULT_FG_TO_BG_FGS_GRACE_DURATION = 5 * 1000;
    private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
    private static final float DEFAULT_FGS_ATOM_SAMPLE_RATE = 1; // 100 %

    static final long DEFAULT_BACKGROUND_SETTLE_TIME = 60 * 1000;
    static final boolean DEFAULT_KILL_FAS_CACHED_IDLE = true;

    /**
     * Same as {@link TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED}
     */
@@ -500,12 +495,6 @@ final class ActivityManagerConstants extends ContentObserver {
     */
    volatile float mFgsAtomSampleRate = DEFAULT_FGS_ATOM_SAMPLE_RATE;

    /**
     * Whether or not to kill apps in force-app-standby state and it's cached, its UID state is
     * idle.
     */
    volatile boolean mKillForceAppStandByAndCachedIdle = DEFAULT_KILL_FAS_CACHED_IDLE;

    /**
     * Whether to allow "opt-out" from the foreground service restrictions.
     * (https://developer.android.com/about/versions/12/foreground-services)
@@ -720,9 +709,6 @@ final class ActivityManagerConstants extends ContentObserver {
                            case KEY_FGS_ATOM_SAMPLE_RATE:
                                updateFgsAtomSamplePercent();
                                break;
                            case KEY_KILL_FAS_CACHED_IDLE:
                                updateKillFasCachedIdle();
                                break;
                            case KEY_FGS_ALLOW_OPT_OUT:
                                updateFgsAllowOptOut();
                                break;
@@ -1065,13 +1051,6 @@ final class ActivityManagerConstants extends ContentObserver {
                DEFAULT_FGS_ATOM_SAMPLE_RATE);
    }

    private void updateKillFasCachedIdle() {
        mKillForceAppStandByAndCachedIdle = DeviceConfig.getBoolean(
                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
                KEY_KILL_FAS_CACHED_IDLE,
                DEFAULT_KILL_FAS_CACHED_IDLE);
    }

    private void updateFgsAllowOptOut() {
        mFgsAllowOptOut = DeviceConfig.getBoolean(
                DeviceConfig.NAMESPACE_ACTIVITY_MANAGER,
+0 −4
Original line number Diff line number Diff line
@@ -14382,10 +14382,6 @@ public class ActivityManagerService extends IActivityManager.Stub
        final int capability = uidRec != null ? uidRec.getSetCapability() : 0;
        final boolean ephemeral = uidRec != null ? uidRec.isEphemeral() : isEphemeralLocked(uid);
        if (uidRec != null && uidRec.isIdle() && (change & UidRecord.CHANGE_IDLE) != 0) {
            mProcessList.killAppIfForceStandbyAndCachedIdleLocked(uidRec);
        }
        if (uidRec != null && !uidRec.isIdle() && (change & UidRecord.CHANGE_GONE) != 0) {
            // If this uid is going away, and we haven't yet reported it is gone,
            // then do so now.
Loading