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

Commit ce7fc422 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Allow clients to register for listening to changes in capabilities.

Clients can register for listening to changes in procstates and if
they are only interested in knowing if the procstate crosses a certain
threshold, they can specify a "cutpoint" and they will only get
a callback if the procstate crosses that "cutpoint" but clients cannot
use this if they are also interested in changes in capabilities. So,
add a way for clients to use the "cutpoint" facility and also listen
to changes in capabilities.

Bug: 177641226
Test: atest services/tests/servicestests/src/com/android/server/am/ActivityManagerServiceTest.java
Test: atest services/tests/servicestests/src/com/android/server/am/UidObserverControllerTest.java
Test: atest ./tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Change-Id: I1198e602ca81382975500b10f930dacfd16919e3
parent ddf368aa
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -794,6 +794,9 @@ public class ActivityManager {
    /** @hide Flag for registerUidObserver: report uid cached state has changed. */
    public static final int UID_OBSERVER_CACHED = 1<<4;

    /** @hide Flag for registerUidObserver: report uid capability has changed. */
    public static final int UID_OBSERVER_CAPABILITY = 1<<5;

    /** @hide Mode for {@link IActivityManager#isAppStartModeDisabled}: normal free-to-run operation. */
    public static final int APP_START_MODE_NORMAL = 0;

+2 −0
Original line number Diff line number Diff line
@@ -33,4 +33,6 @@ enum UidObserverFlag {
    UID_OBSERVER_FLAG_ACTIVE = 4;
    // report uid cached state has changed, original value is 1 << 4
    UID_OBSERVER_FLAG_CACHED = 5;
    // report uid capability has changed, original value is 1 << 5
    UID_OBSERVER_FLAG_CAPABILITY = 6;
}
+1 −0
Original line number Diff line number Diff line
@@ -778,6 +778,7 @@ message UidRecordProto {
        CHANGE_ACTIVE = 2;
        CHANGE_CACHED = 3;
        CHANGE_UNCACHED = 4;
        CHANGE_CAPABILITY = 5;
    }
    repeated Change last_reported_changes = 8;
    optional int32 num_procs = 9;
+3 −0
Original line number Diff line number Diff line
@@ -1328,6 +1328,9 @@ public final class OomAdjuster {
                        || uidRec.getSetProcState() == PROCESS_STATE_NONEXISTENT) {
                    uidChange |= isCached ? UidRecord.CHANGE_CACHED : UidRecord.CHANGE_UNCACHED;
                }
                if (uidRec.getSetCapability() != uidRec.getCurCapability()) {
                    uidChange |= UidRecord.CHANGE_CAPABILITY;
                }
                uidRec.setSetProcState(uidRec.getCurProcState());
                uidRec.setSetCapability(uidRec.getCurCapability());
                uidRec.setSetAllowListed(uidRec.isCurAllowListed());
+23 −11
Original line number Diff line number Diff line
@@ -147,6 +147,9 @@ public class UidObserverController {
        if ((currentChange & UidRecord.CHANGE_GONE) != 0) {
            currentChange &= ~(UidRecord.CHANGE_ACTIVE | UidRecord.CHANGE_CACHED);
        }
        if ((pendingChange & UidRecord.CHANGE_CAPABILITY) != 0) {
            currentChange |= UidRecord.CHANGE_CAPABILITY;
        }
        return currentChange;
    }

@@ -285,12 +288,9 @@ public class UidObserverController {
                        reg.mLastProcStates.delete(item.uid);
                    }
                } else {
                    boolean doReport = false;
                    if ((reg.mWhich & ActivityManager.UID_OBSERVER_PROCSTATE) != 0) {
                        if (DEBUG_UID_OBSERVERS) {
                            Slog.i(TAG_UID_OBSERVERS, "UID CHANGED uid=" + item.uid
                                    + ": " + item.procState + ": " + item.capability);
                        }
                        boolean doReport = true;
                        doReport = true;
                        if (reg.mCutpoint >= ActivityManager.MIN_PROCESS_STATE) {
                            final int lastState = reg.mLastProcStates.get(item.uid,
                                    ActivityManager.PROCESS_STATE_UNKNOWN);
@@ -302,7 +302,15 @@ public class UidObserverController {
                                doReport = item.procState != PROCESS_STATE_NONEXISTENT;
                            }
                        }
                    }
                    if ((reg.mWhich & ActivityManager.UID_OBSERVER_CAPABILITY) != 0) {
                        doReport |= (change & UidRecord.CHANGE_CAPABILITY) != 0;
                    }
                    if (doReport) {
                        if (DEBUG_UID_OBSERVERS) {
                            Slog.i(TAG_UID_OBSERVERS, "UID CHANGED uid=" + item.uid
                                    + ": " + item.procState + ": " + item.capability);
                        }
                        if (reg.mLastProcStates != null) {
                            reg.mLastProcStates.put(item.uid, item.procState);
                        }
@@ -310,7 +318,6 @@ public class UidObserverController {
                                item.procStateSeq, item.capability);
                    }
                }
                }
                final int duration = (int) (SystemClock.uptimeMillis() - start);
                if (reg.mMaxDispatchTime < duration) {
                    reg.mMaxDispatchTime = duration;
@@ -428,12 +435,14 @@ public class UidObserverController {
                ActivityManager.UID_OBSERVER_ACTIVE,
                ActivityManager.UID_OBSERVER_GONE,
                ActivityManager.UID_OBSERVER_PROCSTATE,
                ActivityManager.UID_OBSERVER_CAPABILITY,
        };
        private static final int[] PROTO_ENUMS = new int[]{
                ActivityManagerProto.UID_OBSERVER_FLAG_IDLE,
                ActivityManagerProto.UID_OBSERVER_FLAG_ACTIVE,
                ActivityManagerProto.UID_OBSERVER_FLAG_GONE,
                ActivityManagerProto.UID_OBSERVER_FLAG_PROCSTATE,
                ActivityManagerProto.UID_OBSERVER_FLAG_CAPABILITY,
        };

        UidObserverRegistration(int uid, @NonNull String pkg, int which, int cutpoint) {
@@ -462,6 +471,9 @@ public class UidObserverController {
            if ((mWhich & ActivityManager.UID_OBSERVER_GONE) != 0) {
                pw.print(" GONE");
            }
            if ((mWhich & ActivityManager.UID_OBSERVER_CAPABILITY) != 0) {
                pw.print(" CAP");
            }
            if ((mWhich & ActivityManager.UID_OBSERVER_PROCSTATE) != 0) {
                pw.print(" STATE");
                pw.print(" (cut=");
Loading