BroadcastQueue: DISPATCH_SENDING_BROADCAST_EVENT.

Consistently send this event from both legacy and modern stack, and
refactor to hide the DISPATCH_SENDING_BROADCAST_EVENT message as an
implementation detail inside AMS.

Tests to verify that we always deliver expected "finished" events.

Bug: 257038590
Test: atest FrameworksMockingServicesTests:BroadcastRecordTest
Test: atest FrameworksMockingServicesTests:BroadcastQueueTest
Test: atest FrameworksMockingServicesTests:BroadcastQueueModernImplTest
Change-Id: Ib5c0b78ede454a11cac75cec06deebc6adbb8a40
This commit is contained in:
Jeff Sharkey
2022-11-02 11:59:38 -06:00
parent 3e67bb6a03
commit 65edb3b5d1
4 changed files with 29 additions and 7 deletions

View File

@@ -14723,6 +14723,15 @@ public class ActivityManagerService extends IActivityManager.Stub
mCurBroadcastStats.addBackgroundCheckViolation(action, targetPackage);
}
final void notifyBroadcastFinishedLocked(@NonNull BroadcastRecord original) {
final ApplicationInfo info = original.callerApp != null ? original.callerApp.info : null;
final String callerPackage = info != null ? info.packageName : original.callerPackage;
if (callerPackage != null) {
mHandler.obtainMessage(ActivityManagerService.DISPATCH_SENDING_BROADCAST_EVENT,
original.callingUid, 0, callerPackage).sendToTarget();
}
}
final Intent verifyBroadcastLocked(Intent intent) {
// Refuse possible leaked file descriptors
if (intent != null && intent.hasFileDescriptors() == true) {

View File

@@ -1690,13 +1690,7 @@ public class BroadcastQueueImpl extends BroadcastQueue {
System.identityHashCode(original));
}
final ApplicationInfo info = original.callerApp != null ? original.callerApp.info : null;
final String callerPackage = info != null ? info.packageName : original.callerPackage;
if (callerPackage != null) {
mService.mHandler.obtainMessage(ActivityManagerService.DISPATCH_SENDING_BROADCAST_EVENT,
original.callingUid, 0, callerPackage).sendToTarget();
}
mService.notifyBroadcastFinishedLocked(original);
mHistory.addBroadcastToHistoryLocked(original);
}

View File

@@ -1403,6 +1403,7 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
final boolean recordFinished = (r.terminalCount == r.receivers.size());
if (recordFinished) {
mService.notifyBroadcastFinishedLocked(r);
mHistory.addBroadcastToHistoryLocked(r);
r.finishTime = SystemClock.uptimeMillis();

View File

@@ -1639,4 +1639,22 @@ public class BroadcastQueueTest {
waitForIdle();
verify(mAms, never()).enqueueOomAdjTargetLocked(any());
}
/**
* Verify that expected events are triggered when a broadcast is finished.
*/
@Test
public void testNotifyFinished() throws Exception {
final ProcessRecord callerApp = makeActiveProcessRecord(PACKAGE_RED);
final Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED);
final BroadcastRecord record = makeBroadcastRecord(intent, callerApp,
List.of(makeManifestReceiver(PACKAGE_GREEN, CLASS_GREEN)));
enqueueBroadcast(record);
waitForIdle();
verify(mAms).notifyBroadcastFinishedLocked(eq(record));
verify(mAms).addBroadcastStatLocked(eq(Intent.ACTION_TIMEZONE_CHANGED), eq(PACKAGE_RED),
eq(1), eq(0), anyLong());
}
}