Dismiss one missed call notification when swipe it away

Before the change, swiping the notification away will mark all unread
missed calls as read which cancels all the missed call notificatinos.

Fixes: 165077708
Test: manually
Change-Id: Id818236149b4fcfed52b289d2f2a7802b841e437
diff --git a/src/com/android/car/dialer/notification/InCallNotificationController.java b/src/com/android/car/dialer/notification/InCallNotificationController.java
index 464787b..18bf8d1 100644
--- a/src/com/android/car/dialer/notification/InCallNotificationController.java
+++ b/src/com/android/car/dialer/notification/InCallNotificationController.java
@@ -195,7 +195,8 @@
     private Intent getIntent(String action, Call call) {
         Intent intent = new Intent(action, null, mContext, NotificationService.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        intent.putExtra(NotificationService.EXTRA_CALL_ID, call.getDetails().getTelecomCallId());
+        intent.putExtra(NotificationService.EXTRA_PHONE_NUMBER,
+                call.getDetails().getTelecomCallId());
         return intent;
     }
 }
diff --git a/src/com/android/car/dialer/notification/MissedCallNotificationController.java b/src/com/android/car/dialer/notification/MissedCallNotificationController.java
index 219f879..633b8b1 100644
--- a/src/com/android/car/dialer/notification/MissedCallNotificationController.java
+++ b/src/com/android/car/dialer/notification/MissedCallNotificationController.java
@@ -147,7 +147,7 @@
                                     R.plurals.notification_missed_call, callLogSize, callLogSize))
                             .setContentText(TelecomUtils.getBidiWrappedNumber(pair.first))
                             .setContentIntent(getContentPendingIntent())
-                            .setDeleteIntent(getDeleteIntent())
+                            .setDeleteIntent(getDeleteIntent(callLog))
                             .setOnlyAlertOnce(true)
                             .setShowWhen(true)
                             .setWhen(callLog.getLastCallEndTimestamp())
@@ -193,14 +193,22 @@
         return pendingIntent;
     }
 
