Inform Application thread to block for network rules to be updated.

Bug: 226299593
Test: atest tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java
Change-Id: Ie37173d0d3f4034a74b834550920530166cd4201
Merged-In: Ie37173d0d3f4034a74b834550920530166cd4201
This commit is contained in:
Sudheer Shanka
2022-04-05 18:17:35 +00:00
parent 46ddc965b9
commit 3bad67bf01
4 changed files with 27 additions and 9 deletions

View File

@@ -587,7 +587,7 @@ public abstract class ActivityManagerInternal {
* @param uid uid * @param uid uid
* @param pid pid of the ProcessRecord that is pending top. * @param pid pid of the ProcessRecord that is pending top.
*/ */
public abstract void addPendingTopUid(int uid, int pid); public abstract void addPendingTopUid(int uid, int pid, @Nullable IApplicationThread thread);
/** /**
* Delete uid from the ActivityManagerService PendingStartActivityUids list. * Delete uid from the ActivityManagerService PendingStartActivityUids list.

View File

@@ -16315,20 +16315,36 @@ public class ActivityManagerService extends IActivityManager.Stub
} }
@Override @Override
public void addPendingTopUid(int uid, int pid) { public void addPendingTopUid(int uid, int pid, @Nullable IApplicationThread thread) {
mPendingStartActivityUids.add(uid, pid); final boolean isNewPending = mPendingStartActivityUids.add(uid, pid);
// We need to update the network rules for the app coming to the top state so that // 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 // 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 // (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 // informing NetworkPolicyManager might get delayed, informing the state change as soon
// as we know app is going to come to the top state. // as we know app is going to come to the top state.
if (mNetworkPolicyUidObserver != null) { if (isNewPending && mNetworkPolicyUidObserver != null) {
try { try {
final long procStateSeq = mProcessList.getNextProcStateSeq();
mNetworkPolicyUidObserver.onUidStateChanged(uid, PROCESS_STATE_TOP, mNetworkPolicyUidObserver.onUidStateChanged(uid, PROCESS_STATE_TOP,
mProcessList.getNextProcStateSeq(), PROCESS_CAPABILITY_ALL); procStateSeq, PROCESS_CAPABILITY_ALL);
} catch (RemoteException e) { if (thread != null && isNetworkingBlockedForUid(uid)) {
// Should not happen; call is within the same process thread.setNetworkBlockSeq(procStateSeq);
} }
} catch (RemoteException e) {
Slog.d(TAG, "Error calling setNetworkBlockSeq", e);
}
}
}
private boolean isNetworkingBlockedForUid(int uid) {
synchronized (mUidNetworkBlockedReasons) {
// TODO: We can consider only those blocked reasons that will be overridden
// by the TOP state. For other ones, there is no point in waiting.
// TODO: We can reuse this data in
// ProcessList#incrementProcStateSeqAndNotifyAppsLOSP instead of calling into
// NetworkManagementService.
return mUidNetworkBlockedReasons.get(uid, BLOCKED_REASON_NONE)
!= BLOCKED_REASON_NONE;
} }
} }

View File

@@ -44,10 +44,12 @@ final class PendingStartActivityUids {
mContext = context; mContext = context;
} }
synchronized void add(int uid, int pid) { synchronized boolean add(int uid, int pid) {
if (mPendingUids.get(uid) == null) { if (mPendingUids.get(uid) == null) {
mPendingUids.put(uid, new Pair<>(pid, SystemClock.elapsedRealtime())); mPendingUids.put(uid, new Pair<>(pid, SystemClock.elapsedRealtime()));
return true;
} }
return false;
} }
synchronized void delete(int uid, long nowElapsed) { synchronized void delete(int uid, long nowElapsed) {

View File

@@ -1123,7 +1123,7 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
/** Makes the process have top state before oom-adj is computed from a posted message. */ /** Makes the process have top state before oom-adj is computed from a posted message. */
void addToPendingTop() { void addToPendingTop() {
mAtm.mAmInternal.addPendingTopUid(mUid, mPid); mAtm.mAmInternal.addPendingTopUid(mUid, mPid, mThread);
} }
void updateServiceConnectionActivities() { void updateServiceConnectionActivities() {
@@ -1176,7 +1176,7 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio
} }
// update ActivityManagerService.PendingStartActivityUids list. // update ActivityManagerService.PendingStartActivityUids list.
if (topProcessState == ActivityManager.PROCESS_STATE_TOP) { if (topProcessState == ActivityManager.PROCESS_STATE_TOP) {
mAtm.mAmInternal.addPendingTopUid(mUid, mPid); mAtm.mAmInternal.addPendingTopUid(mUid, mPid, mThread);
} }
prepareOomAdjustment(); prepareOomAdjustment();
// Posting the message at the front of queue so WM lock isn't held when we call into AM, // Posting the message at the front of queue so WM lock isn't held when we call into AM,