Apply delivery group policies to CLOSE_SYSTEM_DIALOG broadcast.

- The "set-defer-until-active" policy is applied so that the
broadcast targeted to apps in the Cached state is deferred until
they come out of that state.
- The "deliver-most-recent" policy is applied so that if there are
already pending broadcasts waiting to be delivered when a new
broadcast is sent, the old ones are discarded.

Bug: 271950524
Test: TH
Change-Id: I2c75498e93b6a04ddc631348cfb1cb9a4daec1fd
diff --git a/java/com/android/incallui/NotificationBroadcastReceiver.java b/java/com/android/incallui/NotificationBroadcastReceiver.java
index 241d8ed..91421ea 100644
--- a/java/com/android/incallui/NotificationBroadcastReceiver.java
+++ b/java/com/android/incallui/NotificationBroadcastReceiver.java
@@ -16,14 +16,17 @@
 
 package com.android.incallui;
 
+import android.app.BroadcastOptions;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build.VERSION_CODES;
+import android.os.Bundle;
 import android.support.annotation.NonNull;
 import android.support.annotation.RequiresApi;
 import android.telecom.CallAudioState;
 import android.telecom.VideoProfile;
+
 import com.android.dialer.common.LogUtil;
 import com.android.dialer.common.concurrent.DialerExecutorComponent;
 import com.android.dialer.logging.DialerImpression;
@@ -32,6 +35,7 @@
 import com.android.incallui.call.DialerCall;
 import com.android.incallui.call.TelecomAdapter;
 import com.android.incallui.speakeasy.SpeakEasyCallManager;
+
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -97,7 +101,7 @@
     } else if (action.equals(ACTION_DECLINE_VIDEO_UPGRADE_REQUEST)) {
       declineUpgradeRequest();
     } else if (action.equals(ACTION_PULL_EXTERNAL_CALL)) {
-      context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
+      closeSystemDialogs(context);
       int notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1);
       InCallPresenter.getInstance().getExternalCallNotifier().pullExternalCall(notificationId);
     } else if (action.equals(ACTION_TURN_ON_SPEAKER)) {
@@ -220,4 +224,15 @@
       }
     }
   }
+
+  /** Closes open system dialogs and the notification shade. */
+  private void closeSystemDialogs(Context context) {
+    final Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
+            .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+    final Bundle options = BroadcastOptions.makeBasic()
+            .setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT)
+            .setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE)
+            .toBundle();
+    context.sendBroadcast(intent, null /* receiverPermission */, options);
+  }
 }