Bug 1195692 - Replace AlertNotification with NotificationCompat.Builder based implementation. r=mcomella

This commit is contained in:
Sebastian Kaspari 2015-09-23 19:43:31 +02:00
parent 1fd5710f2b
commit c75e092f99
4 changed files with 22 additions and 157 deletions

View File

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

View File

@ -12,6 +12,8 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import java.util.concurrent.ConcurrentHashMap;
@ -20,7 +22,7 @@ public class NotificationHandler {
private final ConcurrentHashMap<Integer, Notification>
mNotifications = new ConcurrentHashMap<Integer, Notification>();
private final Context mContext;
private final NotificationManager mNotificationManager;
private final NotificationManagerCompat mNotificationManager;
/**
* Notification associated with this service's foreground state.
@ -37,7 +39,7 @@ public class NotificationHandler {
public NotificationHandler(Context context) {
mContext = context;
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager = NotificationManagerCompat.from(context);
}
/**
@ -48,7 +50,6 @@ public class NotificationHandler {
* @param aAlertTitle title of the notification
* @param aAlertText text of the notification
* @param contentIntent Intent used when the notification is clicked
* @param clearIntent Intent used when the notification is removed
*/
public void add(int notificationID, String aImageUrl, String aAlertTitle,
String aAlertText, PendingIntent contentIntent) {
@ -57,10 +58,14 @@ public class NotificationHandler {
Uri imageUri = Uri.parse(aImageUrl);
int icon = BitmapUtils.getResource(imageUri, R.drawable.ic_status_logo);
final AlertNotification notification = new AlertNotification(mContext, notificationID,
icon, aAlertTitle, aAlertText, System.currentTimeMillis(), imageUri);
notification.setLatestEventInfo(mContext, aAlertTitle, aAlertText, contentIntent);
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle(aAlertTitle)
.setContentText(aAlertText)
.setSmallIcon(icon)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();
mNotificationManager.notify(notificationID, notification);
mNotifications.put(notificationID, notification);
@ -70,7 +75,7 @@ public class NotificationHandler {
* Adds a notification.
*
* @param id the unique ID of the notification
* @param aNotification the Notification to add
* @param notification the Notification to add
*/
public void add(int id, Notification notification) {
mNotificationManager.notify(id, notification);
@ -90,19 +95,20 @@ public class NotificationHandler {
* @param aAlertText text of the notification
*/
public void update(int notificationID, long aProgress, long aProgressMax, String aAlertText) {
final Notification notification = mNotifications.get(notificationID);
Notification notification = mNotifications.get(notificationID);
if (notification == null) {
return;
}
if (notification instanceof AlertNotification) {
AlertNotification alert = (AlertNotification)notification;
alert.updateProgress(aAlertText, aProgress, aProgressMax);
}
notification = new NotificationCompat.Builder(mContext)
.setContentText(aAlertText)
.setSmallIcon(notification.icon)
.setWhen(notification.when)
.setContentIntent(notification.contentIntent)
.setProgress((int) aProgressMax, (int) aProgress, false)
.build();
if (mForegroundNotification == null && isOngoing(notification)) {
setForegroundNotification(notificationID, notification);
}
add(notificationID, notification);
}
/**
@ -148,26 +154,12 @@ public class NotificationHandler {
* @return whether the notification is ongoing
*/
public boolean isOngoing(Notification notification) {
if (notification != null && (isProgressStyle(notification) || ((notification.flags & Notification.FLAG_ONGOING_EVENT) > 0))) {
if (notification != null && (notification.flags & Notification.FLAG_ONGOING_EVENT) > 0) {
return true;
}
return false;
}
/**
* Helper method to determines whether a notification is an AlertNotification that is showing progress
* This method will be deprecated when AlertNotifications are removed (bug 893289).
*
* @param notification the notification to check
* @return whether the notification is an AlertNotification showing progress.
*/
private boolean isProgressStyle(Notification notification) {
if (notification instanceof AlertNotification) {
return ((AlertNotification)notification).isProgressStyle();
}
return false;
}
protected void setForegroundNotification(int id, Notification notification) {
mForegroundNotificationId = id;
mForegroundNotification = notification;

View File

@ -46,7 +46,6 @@ show allResults
[middle] = \
org.mozilla.gecko.prompts.* \
org.mozilla.gecko.AlertNotification \
org.mozilla.gecko.FormAssistPopup \
org.mozilla.gecko.GeckoActivity \
org.mozilla.gecko.GeckoApp \

View File

@ -156,7 +156,6 @@ gbjar.sources += [
'ActionModeCompat.java',
'ActionModeCompatView.java',
'ActivityHandlerHelper.java',
'AlertNotification.java',
'AndroidGamepadManager.java',
'animation/AnimationUtils.java',
'animation/AnimatorProxy.java',