-    private PendingIntent getDeleteIntent() {
-        Intent intent = new Intent(NotificationService.ACTION_READ_ALL_MISSED, null, mContext,
+    private PendingIntent getDeleteIntent(PhoneCallLog phoneCallLog) {
+        Intent intent = new Intent(NotificationService.ACTION_READ_MISSED, null, mContext,
                 NotificationService.class);
+        String phoneNumberString = phoneCallLog.getPhoneNumberString();
+        if (TextUtils.isEmpty(phoneNumberString)) {
+            // For unknown call, pass the call log id to mark as read
+            intent.putExtra(NotificationService.EXTRA_CALL_LOG_ID, phoneCallLog.getPhoneLogId());
+        } else {
+            intent.putExtra(NotificationService.EXTRA_PHONE_NUMBER, phoneNumberString);
+        }
         PendingIntent pendingIntent = PendingIntent.getService(
                 mContext,
-                0,
+                // Unique id for PendingIntents with different extras
+                /* requestCode= */(int) System.currentTimeMillis(),
                 intent,
-                PendingIntent.FLAG_UPDATE_CURRENT);
+                PendingIntent.FLAG_IMMUTABLE);
         return pendingIntent;
     }
 
@@ -213,7 +221,7 @@
 
     private PendingIntent getIntent(String action, String phoneNumberString) {
         Intent intent = new Intent(action, null, mContext, NotificationService.class);
-        intent.putExtra(NotificationService.EXTRA_CALL_ID, phoneNumberString);
+        intent.putExtra(NotificationService.EXTRA_PHONE_NUMBER, phoneNumberString);
         return PendingIntent.getService(
                 mContext,
                 // Unique id for PendingIntents with different extras
diff --git a/src/com/android/car/dialer/notification/NotificationService.java b/src/com/android/car/dialer/notification/NotificationService.java
index 54abe15..948b0ca 100644
--- a/src/com/android/car/dialer/notification/NotificationService.java
+++ b/src/com/android/car/dialer/notification/NotificationService.java
@@ -41,8 +41,9 @@
     static final String ACTION_SHOW_FULLSCREEN_UI = "CD.ACTION_SHOW_FULLSCREEN_UI";
     static final String ACTION_CALL_BACK_MISSED = "CD.ACTION_CALL_BACK_MISSED";
     static final String ACTION_MESSAGE_MISSED = "CD.ACTION_MESSAGE_MISSED";
-    static final String ACTION_READ_ALL_MISSED = "CD.ACTION_READ_ALL_MISSED";
-    static final String EXTRA_CALL_ID = "CD.EXTRA_CALL_ID";
+    static final String ACTION_READ_MISSED = "CD.ACTION_READ_MISSED";
+    static final String EXTRA_PHONE_NUMBER = "CD.EXTRA_PHONE_NUMBER";
+    static final String EXTRA_CALL_LOG_ID = "CD.EXTRA_CALL_LOG_ID";
 
     @Override
     public IBinder onBind(Intent intent) {
@@ -52,22 +53,22 @@
     /** Create an intent to handle reading all missed call action and schedule for executing. */
     public static void readAllMissedCall(Context context) {
         Intent readAllMissedCallIntent = new Intent(context, NotificationService.class);
-        readAllMissedCallIntent.setAction(ACTION_READ_ALL_MISSED);
+        readAllMissedCallIntent.setAction(ACTION_READ_MISSED);
         context.startService(readAllMissedCallIntent);
     }
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         String action = intent.getAction();
-        String callId = intent.getStringExtra(EXTRA_CALL_ID);
+        String phoneNumber = intent.getStringExtra(EXTRA_PHONE_NUMBER);
         switch (action) {
             case ACTION_ANSWER_CALL:
-                answerCall(callId);
-                InCallNotificationController.get().cancelInCallNotification(callId);
+                answerCall(phoneNumber);
+                InCallNotificationController.get().cancelInCallNotification(phoneNumber);
                 break;
             case ACTION_DECLINE_CALL:
-                declineCall(callId);
-                InCallNotificationController.get().cancelInCallNotification(callId);
+                declineCall(phoneNumber);
+                InCallNotificationController.get().cancelInCallNotification(phoneNumber);
                 break;
             case ACTION_SHOW_FULLSCREEN_UI:
                 Intent inCallActivityIntent = new Intent(getApplicationContext(),
@@ -75,18 +76,23 @@
                 inCallActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 inCallActivityIntent.putExtra(Constants.Intents.EXTRA_SHOW_INCOMING_CALL, true);
                 startActivity(inCallActivityIntent);
-                InCallNotificationController.get().cancelInCallNotification(callId);
+                InCallNotificationController.get().cancelInCallNotification(phoneNumber);
                 break;
             case ACTION_CALL_BACK_MISSED:
-                UiCallManager.get().placeCall(callId);
-                TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
+                UiCallManager.get().placeCall(phoneNumber);
+                TelecomUtils.markCallLogAsRead(getApplicationContext(), phoneNumber);
                 break;
             case ACTION_MESSAGE_MISSED:
                 // TODO: call assistant to send message
-                TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
+                TelecomUtils.markCallLogAsRead(getApplicationContext(), phoneNumber);
                 break;
-            case ACTION_READ_ALL_MISSED:
-                TelecomUtils.markCallLogAsRead(getApplicationContext(), callId);
+            case ACTION_READ_MISSED:
+                if (!TextUtils.isEmpty(phoneNumber)) {
+                    TelecomUtils.markCallLogAsRead(getApplicationContext(), phoneNumber);
+                } else {
+                    long callLogId = intent.getLongExtra(EXTRA_CALL_LOG_ID, -1);
+                    TelecomUtils.markCallLogAsRead(getApplicationContext(), callLogId);
+                }
                 break;
             default:
                 break;