Add "defer-until-active" policy to TIME_TICK broadcast.

Adding this policy ensures that apps in the Cached/Frozen
state are not brought out of that state to deliver this
broadcast.

Also, update the usage of makeRemovingMatchingFilter() as
it is deprecated.

Bug: 266988990
Bug: 266001401
Test: atest ./services/tests/mockingservicestests/src/com/android/server/alarm/AlarmManagerServiceTest.java
Change-Id: Iff993aa8fa03a5da69517bd46343e8b8c5a1bf48
This commit is contained in:
Sudheer Shanka
2023-01-27 16:21:46 -08:00
parent 8970870803
commit 14b9df230e
2 changed files with 29 additions and 6 deletions

View File

@@ -1933,8 +1933,9 @@ public class AlarmManagerService extends SystemService {
Intent.FLAG_RECEIVER_REGISTERED_ONLY
| Intent.FLAG_RECEIVER_FOREGROUND
| Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
mTimeTickOptions = BroadcastOptions
.makeRemovingMatchingFilter(new IntentFilter(Intent.ACTION_TIME_TICK))
mTimeTickOptions = BroadcastOptions.makeBasic()
.setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT)
.setDeferUntilActive(true)
.toBundle();
mTimeTickTrigger = new IAlarmListener.Stub() {
@Override
@@ -4252,8 +4253,8 @@ public class AlarmManagerService extends SystemService {
}
}
// And send a TIME_TICK right now, since it is important to get the UI updated.
mHandler.post(() ->
getContext().sendBroadcastAsUser(mTimeTickIntent, UserHandle.ALL));
mHandler.post(() -> getContext().sendBroadcastAsUser(mTimeTickIntent,
UserHandle.ALL, null, mTimeTickOptions));
} else {
mNonInteractiveStartTime = nowELAPSED;
}

View File

@@ -112,7 +112,6 @@ import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.Manifest;
import android.app.ActivityManager;
@@ -1264,7 +1263,30 @@ public class AlarmManagerServiceTest {
mService.interactiveStateChangedLocked(false);
mService.interactiveStateChangedLocked(true);
runnableCaptor.getValue().run();
verify(mMockContext).sendBroadcastAsUser(mService.mTimeTickIntent, UserHandle.ALL);
final ArgumentCaptor<Bundle> optionsCaptor = ArgumentCaptor.forClass(Bundle.class);
verify(mMockContext).sendBroadcastAsUser(eq(mService.mTimeTickIntent), eq(UserHandle.ALL),
isNull(), optionsCaptor.capture());
verifyTimeTickBroadcastOptions(optionsCaptor.getValue());
}
@Test
public void sendsTimeTickOnAlarmTrigger() throws Exception {
final ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
// Stubbing so the handler doesn't actually run the runnable.
doReturn(true).when(mService.mHandler).post(runnableCaptor.capture());
mService.mTimeTickTrigger.doAlarm(mock(IAlarmCompleteListener.class));
runnableCaptor.getValue().run();
final ArgumentCaptor<Bundle> optionsCaptor = ArgumentCaptor.forClass(Bundle.class);
verify(mMockContext).sendBroadcastAsUser(eq(mService.mTimeTickIntent), eq(UserHandle.ALL),
isNull(), optionsCaptor.capture());
verifyTimeTickBroadcastOptions(optionsCaptor.getValue());
}
private void verifyTimeTickBroadcastOptions(Bundle actualOptionsBundle) {
final BroadcastOptions actualOptions = new BroadcastOptions(actualOptionsBundle);
assertEquals(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT,
actualOptions.getDeliveryGroupPolicy());
assertTrue(actualOptions.isDeferUntilActive());
}
@Test