Adding a delete action to the screenshot notification.

Bug: 21161917
Change-Id: I03e2073ef09e9aea00b523eddbe2b9122d280d89
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index e47c7a0..cc8e46b 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -225,6 +225,12 @@
 
         <!-- Callback for dismissing screenshot notification after a share target is picked -->
         <receiver android:name=".screenshot.GlobalScreenshot$TargetChosenReceiver"
+                  android:process=":screenshot"
+                  android:exported="false" />
+
+        <!-- Callback for deleting screenshot notification -->
+        <receiver android:name=".screenshot.GlobalScreenshot$DeleteScreenshotReceiver"
+                  android:process=":screenshot"
                   android:exported="false" />
 
         <!-- started from UsbDeviceSettingsManager -->
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_menu_share.png b/packages/SystemUI/res/drawable-hdpi/ic_menu_share.png
deleted file mode 100644
index 11ab480..0000000
--- a/packages/SystemUI/res/drawable-hdpi/ic_menu_share.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_menu_share.png b/packages/SystemUI/res/drawable-mdpi/ic_menu_share.png
deleted file mode 100644
index 30e69bb..0000000
--- a/packages/SystemUI/res/drawable-mdpi/ic_menu_share.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_menu_share.png b/packages/SystemUI/res/drawable-xhdpi/ic_menu_share.png
deleted file mode 100644
index af3e112..0000000
--- a/packages/SystemUI/res/drawable-xhdpi/ic_menu_share.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxhdpi/ic_menu_share.png b/packages/SystemUI/res/drawable-xxhdpi/ic_menu_share.png
deleted file mode 100644
index d450531..0000000
--- a/packages/SystemUI/res/drawable-xxhdpi/ic_menu_share.png
+++ /dev/null
Binary files differ
diff --git a/packages/SystemUI/res/drawable/ic_screenshot_delete.xml b/packages/SystemUI/res/drawable/ic_screenshot_delete.xml
new file mode 100644
index 0000000..e35c73d
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_screenshot_delete.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2015 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="32dp"
+        android:height="32dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,38c0,2.21 1.79,4 4,4h16c2.21,0 4,-1.79 4,-4V14H12v24zM38,8h-7l-2,-2H19l-2,2h-7v4h28V8z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/ic_screenshot_share.xml b/packages/SystemUI/res/drawable/ic_screenshot_share.xml
new file mode 100644
index 0000000..62733ec
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_screenshot_share.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2015 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="32dp"
+        android:height="32dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M36,32.17c-1.52,0 -2.89,0.59 -3.93,1.54L17.82,25.4c0.11,-0.45 0.18,-0.92 0.18,-1.4s-0.07,-0.95 -0.18,-1.4l14.1,-8.23c1.07,1 2.5,1.62 4.08,1.62 3.31,0 6,-2.69 6,-6s-2.69,-6 -6,-6 -6,2.69 -6,6c0,0.48 0.07,0.95 0.18,1.4l-14.1,8.23c-1.07,-1 -2.5,-1.62 -4.08,-1.62 -3.31,0 -6,2.69 -6,6s2.69,6 6,6c1.58,0 3.01,-0.62 4.08,-1.62l14.25,8.31c-0.1,0.42 -0.16,0.86 -0.16,1.31 0,3.22 2.61,5.83 5.83,5.83s5.83,-2.61 5.83,-5.83 -2.61,-5.83 -5.83,-5.83z"/>
+</vector>
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java b/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java
index f16f6bd..7e32c3a 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/GlobalScreenshot.java
@@ -253,6 +253,17 @@
             sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
             sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
 
+            OutputStream out = resolver.openOutputStream(uri);
+            image.compress(Bitmap.CompressFormat.PNG, 100, out);
+            out.flush();
+            out.close();
+
+            // Update file size in the database
+            values.clear();
+            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
+            resolver.update(uri, values, null, null);
+
+            // Create a share action for the notification
             final PendingIntent callback = PendingIntent.getBroadcast(context, 0,
                     new Intent(context, GlobalScreenshot.TargetChosenReceiver.class)
                             .putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId),
