Bug 1282222 - Add a 'deleteIntent' parameter to the 'NotificationClient.add' method ;r?jchen draft
authorSUN Haitao <sunhaitao@devtaste.com>
Sat, 25 Jun 2016 23:17:50 +0800
changeset 381371 205eab4a3e79b7439182b854e2a7ef1a38eee39c
parent 381370 e847d74def4b758c90f36f47659b4f40faf00150
child 381372 409ff3b09ce875cbca762a4e143497d3fdc56e74
push id21455
push userbmo:sunhaitao@devtaste.com
push dateSun, 26 Jun 2016 12:04:13 +0000
reviewersjchen
bugs1282222
milestone50.0a1
Bug 1282222 - Add a 'deleteIntent' parameter to the 'NotificationClient.add' method ;r?jchen MozReview-Commit-ID: GOwCi2LkFTm
mobile/android/base/java/org/mozilla/gecko/GeckoAppShell.java
mobile/android/base/java/org/mozilla/gecko/NotificationClient.java
mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoAppShell.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoAppShell.java
@@ -891,27 +891,38 @@ public class GeckoAppShell
     public static void setNotificationClient(NotificationClient client) {
         if (notificationClient == null) {
             notificationClient = client;
         } else {
             Log.d(LOGTAG, "Notification client already set");
         }
     }
 
+    private static PendingIntent makePersistentNotificationIntent(
+            int aNotificationID, String aType, String aPersistentData) {
+        Uri.Builder b = new Uri.Builder();
+        Uri u = b.scheme("notification-event").path(Integer.toString(aNotificationID))
+                .appendQueryParameter("type", aType)
+                .build();
+        Intent intent = GeckoService.getIntentToCreateServices(
+                getApplicationContext(), aType, aPersistentData);
+        intent.setData(u);
+        return PendingIntent.getService(
+                getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+    }
+
     @WrapForJNI(stubName = "ShowPersistentAlertNotificationWrapper")
     public static void showPersistentAlertNotification(
-          String aPersistentData,
-          String aImageUrl, String aAlertTitle, String aAlertText,
-          String aAlertCookie, String aAlertName, String aHost) {
-        Intent notificationIntent = GeckoService.getIntentToCreateServices(
-                getApplicationContext(), "persistent-notification-click", aPersistentData);
+            String aPersistentData,
+            String aImageUrl, String aAlertTitle, String aAlertText,
+            String aAlertCookie, String aAlertName, String aHost) {
         int notificationID = aAlertName.hashCode();
-        PendingIntent contentIntent = PendingIntent.getService(
-                getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
-        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent);
+        PendingIntent clickIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-click", aPersistentData);
+        PendingIntent closeIntent = makePersistentNotificationIntent(notificationID, "persistent-notification-close", aPersistentData);
+        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, clickIntent, closeIntent);
     }
 
     @WrapForJNI(stubName = "ShowAlertNotificationWrapper")
     public static void showAlertNotification(String aImageUrl, String aAlertTitle, String aAlertText, String aAlertCookie, String aAlertName, String aHost) {
         // The intent to launch when the user clicks the expanded notification
         Intent notificationIntent = new Intent(GeckoApp.ACTION_ALERT_CALLBACK);
         notificationIntent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
         notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -926,17 +937,17 @@ public class GeckoAppShell
                                        .build();
         notificationIntent.setData(dataUri);
         PendingIntent contentIntent = PendingIntent.getActivity(
                 getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
 
         ALERT_COOKIES.put(aAlertName, aAlertCookie);
         callObserver(aAlertName, "alertshow", aAlertCookie);
 
-        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent);
+        notificationClient.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent, null);
     }
 
     @WrapForJNI
     public static void alertsProgressListener_OnProgress(String aAlertName, long aProgress, long aProgressMax, String aAlertText) {
         int notificationID = aAlertName.hashCode();
         notificationClient.update(notificationID, aProgress, aProgressMax, aAlertText);
     }
 
--- a/mobile/android/base/java/org/mozilla/gecko/NotificationClient.java
+++ b/mobile/android/base/java/org/mozilla/gecko/NotificationClient.java
@@ -67,24 +67,25 @@ public abstract class NotificationClient
 
             mHandler.update(mNotificationID, progress, progressMax, alertText);
         }
     }
 
     /**
      * Adds a notification.
      *
-     * @see NotificationHandler#add(int, String, String, String, PendingIntent, PendingIntent)
+     * @see NotificationHandler#add(int, String, String, String, String, PendingIntent, PendingIntent)
      */
     public synchronized void add(final int notificationID, final String aImageUrl, final String aHost,
-            final String aAlertTitle, final String aAlertText, final PendingIntent contentIntent) {
+                                 final String aAlertTitle, final String aAlertText,
+                                 final PendingIntent contentIntent, final PendingIntent deleteIntent) {
         mTaskQueue.add(new Runnable() {
             @Override
             public void run() {
-                mHandler.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent);
+                mHandler.add(notificationID, aImageUrl, aHost, aAlertTitle, aAlertText, contentIntent, deleteIntent);
             }
         });
         notify();
 
         if (!mReady) {
             bind();
         }
     }
--- a/mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
+++ b/mobile/android/base/java/org/mozilla/gecko/NotificationHandler.java
@@ -44,27 +44,29 @@ public class NotificationHandler {
     /**
      * Adds a notification.
      *
      * @param notificationID the unique ID of the notification
      * @param aImageUrl      URL of the image to use
      * @param aAlertTitle    title of the notification
      * @param aAlertText     text of the notification
      * @param contentIntent  Intent used when the notification is clicked
+     * @param deleteIntent   Intent used when the notification is closed
      */
     public void add(final int notificationID, String aImageUrl, String aHost, String aAlertTitle,
-                    String aAlertText, PendingIntent contentIntent) {
+                    String aAlertText, PendingIntent contentIntent, PendingIntent deleteIntent) {
         // Remove the old notification with the same ID, if any
         remove(notificationID);
 
         final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                 .setContentTitle(aAlertTitle)
                 .setContentText(aAlertText)
                 .setSmallIcon(R.drawable.ic_status_logo)
                 .setContentIntent(contentIntent)
+                .setDeleteIntent(deleteIntent)
                 .setAutoCancel(true)
                 .setStyle(new NotificationCompat.InboxStyle()
                           .addLine(aAlertText)
                           .setSummaryText(aHost));
 
         // Fetch icon.
         if (!aImageUrl.isEmpty()) {
             final Bitmap image = BitmapUtils.decodeUrl(aImageUrl);