michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.gfx.BitmapUtils; michael@0: michael@0: import android.app.Notification; michael@0: import android.app.NotificationManager; michael@0: import android.content.Context; michael@0: import android.graphics.Bitmap; michael@0: import android.net.Uri; michael@0: import android.util.Log; michael@0: import android.widget.RemoteViews; michael@0: michael@0: import java.text.NumberFormat; michael@0: michael@0: public class AlertNotification michael@0: extends Notification michael@0: { michael@0: private static final String LOGTAG = "GeckoAlertNotification"; michael@0: michael@0: private final int mId; michael@0: private final int mIcon; michael@0: private final String mTitle; michael@0: private final String mText; michael@0: private final NotificationManager mNotificationManager; michael@0: michael@0: private boolean mProgressStyle; michael@0: private double mPrevPercent = -1; michael@0: private String mPrevAlertText = ""; michael@0: michael@0: private static final double UPDATE_THRESHOLD = .01; michael@0: private Context mContext; michael@0: michael@0: public AlertNotification(Context aContext, int aNotificationId, int aIcon, michael@0: String aTitle, String aText, long aWhen, Uri aIconUri) { michael@0: super(aIcon, (aText.length() > 0) ? aText : aTitle, aWhen); michael@0: michael@0: mIcon = aIcon; michael@0: mTitle = aTitle; michael@0: mText = aText; michael@0: mId = aNotificationId; michael@0: mContext = aContext; michael@0: michael@0: mNotificationManager = (NotificationManager) aContext.getSystemService(Context.NOTIFICATION_SERVICE); michael@0: michael@0: if (aIconUri == null || aIconUri.getScheme() == null) michael@0: return; michael@0: michael@0: // Custom view michael@0: int layout = R.layout.notification_icon_text; michael@0: RemoteViews view = new RemoteViews(mContext.getPackageName(), layout); michael@0: try { michael@0: Bitmap bm = BitmapUtils.decodeUrl(aIconUri); michael@0: if (bm == null) { michael@0: Log.e(LOGTAG, "failed to decode icon"); michael@0: return; michael@0: } michael@0: view.setImageViewBitmap(R.id.notification_image, bm); michael@0: view.setTextViewText(R.id.notification_title, mTitle); michael@0: if (mText.length() > 0) { michael@0: view.setTextViewText(R.id.notification_text, mText); michael@0: } michael@0: contentView = view; michael@0: } catch (Exception e) { michael@0: Log.e(LOGTAG, "failed to create bitmap", e); michael@0: } michael@0: } michael@0: michael@0: public int getId() { michael@0: return mId; michael@0: } michael@0: michael@0: public synchronized boolean isProgressStyle() { michael@0: return mProgressStyle; michael@0: } michael@0: michael@0: public void show() { michael@0: mNotificationManager.notify(mId, this); michael@0: } michael@0: michael@0: public void cancel() { michael@0: mNotificationManager.cancel(mId); michael@0: } michael@0: michael@0: public synchronized void updateProgress(String aAlertText, long aProgress, long aProgressMax) { michael@0: if (!mProgressStyle) { michael@0: // Custom view michael@0: int layout = aAlertText.length() > 0 ? R.layout.notification_progress_text : R.layout.notification_progress; michael@0: michael@0: RemoteViews view = new RemoteViews(mContext.getPackageName(), layout); michael@0: view.setImageViewResource(R.id.notification_image, mIcon); michael@0: view.setTextViewText(R.id.notification_title, mTitle); michael@0: contentView = view; michael@0: flags |= FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE; michael@0: michael@0: mProgressStyle = true; michael@0: } michael@0: michael@0: String text; michael@0: double percent = 0; michael@0: if (aProgressMax > 0) michael@0: percent = ((double)aProgress / (double)aProgressMax); michael@0: michael@0: if (aAlertText.length() > 0) michael@0: text = aAlertText; michael@0: else michael@0: text = NumberFormat.getPercentInstance().format(percent); michael@0: michael@0: if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD) michael@0: return; michael@0: michael@0: contentView.setTextViewText(R.id.notification_text, text); michael@0: contentView.setProgressBar(R.id.notification_progressbar, (int)aProgressMax, (int)aProgress, false); michael@0: michael@0: // Update the notification michael@0: mNotificationManager.notify(mId, this); michael@0: michael@0: mPrevPercent = percent; michael@0: mPrevAlertText = text; michael@0: } michael@0: }