fixup! SystemUI: screenshot: Add delete action chip intent

- After QPR3 changes:
  https://android.googlesource.com/platform/frameworks/base/+/f95951a

Change-Id: I12fd04d00c610a10aa4a43d8fa8d3cf251ff1407
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java b/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java
index bbf7ed5..eb1138b 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/SaveImageInBackgroundTask.java
@@ -28,6 +28,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.graphics.drawable.Icon;
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -39,6 +40,7 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
 import com.android.systemui.flags.FeatureFlags;
+import com.android.systemui.res.R;
 
 import com.google.common.util.concurrent.ListenableFuture;
 
@@ -164,6 +166,7 @@
             mImageData.quickShareAction = createQuickShareAction(
                     mQuickShareData.quickShareAction, mScreenshotId, uri, mImageTime, image,
                     mParams.owner);
+            mImageData.deleteAction = createDeleteAction(uri, smartActionsEnabled);
             mImageData.subject = getSubjectString(mImageTime);
 
             mParams.mActionsReadyListener.onActionsReady(mImageData);
@@ -285,6 +288,29 @@
                 .build();
     }
 
+    @VisibleForTesting
+    Notification.Action createDeleteAction(Uri uri, boolean smartActionsEnabled) {
+        // Make sure pending intents for the system user are still unique across users
+        // by setting the (otherwise unused) request code to the current user id.
+        int requestCode = mContext.getUserId();
+
+        // Create a delete action for the notification
+        Intent wrappedIntent = new Intent(mContext, DeleteScreenshotReceiver.class)
+                       .putExtra(ScreenshotController.SCREENSHOT_URI_ID, uri.toString())
+                       .putExtra(ScreenshotController.EXTRA_ID, mScreenshotId)
+                       .putExtra(ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED,
+                               smartActionsEnabled)
+                       .addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+        PendingIntent deleteAction = PendingIntent.getBroadcast(mContext, requestCode, wrappedIntent,
+               PendingIntent.FLAG_CANCEL_CURRENT
+                       | PendingIntent.FLAG_ONE_SHOT
+                       | PendingIntent.FLAG_IMMUTABLE);
+        Notification.Action.Builder deleteActionBuilder = new Notification.Action.Builder(
+               Icon.createWithResource(mContext.getResources(), R.drawable.ic_screenshot_delete),
+               mContext.getResources().getString(com.android.internal.R.string.delete), deleteAction);
+        return deleteActionBuilder.build();
+    }
+
     private Intent createFillInIntent(Uri uri, long imageTime) {
         Intent fillIn = new Intent();
         fillIn.setType("image/png");
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
index ee3e7ba..938b786 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java
@@ -168,6 +168,7 @@
     static class SavedImageData {
         public Uri uri;
         public List<Notification.Action> smartActions;
+        public Notification.Action deleteAction;
         public Notification.Action quickShareAction;
         public UserHandle owner;
         public String subject;  // Title for sharing
@@ -179,6 +180,7 @@
             uri = null;
             smartActions = null;
             quickShareAction = null;
+            deleteAction = null;
             subject = null;
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
index 1668849..fc18c72 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotView.java
@@ -829,10 +829,12 @@
                     ActionIntentCreator.INSTANCE.createEdit(imageData.uri, mContext),
                     imageData.owner, true);
         });
-        mDeleteChip.setPendingIntent(imageData.deleteAction.actionIntent, () -> {
-            mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_DELETE_TAPPED);
-            animateDismissal();
-        });
+        if (imageData.deleteAction != null) {
+            mDeleteChip.setPendingIntent(imageData.deleteAction.actionIntent, () -> {
+                mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_DELETE_TAPPED);
+                animateDismissal();
+            });
+        }
         mScreenshotPreview.setOnClickListener(v -> {
             mUiEventLogger.log(ScreenshotEvent.SCREENSHOT_PREVIEW_TAPPED, 0, mPackageName);
             prepareSharedTransition();