Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.gecko;
8 import org.mozilla.gecko.gfx.BitmapUtils;
10 import android.app.Notification;
11 import android.app.NotificationManager;
12 import android.content.Context;
13 import android.graphics.Bitmap;
14 import android.net.Uri;
15 import android.util.Log;
16 import android.widget.RemoteViews;
18 import java.text.NumberFormat;
20 public class AlertNotification
21 extends Notification
22 {
23 private static final String LOGTAG = "GeckoAlertNotification";
25 private final int mId;
26 private final int mIcon;
27 private final String mTitle;
28 private final String mText;
29 private final NotificationManager mNotificationManager;
31 private boolean mProgressStyle;
32 private double mPrevPercent = -1;
33 private String mPrevAlertText = "";
35 private static final double UPDATE_THRESHOLD = .01;
36 private Context mContext;
38 public AlertNotification(Context aContext, int aNotificationId, int aIcon,
39 String aTitle, String aText, long aWhen, Uri aIconUri) {
40 super(aIcon, (aText.length() > 0) ? aText : aTitle, aWhen);
42 mIcon = aIcon;
43 mTitle = aTitle;
44 mText = aText;
45 mId = aNotificationId;
46 mContext = aContext;
48 mNotificationManager = (NotificationManager) aContext.getSystemService(Context.NOTIFICATION_SERVICE);
50 if (aIconUri == null || aIconUri.getScheme() == null)
51 return;
53 // Custom view
54 int layout = R.layout.notification_icon_text;
55 RemoteViews view = new RemoteViews(mContext.getPackageName(), layout);
56 try {
57 Bitmap bm = BitmapUtils.decodeUrl(aIconUri);
58 if (bm == null) {
59 Log.e(LOGTAG, "failed to decode icon");
60 return;
61 }
62 view.setImageViewBitmap(R.id.notification_image, bm);
63 view.setTextViewText(R.id.notification_title, mTitle);
64 if (mText.length() > 0) {
65 view.setTextViewText(R.id.notification_text, mText);
66 }
67 contentView = view;
68 } catch (Exception e) {
69 Log.e(LOGTAG, "failed to create bitmap", e);
70 }
71 }
73 public int getId() {
74 return mId;
75 }
77 public synchronized boolean isProgressStyle() {
78 return mProgressStyle;
79 }
81 public void show() {
82 mNotificationManager.notify(mId, this);
83 }
85 public void cancel() {
86 mNotificationManager.cancel(mId);
87 }
89 public synchronized void updateProgress(String aAlertText, long aProgress, long aProgressMax) {
90 if (!mProgressStyle) {
91 // Custom view
92 int layout = aAlertText.length() > 0 ? R.layout.notification_progress_text : R.layout.notification_progress;
94 RemoteViews view = new RemoteViews(mContext.getPackageName(), layout);
95 view.setImageViewResource(R.id.notification_image, mIcon);
96 view.setTextViewText(R.id.notification_title, mTitle);
97 contentView = view;
98 flags |= FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE;
100 mProgressStyle = true;
101 }
103 String text;
104 double percent = 0;
105 if (aProgressMax > 0)
106 percent = ((double)aProgress / (double)aProgressMax);
108 if (aAlertText.length() > 0)
109 text = aAlertText;
110 else
111 text = NumberFormat.getPercentInstance().format(percent);
113 if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD)
114 return;
116 contentView.setTextViewText(R.id.notification_text, text);
117 contentView.setProgressBar(R.id.notification_progressbar, (int)aProgressMax, (int)aProgress, false);
119 // Update the notification
120 mNotificationManager.notify(mId, this);
122 mPrevPercent = percent;
123 mPrevAlertText = text;
124 }
125 }