2011-04-28 12:34:50 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
2010-10-01 14:21:21 -07:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Alex Pakhotin <alexp@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.content.Context;
|
2011-04-28 12:34:50 -07:00
|
|
|
import android.graphics.*;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
2010-10-01 14:21:21 -07:00
|
|
|
import android.widget.RemoteViews;
|
2011-04-28 12:34:50 -07:00
|
|
|
import java.net.*;
|
2010-12-02 13:27:26 -08:00
|
|
|
import java.text.NumberFormat;
|
2010-10-01 14:21:21 -07:00
|
|
|
|
|
|
|
public class AlertNotification
|
|
|
|
extends Notification
|
|
|
|
{
|
|
|
|
Context mContext;
|
|
|
|
int mId;
|
|
|
|
int mIcon;
|
|
|
|
String mTitle;
|
2011-01-31 09:50:11 -08:00
|
|
|
String mText;
|
2010-10-01 14:21:21 -07:00
|
|
|
boolean mProgressStyle;
|
|
|
|
NotificationManager mNotificationManager;
|
2010-12-02 13:27:26 -08:00
|
|
|
double mPrevPercent = -1;
|
|
|
|
String mPrevAlertText = "";
|
|
|
|
static final double UPDATE_THRESHOLD = .01;
|
2010-10-01 14:21:21 -07:00
|
|
|
|
2011-01-31 09:50:11 -08:00
|
|
|
public AlertNotification(Context aContext, int aNotificationId, int aIcon,
|
|
|
|
String aTitle, String aText, long aWhen) {
|
|
|
|
super(aIcon, (aText.length() > 0) ? aText : aTitle, aWhen);
|
2010-10-01 14:21:21 -07:00
|
|
|
|
|
|
|
mContext = aContext;
|
|
|
|
mIcon = aIcon;
|
|
|
|
mTitle = aTitle;
|
2011-01-31 09:50:11 -08:00
|
|
|
mText = aText;
|
2010-10-01 14:21:21 -07:00
|
|
|
mProgressStyle = false;
|
|
|
|
mId = aNotificationId;
|
|
|
|
|
|
|
|
mNotificationManager = (NotificationManager)
|
|
|
|
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isProgressStyle() {
|
|
|
|
return mProgressStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void show() {
|
|
|
|
mNotificationManager.notify(mId, this);
|
|
|
|
}
|
|
|
|
|
2011-04-28 12:34:50 -07:00
|
|
|
public void setCustomIcon(Uri aIconUri) {
|
2011-07-25 19:12:21 -07:00
|
|
|
if (aIconUri == null || aIconUri.getScheme() == null)
|
|
|
|
return;
|
|
|
|
|
2011-04-28 12:34:50 -07:00
|
|
|
// Custom view
|
|
|
|
int layout = R.layout.notification_icon_text;
|
|
|
|
RemoteViews view = new RemoteViews(GeckoApp.mAppContext.getPackageName(), layout);
|
|
|
|
try {
|
|
|
|
URL url = new URL(aIconUri.toString());
|
|
|
|
Bitmap bm = BitmapFactory.decodeStream(url.openStream());
|
|
|
|
view.setImageViewBitmap(R.id.notificationImage, bm);
|
|
|
|
view.setTextViewText(R.id.notificationTitle, mTitle);
|
|
|
|
if (mText.length() > 0) {
|
|
|
|
view.setTextViewText(R.id.notificationText, mText);
|
|
|
|
}
|
|
|
|
contentView = view;
|
|
|
|
mNotificationManager.notify(mId, this);
|
|
|
|
} catch(Exception ex) {
|
|
|
|
Log.e("GeckoAlert", "failed to create bitmap", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 14:21:21 -07:00
|
|
|
public void updateProgress(String aAlertText, long aProgress, long aProgressMax) {
|
|
|
|
if (!mProgressStyle) {
|
|
|
|
// Custom view
|
2011-04-28 12:34:50 -07:00
|
|
|
int layout = aAlertText.length() > 0 ? R.layout.notification_progress_text : R.layout.notification_progress;
|
2010-10-01 14:21:21 -07:00
|
|
|
|
2010-12-21 12:02:14 -08:00
|
|
|
RemoteViews view = new RemoteViews(GeckoApp.mAppContext.getPackageName(), layout);
|
2010-10-01 14:21:21 -07:00
|
|
|
view.setImageViewResource(R.id.notificationImage, mIcon);
|
|
|
|
view.setTextViewText(R.id.notificationTitle, mTitle);
|
|
|
|
contentView = view;
|
2011-09-16 12:10:58 -07:00
|
|
|
flags |= FLAG_ONGOING_EVENT | FLAG_ONLY_ALERT_ONCE;
|
2010-10-01 14:21:21 -07:00
|
|
|
|
|
|
|
mProgressStyle = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String text;
|
2010-12-02 13:27:26 -08:00
|
|
|
double percent = 0;
|
|
|
|
if (aProgressMax > 0)
|
|
|
|
percent = ((double)aProgress / (double)aProgressMax);
|
|
|
|
|
|
|
|
if (aAlertText.length() > 0)
|
2010-10-01 14:21:21 -07:00
|
|
|
text = aAlertText;
|
2010-12-02 13:27:26 -08:00
|
|
|
else
|
|
|
|
text = NumberFormat.getPercentInstance().format(percent);
|
|
|
|
|
|
|
|
if (mPrevAlertText.equals(text) && Math.abs(mPrevPercent - percent) < UPDATE_THRESHOLD)
|
|
|
|
return;
|
2010-10-01 14:21:21 -07:00
|
|
|
|
|
|
|
contentView.setTextViewText(R.id.notificationText, text);
|
|
|
|
contentView.setProgressBar(R.id.notificationProgressbar, (int)aProgressMax, (int)aProgress, false);
|
|
|
|
|
|
|
|
// Update the notification
|
|
|
|
mNotificationManager.notify(mId, this);
|
2010-12-02 13:27:26 -08:00
|
|
|
|
|
|
|
mPrevPercent = percent;
|
|
|
|
mPrevAlertText = text;
|
2010-10-01 14:21:21 -07:00
|
|
|
}
|
|
|
|
}
|