Bug 932816 - Add a 'persistent' flag to the api in order to avoid to clear downloads notifications. r=wesj, a=bajaj
authorWes Johnston <wjohnston@mozilla.com>
Tue, 19 Nov 2013 10:31:31 -0500
changeset 166538 b5eabc6c76ddd11ba2bfc7836cdefc0b5f15c969
parent 166537 78c9a2d8a5a9a3359ad7667c14350a47653c78fd
child 166539 7cffbb50b68cffa91bc5e6387d37d482c141b16e
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswesj, bajaj
bugs932816
milestone27.0a2
Bug 932816 - Add a 'persistent' flag to the api in order to avoid to clear downloads notifications. r=wesj, a=bajaj
mobile/android/base/GeckoApp.java
mobile/android/base/NotificationHelper.java
mobile/android/chrome/content/downloads.js
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -2062,16 +2062,17 @@ abstract public class GeckoApp
         if (mFormAssistPopup != null)
             mFormAssistPopup.destroy();
         if (mContactService != null)
             mContactService.destroy();
         if (mPromptService != null)
             mPromptService.destroy();
         if (mTextSelection != null)
             mTextSelection.destroy();
+        NotificationHelper.destroy();
 
         if (SmsManager.getInstance() != null) {
             SmsManager.getInstance().stop();
             if (isFinishing())
                 SmsManager.getInstance().shutdown();
         }
 
         final BrowserHealthRecorder rec = mHealthRecorder;
--- a/mobile/android/base/NotificationHelper.java
+++ b/mobile/android/base/NotificationHelper.java
@@ -46,37 +46,38 @@ public final class NotificationHelper im
     private static final String WHEN_ATTR = "when";
     private static final String LARGE_ICON_ATTR = "largeIcon";
     private static final String COOKIE_ATTR = "cookie";
     private static final String EVENT_TYPE_ATTR = "eventType";
     private static final String ACTIONS_ATTR = "actions";
     private static final String ACTION_ATTR = "actionKind";
     private static final String ACTION_TITLE_ATTR = "title";
     private static final String ACTION_ICON_ATTR = "icon";
+    private static final String PERSISTENT_ATTR = "persistent";
 
     private static final String NOTIFICATION_SCHEME = "moz-notification";
 
     private static final String CLICK_EVENT = "notification-clicked";
     private static final String CLEARED_EVENT = "notification-cleared";
 
     private static Context mContext;
-    private static Set<String> mShowing;
+    private static Set<String> mClearableNotifications;
     private static BroadcastReceiver mReceiver;
     private static NotificationHelper mInstance;
 
     private NotificationHelper() {
     }
 
     public static void init(Context context) {
         if (mInstance != null) {
             Log.w(LOGTAG, "NotificationHelper.init() called twice!");
         }
         mInstance = new NotificationHelper();
         mContext = context;
-        mShowing = new HashSet<String>();
+        mClearableNotifications = new HashSet<String>();
         registerEventListener("Notification:Show");
         registerEventListener("Notification:Hide");
         registerReceiver(context);
     }
 
     private static void registerEventListener(String event) {
         GeckoAppShell.getEventDispatcher().registerEventListener(event, mInstance);
     }
@@ -119,17 +120,17 @@ public final class NotificationHelper im
         if (id == null || notificationType == null) {
             Log.w(LOGTAG, "handleNotificationEvent: invalid intent parameters");
             return;
         }
 
         // In case the user swiped out the notification, we empty the id
         // set.
         if (CLEARED_EVENT.equals(notificationType)) {
-            mShowing.remove(id);
+            mClearableNotifications.remove(id);
         }
 
         // If the notification was clicked, we are closing it.
         if (CLICK_EVENT.equals(notificationType) && !i.getBooleanExtra(ONGOING_ATTR, false)) {
             hideNotification(id);
         }
         String cookie = data.getQueryParameter(COOKIE_ATTR);
 
@@ -243,18 +244,22 @@ public final class NotificationHelper im
         }
 
         PendingIntent pi = buildNotificationPendingIntent(message, CLICK_EVENT);
         builder.setContentIntent(pi);
         PendingIntent deletePendingIntent = buildNotificationPendingIntent(message, CLEARED_EVENT);
         builder.setDeleteIntent(deletePendingIntent);
 
         GeckoAppShell.sNotificationClient.add(id.hashCode(), builder.build());
-        if (!mShowing.contains(id)) {
-            mShowing.add(id);
+
+        boolean persistent = message.optBoolean(PERSISTENT_ATTR);
+        // We add only not persistent notifications to the list since we want to purge only
+        // them when geckoapp is destroyed.
+        if (!persistent && !mClearableNotifications.contains(id)) {
+            mClearableNotifications.add(id);
         }
     }
 
     private void hideNotification(JSONObject message) {
         String id;
         try {
             id = message.getString("id");
         } catch (JSONException ex) {
@@ -262,17 +267,23 @@ public final class NotificationHelper im
             return;
         }
 
         hideNotification(id);
     }
 
     public void hideNotification(String id) {
         GeckoAppShell.sNotificationClient.remove(id.hashCode());
-        mShowing.remove(id);
+        mClearableNotifications.remove(id);
     }
 
     private void clearAll() {
-        for (String id : mShowing) {
+        for (String id : mClearableNotifications) {
             hideNotification(id);
         }
     }
+
+    public static void destroy() {
+        if (mInstance != null) {
+            mInstance.clearAll();
+        }
+    }
 }
--- a/mobile/android/chrome/content/downloads.js
+++ b/mobile/android/chrome/content/downloads.js
@@ -107,16 +107,17 @@ var Downloads = {
       msg.progress_value = aOptions.percentComplete;
       msg.progress_max = 100;
     }
     if (aOptions && aOptions.actions) {
       msg.actions = aOptions.actions;
     }
     if (aOptions && aOptions.ongoing) {
       msg.ongoing = aOptions.ongoing;
+      msg.persistent = true;
     }
     this._bridge.handleGeckoMessage(JSON.stringify(msg));
   },
 
   removeNotification: function dl_removeNotification(aDownload) {
     let msg = {
         type: "Notification:Hide",
         id: this.getNotificationIdFromDownload(aDownload)