Bug 612703 - Don't always update notification progressbars. Android only. [r=blassey, a=blocking-fennec]
authorWes Johnston <wjohnston@mozilla.com>
Thu, 02 Dec 2010 13:27:26 -0800
changeset 58512 5ae1f2fa0d9f2b9560d5aecc28d4b945a78db941
parent 58511 be1f9b4e522e94d7db7488d52d8394b0dc883c56
child 58513 2c24ce211f2b7ee5223adec98c82b56178053b03
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersblassey, blocking-fennec
bugs612703
milestone2.0b8pre
Bug 612703 - Don't always update notification progressbars. Android only. [r=blassey, a=blocking-fennec]
embedding/android/AlertNotification.java
--- a/embedding/android/AlertNotification.java
+++ b/embedding/android/AlertNotification.java
@@ -36,26 +36,30 @@
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.content.Context;
 import android.widget.RemoteViews;
+import java.text.NumberFormat;
 
 public class AlertNotification
     extends Notification
 {
     Context mContext;
     int mId;
     int mIcon;
     String mTitle;
     boolean mProgressStyle;
     NotificationManager mNotificationManager;
+    double mPrevPercent  = -1;
+    String mPrevAlertText = "";
+    static final double UPDATE_THRESHOLD = .01;
 
     public AlertNotification(Context aContext, int aNotificationId, int aIcon, String aTitle, long aWhen) {
         super(aIcon, aTitle, aWhen);
 
         mContext = aContext;
         mIcon = aIcon;
         mTitle = aTitle;
         mProgressStyle = false;
@@ -83,24 +87,30 @@ public class AlertNotification
             view.setTextViewText(R.id.notificationTitle, mTitle);
             contentView = view;
             flags |= FLAG_ONGOING_EVENT;
 
             mProgressStyle = true;
         }
 
         String text;
-        if (aAlertText.length() > 0) {
+        double percent = 0;
+        if (aProgressMax > 0)
+            percent = ((double)aProgress / (double)aProgressMax);
+
+        if (aAlertText.length() > 0)
             text = aAlertText;
-        } else {
-            int percent = 0;
-            if (aProgressMax > 0)
-                percent = (int)(100 * aProgress / aProgressMax);
-            text = percent + "%";
-        }
+        else
+            text = NumberFormat.getPercentInstance().format(percent); 
+
+        if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD)
+            return;
 
         contentView.setTextViewText(R.id.notificationText, text);
         contentView.setProgressBar(R.id.notificationProgressbar, (int)aProgressMax, (int)aProgress, false);
 
         // Update the notification
         mNotificationManager.notify(mId, this);
+
+        mPrevPercent = percent;
+        mPrevAlertText = text;
     }
 }