|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko; |
|
7 |
|
8 import org.mozilla.gecko.gfx.BitmapUtils; |
|
9 |
|
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; |
|
17 |
|
18 import java.text.NumberFormat; |
|
19 |
|
20 public class AlertNotification |
|
21 extends Notification |
|
22 { |
|
23 private static final String LOGTAG = "GeckoAlertNotification"; |
|
24 |
|
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; |
|
30 |
|
31 private boolean mProgressStyle; |
|
32 private double mPrevPercent = -1; |
|
33 private String mPrevAlertText = ""; |
|
34 |
|
35 private static final double UPDATE_THRESHOLD = .01; |
|
36 private Context mContext; |
|
37 |
|
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); |
|
41 |
|
42 mIcon = aIcon; |
|
43 mTitle = aTitle; |
|
44 mText = aText; |
|
45 mId = aNotificationId; |
|
46 mContext = aContext; |
|
47 |
|
48 mNotificationManager = (NotificationManager) aContext.getSystemService(Context.NOTIFICATION_SERVICE); |
|
49 |
|
50 if (aIconUri == null || aIconUri.getScheme() == null) |
|
51 return; |
|
52 |
|
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 } |
|
72 |
|
73 public int getId() { |
|
74 return mId; |
|
75 } |
|
76 |
|
77 public synchronized boolean isProgressStyle() { |
|
78 return mProgressStyle; |
|
79 } |
|
80 |
|
81 public void show() { |
|
82 mNotificationManager.notify(mId, this); |
|
83 } |
|
84 |
|
85 public void cancel() { |
|
86 mNotificationManager.cancel(mId); |
|
87 } |
|
88 |
|
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; |
|
93 |
|
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; |
|
99 |
|
100 mProgressStyle = true; |
|
101 } |
|
102 |
|
103 String text; |
|
104 double percent = 0; |
|
105 if (aProgressMax > 0) |
|
106 percent = ((double)aProgress / (double)aProgressMax); |
|
107 |
|
108 if (aAlertText.length() > 0) |
|
109 text = aAlertText; |
|
110 else |
|
111 text = NumberFormat.getPercentInstance().format(percent); |
|
112 |
|
113 if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD) |
|
114 return; |
|
115 |
|
116 contentView.setTextViewText(R.id.notification_text, text); |
|
117 contentView.setProgressBar(R.id.notification_progressbar, (int)aProgressMax, (int)aProgress, false); |
|
118 |
|
119 // Update the notification |
|
120 mNotificationManager.notify(mId, this); |
|
121 |
|
122 mPrevPercent = percent; |
|
123 mPrevAlertText = text; |
|
124 } |
|
125 } |