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

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

Inform NPMS about the app coming to the TOP state early.

It's possible for an app to come to the TOP state before
the oomAdj computation is done and we cannot wait for
oomAdj computation to complete and then inform NPMS about
the state change. So, inform NPMS early about the state
change when we know the app is going to the TOP state.

Bug: 226299593
Test: atest tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Change-Id: I5d582618652cead179c419f7fe084283dd89d514
parent 3ad19f7a
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -842,4 +842,15 @@ public abstract class ActivityManagerInternal {
     * Returns some summary statistics of the current PendingIntent queue - sizes and counts.
     */
    public abstract List<PendingIntentStats> getPendingIntentStats();

    /**
     * Register the UidObserver for NetworkPolicyManager service.
     *
     * This is equivalent to calling
     * {@link IActivityManager#registerUidObserver(IUidObserver, int, int, String)} but having a
     * separate method for NetworkPolicyManager service so that it's UidObserver can be called
     * separately outside the usual UidObserver flow.
     */
    public abstract void registerNetworkPolicyUidObserver(@NonNull IUidObserver observer,
            int which, int cutpoint, @NonNull String callingPackage);
}
+23 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import static android.app.ActivityManager.INSTR_FLAG_DISABLE_ISOLATED_STORAGE;
import static android.app.ActivityManager.INSTR_FLAG_DISABLE_TEST_API_CHECKS;
import static android.app.ActivityManager.INSTR_FLAG_NO_RESTART;
import static android.app.ActivityManager.INTENT_SENDER_ACTIVITY;
import static android.app.ActivityManager.PROCESS_CAPABILITY_ALL;
import static android.app.ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
import static android.app.ActivityManager.PROCESS_STATE_TOP;
@@ -1483,6 +1484,7 @@ public class ActivityManagerService extends IActivityManager.Stub
    final ActivityThread mSystemThread;
    final UidObserverController mUidObserverController;
    private volatile IUidObserver mNetworkPolicyUidObserver;
    final AppRestrictionController mAppRestrictionController;
@@ -17300,6 +17302,19 @@ public class ActivityManagerService extends IActivityManager.Stub
            if (isNewPending && mOomAdjuster != null) { // It can be null in unit test.
                mOomAdjuster.mCachedAppOptimizer.unfreezeProcess(pid);
            }
            // We need to update the network rules for the app coming to the top state so that
            // it can access network when the device or the app is in a restricted state
            // (e.g. battery/data saver) but since waiting for updateOomAdj to complete and then
            // informing NetworkPolicyManager might get delayed, informing the state change as soon
            // as we know app is going to come to the top state.
            if (mNetworkPolicyUidObserver != null) {
                try {
                    mNetworkPolicyUidObserver.onUidStateChanged(uid, PROCESS_STATE_TOP,
                            mProcessList.getProcStateSeqCounter(), PROCESS_CAPABILITY_ALL);
                } catch (RemoteException e) {
                    // Should not happen; call is within the same process
                }
            }
        }
        @Override
@@ -17497,6 +17512,14 @@ public class ActivityManagerService extends IActivityManager.Stub
        public void restart() {
            ActivityManagerService.this.restart();
        }
        @Override
        public void registerNetworkPolicyUidObserver(@NonNull IUidObserver observer,
                int which, int cutpoint, @NonNull String callingPackage) {
            mNetworkPolicyUidObserver = observer;
            mUidObserverController.register(observer, which, cutpoint, callingPackage,
                    Binder.getCallingUid());
        }
    }
    long inputDispatchingTimedOut(int pid, final boolean aboveSystem, String reason) {
+5 −2
Original line number Diff line number Diff line
@@ -423,9 +423,8 @@ public final class ProcessList {
     * Having a global counter ensures that seq numbers are monotonically increasing for a
     * particular uid even when the uidRecord is re-created.
     */
    @GuardedBy("mService")
    @VisibleForTesting
    long mProcStateSeqCounter = 0;
    volatile long mProcStateSeqCounter = 0;

    /**
     * A global counter for generating sequence numbers to uniquely identify pending process starts.
@@ -4860,6 +4859,10 @@ public final class ProcessList {
        }
    }

    long getProcStateSeqCounter() {
        return mProcStateSeqCounter;
    }

    /**
     * Create a server socket in system_server, zygote will connect to it
     * in order to send unsolicited messages to system_server.
+1 −1
Original line number Diff line number Diff line
@@ -996,7 +996,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
                final int changes = ActivityManager.UID_OBSERVER_PROCSTATE
                        | ActivityManager.UID_OBSERVER_GONE
                        | ActivityManager.UID_OBSERVER_CAPABILITY;
                mActivityManager.registerUidObserver(mUidObserver, changes,
                mActivityManagerInternal.registerNetworkPolicyUidObserver(mUidObserver, changes,
                        NetworkPolicyManager.FOREGROUND_THRESHOLD_STATE, "android");
                mNetworkManager.registerObserver(mAlertObserver);
            } catch (RemoteException e) {
+2 −2
Original line number Diff line number Diff line
@@ -441,14 +441,14 @@ public class NetworkPolicyManagerServiceTest {
        setNetpolicyXml(context);

        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable {
                mUidObserver = (IUidObserver) invocation.getArguments()[0];
                Log.d(TAG, "set mUidObserver to " + mUidObserver);
                return null;
            }
        }).when(mActivityManager).registerUidObserver(any(), anyInt(), anyInt(), any(String.class));
        }).when(mActivityManagerInternal).registerNetworkPolicyUidObserver(any(),
                anyInt(), anyInt(), any(String.class));

        mFutureIntent = newRestrictBackgroundChangedFuture();
        mDeps = new TestDependencies(mServiceContext);