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