@@ -261,21 +272,19 @@
                     callback.getIntentSender());
             chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                     | Intent.FLAG_ACTIVITY_NEW_TASK);
+            mNotificationBuilder.addAction(R.drawable.ic_screenshot_share,
+                    r.getString(com.android.internal.R.string.share),
+                    PendingIntent.getActivity(context, 0, chooserIntent,
+                            PendingIntent.FLAG_CANCEL_CURRENT));
 
-            mNotificationBuilder.addAction(R.drawable.ic_menu_share,
-                     r.getString(com.android.internal.R.string.share),
-                     PendingIntent.getActivity(context, 0, chooserIntent,
-                             PendingIntent.FLAG_CANCEL_CURRENT));
-
-            OutputStream out = resolver.openOutputStream(uri);
-            image.compress(Bitmap.CompressFormat.PNG, 100, out);
-            out.flush();
-            out.close();
-
-            // update file size in the database
-            values.clear();
-            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
-            resolver.update(uri, values, null, null);
+            // Create a delete action for the notification
+            final PendingIntent deleteAction = PendingIntent.getBroadcast(context,  0,
+                    new Intent(context, GlobalScreenshot.DeleteScreenshotReceiver.class)
+                            .putExtra(GlobalScreenshot.CANCEL_ID, mNotificationId)
+                            .putExtra(GlobalScreenshot.SCREENSHOT_URI_ID, uri.toString()),
+                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
+            mNotificationBuilder.addAction(R.drawable.ic_screenshot_delete,
+                    r.getString(com.android.internal.R.string.delete), deleteAction);
 
             params[0].imageUri = uri;
             params[0].image = null;
@@ -349,6 +358,29 @@
 }
 
 /**
+ * An AsyncTask that deletes an image from the media store in the background.
+ */
+class DeleteImageInBackgroundTask extends AsyncTask<Uri, Void, Void> {
+    private static final String TAG = "DeleteImageInBackgroundTask";
+
+    private Context mContext;
+
+    DeleteImageInBackgroundTask(Context context) {
+        mContext = context;
+    }
+
+    @Override
+    protected Void doInBackground(Uri... params) {
+        if (params.length != 1) return null;
+
+        Uri screenshotUri = params[0];
+        ContentResolver resolver = mContext.getContentResolver();
+        resolver.delete(screenshotUri, null, null);
+        return null;
+    }
+}
+
+/**
  * TODO:
  *   - Performance when over gl surfaces? Ie. Gallery
  *   - what do we say in the Toast? Which icon do we get if the user uses another
@@ -358,6 +390,7 @@
     private static final String TAG = "GlobalScreenshot";
 
     static final String CANCEL_ID = "android:cancel_id";
+    static final String SCREENSHOT_URI_ID = "android:screenshot_uri_id";
 
     private static final int SCREENSHOT_FLASH_TO_PEAK_DURATION = 130;
     private static final int SCREENSHOT_DROP_IN_DURATION = 430;
@@ -761,11 +794,33 @@
                 return;
             }
 
+            // Clear the notification
             final NotificationManager nm =
                     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
-
             final int id = intent.getIntExtra(CANCEL_ID, 0);
             nm.cancel(id);
         }
     }
+
+    /**
+     * Removes the last screenshot.
+     */
+    public static class DeleteScreenshotReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (!intent.hasExtra(CANCEL_ID) || !intent.hasExtra(SCREENSHOT_URI_ID)) {
+                return;
+            }
+
+            // Clear the notification
+            final NotificationManager nm =
+                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+            final int id = intent.getIntExtra(CANCEL_ID, 0);
+            final Uri uri = Uri.parse(intent.getStringExtra(SCREENSHOT_URI_ID));
+            nm.cancel(id);
+
+            // And delete the image from the media store
+            new DeleteImageInBackgroundTask(context).execute(uri);
+        }
+    }
 }