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

Commit 18d54421 authored by Chih-Yu Huang's avatar Chih-Yu Huang
Browse files

psc: Move almost perceptible service state

This CL moves the `mLastTopAlmostPerceptibleBindRequestUptimeMs` field
and the `isAlmostPerceptible` logic from `ServiceRecord` and
`ProcessServiceRecord` to their internal classes respectively.

Bug: 442284373
Test: m services.core
Test: atest MockingOomAdjusterTests OomAdjusterTests
Test: atest FrameworksServicesTestsRavenwood_ProcessStateController
Flag: EXEMPT PURE_REFACTOR
Change-Id: I21e46b3fdf563cf0a47278c28bafc84d12d0dfc2
parent 2dbe23f1
Loading
Loading
Loading
Loading
+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);
    }

    /**
+0 −6
Original line number Diff line number Diff line
@@ -189,12 +189,6 @@ final class ServiceRecord extends ServiceRecordInternal implements ComponentName
     */
    boolean mIsNativeIsolated; // is the service a native-only isolated service?

    /**
     * The last time (in uptime timebase) a bind request was made with BIND_ALMOST_PERCEPTIBLE for
     * this service while on TOP.
     */
    long lastTopAlmostPerceptibleBindRequestUptimeMs;

    // any current binding to this service has BIND_ALLOW_BACKGROUND_ACTIVITY_STARTS flag?
    private boolean mIsAllowedBgActivityStartsByBinding;
    // used to clean up the state of mIsAllowedBgActivityStartsByStart after a timeout
+20 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.server.am.psc;

import android.content.Context;

import java.util.ArrayList;

/**
 * Base class for internal process service record state information.
 * This class provides common fields and methods for managing service-related properties
@@ -99,4 +103,20 @@ public abstract class ProcessServiceRecordInternal {

    /** Checks if this process has any foreground services (even timed-out short-FGS) */
    public abstract boolean hasForegroundServices();

    protected static boolean isAlmostPerceptible(ServiceRecordInternal record) {
        if (record.getLastTopAlmostPerceptibleBindRequestUptimeMs() <= 0) {
            return false;
        }
        for (int i = record.getConnectionsSize() - 1; i >= 0; --i) {
            final ArrayList<? extends ConnectionRecordInternal> clist = record.getConnectionAt(i);
            for (int j = clist.size() - 1; j >= 0; --j) {
                final ConnectionRecordInternal cr = clist.get(j);
                if (cr.hasFlag(Context.BIND_ALMOST_PERCEPTIBLE)) {
                    return true;
                }
            }
        }
        return false;
    }
}
+14 −0
Original line number Diff line number Diff line
@@ -45,6 +45,12 @@ public abstract class ServiceRecordInternal extends Binder {
    /** The bitmask of foreground service types declared for this service. */
    private int mForegroundServiceType;

    /**
     * The last time (in uptime timebase) a bind request was made with BIND_ALMOST_PERCEPTIBLE for
     * this service while on TOP.
     */
    private long mLastTopAlmostPerceptibleBindRequestUptimeMs;

    public ServiceRecordInternal(ComponentName instanceName, long lastActivity) {
        this.instanceName = instanceName;
        mLastActivity = lastActivity;
@@ -89,6 +95,14 @@ public abstract class ServiceRecordInternal extends Binder {
        mForegroundServiceType = foregroundServiceType;
    }

    public long getLastTopAlmostPerceptibleBindRequestUptimeMs() {
        return mLastTopAlmostPerceptibleBindRequestUptimeMs;
    }

    public void setLastTopAlmostPerceptibleBindRequestUptimeMs(
            long lastTopAlmostPerceptibleBindRequestUptimeMs) {
        mLastTopAlmostPerceptibleBindRequestUptimeMs = lastTopAlmostPerceptibleBindRequestUptimeMs;
    }
    /**
     * Checks if this foreground service is allowed to access "while-in-use" permissions
     * (e.g., location, camera, microphone) for capability determination.
Loading