Add Smart Suggestions to heads up notifications.

Add smart replies and actions to Heads-up notifications (HUN).

Note: only one line of text is shown in HUNs with messaging templates.
Thus the user might not see the full message to respond to even when the
smart replies/actions are displayed.

Screenshot: https://screenshot.googleplex.com/3R5GVZGXGNg.png

Bug: 117257685
Test: Use cinek@'s Notify app to display HUN with smart replies/actions.
Change-Id: I10d2c87b445de8f471ad0978829cede9ac6a6663
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SmartReplyView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SmartReplyView.java
index 2a4336e..913b2ae 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SmartReplyView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SmartReplyView.java
@@ -16,6 +16,8 @@
 import android.graphics.drawable.InsetDrawable;
 import android.graphics.drawable.RippleDrawable;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.text.Layout;
 import android.text.TextPaint;
 import android.text.method.TransformationMethod;
@@ -61,6 +63,7 @@
 
     private final SmartReplyConstants mConstants;
     private final KeyguardDismissUtil mKeyguardDismissUtil;
+    private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper());
 
     /**
      * The upper bound for the height of this view in pixels. Notifications are automatically
@@ -209,13 +212,15 @@
      * notification are shown.
      */
     public void addSmartActions(SmartActions smartActions,
-            SmartReplyController smartReplyController, NotificationData.Entry entry) {
+            SmartReplyController smartReplyController, NotificationData.Entry entry,
+            HeadsUpManager headsUpManager) {
         int numSmartActions = smartActions.actions.size();
         for (int n = 0; n < numSmartActions; n++) {
             Notification.Action action = smartActions.actions.get(n);
             if (action.actionIntent != null) {
                 Button actionButton = inflateActionButton(
-                        getContext(), this, n, smartActions, smartReplyController, entry);
+                        getContext(), this, n, smartActions, smartReplyController, entry,
+                        headsUpManager);
                 addView(actionButton);
             }
         }
@@ -274,7 +279,7 @@
     @VisibleForTesting
     Button inflateActionButton(Context context, ViewGroup root, int actionIndex,
             SmartActions smartActions, SmartReplyController smartReplyController,
-            NotificationData.Entry entry) {
+            NotificationData.Entry entry, HeadsUpManager headsUpManager) {
         Notification.Action action = smartActions.actions.get(actionIndex);
         Button button = (Button) LayoutInflater.from(context).inflate(
                 R.layout.smart_action_button, root, false);
@@ -290,8 +295,12 @@
         button.setOnClickListener(view ->
                 getActivityStarter().startPendingIntentDismissingKeyguard(
                         action.actionIntent,
-                        () -> smartReplyController.smartActionClicked(
-                                entry, actionIndex, action, smartActions.fromAssistant)));
+                        () -> {
+                            smartReplyController.smartActionClicked(
+                                    entry, actionIndex, action, smartActions.fromAssistant);
+                            postOnUiThread(() ->
+                                    headsUpManager.removeNotification(entry.key, true));
+                        }));
 
         // TODO(b/119010281): handle accessibility
 
@@ -301,6 +310,10 @@
         return button;
     }
 
+    private void postOnUiThread(Runnable runnable) {
+        mMainThreadHandler.post(runnable);
+    }
+
     @Override
     public LayoutParams generateLayoutParams(AttributeSet attrs) {
         return new LayoutParams(mContext, attrs);