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
This commit is contained in:
Sudheer Shanka
2022-04-05 18:17:35 +00:00
parent 3cc4eea5b6
commit ffa1d57dae
3 changed files with 23 additions and 7 deletions

View File

@@ -631,7 +631,7 @@ public abstract class ActivityManagerInternal {
* @param uid uid
* @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.

View File

@@ -17268,7 +17268,7 @@ public class ActivityManagerService extends IActivityManager.Stub
}
@Override
public void addPendingTopUid(int uid, int pid) {
public void addPendingTopUid(int uid, int pid, @Nullable IApplicationThread thread) {
final boolean isNewPending = mPendingStartActivityUids.add(uid, pid);
// If the next top activity is in cached and frozen mode, WM should raise its priority
// to unfreeze it. This is done by calling AMS.updateOomAdj that will lower its oom adj.
@@ -17285,16 +17285,32 @@ public class ActivityManagerService extends IActivityManager.Stub
// (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) {
if (isNewPending && mNetworkPolicyUidObserver != null) {
try {
final long procStateSeq = mProcessList.getNextProcStateSeq();
mNetworkPolicyUidObserver.onUidStateChanged(uid, PROCESS_STATE_TOP,
mProcessList.getNextProcStateSeq(), PROCESS_CAPABILITY_ALL);
procStateSeq, PROCESS_CAPABILITY_ALL);
if (thread != null && isNetworkingBlockedForUid(uid)) {
thread.setNetworkBlockSeq(procStateSeq);
}
} catch (RemoteException e) {
// Should not happen; call is within the same process
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;
}
}
@Override
public void deletePendingTopUid(int uid, long nowElapsed) {
mPendingStartActivityUids.delete(uid, nowElapsed);

View File

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