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

Commit bc4508a9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I21e46b3f,I1d642980 into main

* changes:
  psc: Move almost perceptible service state
  am: Move OomAdjuster.initSettings() logic to ActivityManagerService
parents 13be9156 18d54421
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -19516,6 +19516,25 @@ public class ActivityManagerService extends IActivityManager.Stub
        processesToUnfreeze.clear();
    }
    /**
     * Sets up a broadcast receiver to handle user switch events, which triggers prewarming of
     * services.
     */
    void setupServicePrewarmingOnUserSwitch() {
        if (!mConstants.KEEP_WARMING_SERVICES.isEmpty()) {
            final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
            final BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    synchronized (ActivityManagerService.this) {
                        mOomAdjuster.prewarmServicesIfNecessary();
                    }
                }
            };
            mContext.registerReceiverForAllUsers(receiver, filter, null, mHandler);
        }
    }
    /**
     * Resets the state of the {@link com.android.server.am.AppErrors} instance.
     * This is intended for testing within the CTS only and is protected by
+1 −1
Original line number Diff line number Diff line
@@ -1381,7 +1381,7 @@ public class ContentProviderHelper {
        new DevelopmentSettingsObserver(); // init to observe developer settings enable/disable
        SettingsToPropertiesMapper.start(mService.mContext.getContentResolver());
        mService.getCachedAppOptimizer().init();
        mService.mOomAdjuster.initSettings();
        mService.setupServicePrewarmingOnUserSwitch();

        // Now that the settings provider is published we can consider sending in a rescue party.
        CrashRecoveryAdaptor.rescuePartyOnSettingsProviderPublished(mService.mContext);
+1 −19
Original line number Diff line number Diff line
@@ -123,11 +123,8 @@ import android.app.ActivityManager;
import android.app.ActivityManagerInternal.OomAdjReason;
import android.app.ApplicationExitInfo;
import android.app.usage.UsageEvents;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.ServiceInfo;
import android.net.NetworkPolicyManager;
@@ -548,26 +545,11 @@ public abstract class OomAdjuster {
                group, app));
    }

    void initSettings() {
        if (mService.mConstants.KEEP_WARMING_SERVICES.size() > 0) {
            final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_SWITCHED);
            mService.mContext.registerReceiverForAllUsers(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    synchronized (mService) {
                        handleUserSwitchedLocked();
                    }
                }
            }, filter, null, mService.mHandler);
        }
    }

    /**
     * Update the keep-warming service flags upon user switches
     */
    @VisibleForTesting
    @GuardedBy("mService")
    void handleUserSwitchedLocked() {
    public void prewarmServicesIfNecessary() {
        mProcessList.forEachLruProcessesLOSP(false,
                this::updateKeepWarmIfNecessaryForProcessLocked);
    }
+8 −26
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.util.ArraySet;

import com.android.internal.annotations.GuardedBy;
import com.android.server.am.psc.ProcessServiceRecordInternal;
import com.android.server.am.psc.ServiceRecordInternal;
import com.android.server.wm.WindowProcessController;

import java.io.PrintWriter;
@@ -61,8 +62,8 @@ final class ProcessServiceRecord extends ProcessServiceRecordInternal {
    private boolean mHasTopStartedAlmostPerceptibleServices;

    /**
     * The latest value of {@link ServiceRecord#lastTopAlmostPerceptibleBindRequestUptimeMs} among
     * the currently running services.
     * The latest value of {@link ServiceRecord#getLastTopAlmostPerceptibleBindRequestUptimeMs()}
     * among the currently running services.
     */
    private long mLastTopStartedAlmostPerceptibleBindRequestUptimeMs;

@@ -250,35 +251,16 @@ final class ProcessServiceRecord extends ProcessServiceRecordInternal {
        mHasTopStartedAlmostPerceptibleServices = false;
        mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = 0;
        for (int s = mServices.size() - 1; s >= 0; --s) {
            final ServiceRecord sr = mServices.valueAt(s);
            final ServiceRecordInternal sr = mServices.valueAt(s);
            mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = Math.max(
                    mLastTopStartedAlmostPerceptibleBindRequestUptimeMs,
                    sr.lastTopAlmostPerceptibleBindRequestUptimeMs);
                    sr.getLastTopAlmostPerceptibleBindRequestUptimeMs());
            if (!mHasTopStartedAlmostPerceptibleServices && isAlmostPerceptible(sr)) {
                mHasTopStartedAlmostPerceptibleServices = true;
            }
        }
    }

    private boolean isAlmostPerceptible(ServiceRecord record) {
        if (record.lastTopAlmostPerceptibleBindRequestUptimeMs <= 0) {
            return false;
        }
        final ArrayMap<IBinder, ArrayList<ConnectionRecord>> serviceConnections =
                record.getConnections();
        for (int m = serviceConnections.size() - 1; m >= 0; --m) {
            final ArrayList<ConnectionRecord> clist = serviceConnections.valueAt(m);

            for (int c = clist.size() - 1; c >= 0; --c) {
                final ConnectionRecord cr = clist.get(c);
                if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) {
                    return true;
                }
            }
        }
        return false;
    }

    boolean hasTopStartedAlmostPerceptibleServices() {
        return mHasTopStartedAlmostPerceptibleServices
                || (mLastTopStartedAlmostPerceptibleBindRequestUptimeMs > 0
@@ -315,10 +297,10 @@ final class ProcessServiceRecord extends ProcessServiceRecordInternal {
            mApp.getWindowProcessController().onServiceStarted(record.serviceInfo);
            updateHostingComonentTypeForBindingsLocked();
        }
        if (record.lastTopAlmostPerceptibleBindRequestUptimeMs > 0) {
        if (record.getLastTopAlmostPerceptibleBindRequestUptimeMs() > 0) {
            mLastTopStartedAlmostPerceptibleBindRequestUptimeMs = Math.max(
                    mLastTopStartedAlmostPerceptibleBindRequestUptimeMs,
                    record.lastTopAlmostPerceptibleBindRequestUptimeMs);
                    record.getLastTopAlmostPerceptibleBindRequestUptimeMs());
            if (!mHasTopStartedAlmostPerceptibleServices) {
                mHasTopStartedAlmostPerceptibleServices = isAlmostPerceptible(record);
            }
@@ -334,7 +316,7 @@ final class ProcessServiceRecord extends ProcessServiceRecordInternal {
     */
    boolean stopService(ServiceRecord record) {
        final boolean removed = mServices.remove(record);
        if (record.lastTopAlmostPerceptibleBindRequestUptimeMs > 0) {
        if (record.getLastTopAlmostPerceptibleBindRequestUptimeMs() > 0) {
            updateHasTopStartedAlmostPerceptibleServices();
        }
        if (removed) {
+3 −3
Original line number Diff line number Diff line
@@ -869,10 +869,10 @@ public class ProcessStateController {
     * {@link android.content.Context.BIND_ALMOST_PERCEPTIBLE}
     */
    @GuardedBy("mLock")
    public void setLastTopAlmostPerceptibleBindRequest(@NonNull ServiceRecord sr,
    public void setLastTopAlmostPerceptibleBindRequest(@NonNull ServiceRecordInternal sr,
            long lastTopAlmostPerceptibleBindRequestUptimeMs) {
        sr.lastTopAlmostPerceptibleBindRequestUptimeMs =
                lastTopAlmostPerceptibleBindRequestUptimeMs;
        sr.setLastTopAlmostPerceptibleBindRequestUptimeMs(
                lastTopAlmostPerceptibleBindRequestUptimeMs);
    }

    /**
Loading