Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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.app.PendingIntent; |
michael@0 | 13 | import android.content.Context; |
michael@0 | 14 | import android.net.Uri; |
michael@0 | 15 | import android.util.Log; |
michael@0 | 16 | |
michael@0 | 17 | import java.util.concurrent.ConcurrentHashMap; |
michael@0 | 18 | |
michael@0 | 19 | public class NotificationHandler { |
michael@0 | 20 | private final ConcurrentHashMap<Integer, Notification> |
michael@0 | 21 | mNotifications = new ConcurrentHashMap<Integer, Notification>(); |
michael@0 | 22 | private final Context mContext; |
michael@0 | 23 | private final NotificationManager mNotificationManager; |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * Notification associated with this service's foreground state. |
michael@0 | 27 | * |
michael@0 | 28 | * {@link android.app.Service#startForeground(int, android.app.Notification)} |
michael@0 | 29 | * associates the foreground with exactly one notification from the service. |
michael@0 | 30 | * To keep Fennec alive during downloads (and to make sure it can be killed |
michael@0 | 31 | * once downloads are complete), we make sure that the foreground is always |
michael@0 | 32 | * associated with an active progress notification if and only if at least |
michael@0 | 33 | * one download is in progress. |
michael@0 | 34 | */ |
michael@0 | 35 | private Notification mForegroundNotification; |
michael@0 | 36 | private int mForegroundNotificationId; |
michael@0 | 37 | |
michael@0 | 38 | public NotificationHandler(Context context) { |
michael@0 | 39 | mContext = context; |
michael@0 | 40 | mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Adds a notification. |
michael@0 | 45 | * |
michael@0 | 46 | * @param notificationID the unique ID of the notification |
michael@0 | 47 | * @param aImageUrl URL of the image to use |
michael@0 | 48 | * @param aAlertTitle title of the notification |
michael@0 | 49 | * @param aAlertText text of the notification |
michael@0 | 50 | * @param contentIntent Intent used when the notification is clicked |
michael@0 | 51 | * @param clearIntent Intent used when the notification is removed |
michael@0 | 52 | */ |
michael@0 | 53 | public void add(int notificationID, String aImageUrl, String aAlertTitle, |
michael@0 | 54 | String aAlertText, PendingIntent contentIntent) { |
michael@0 | 55 | // Remove the old notification with the same ID, if any |
michael@0 | 56 | remove(notificationID); |
michael@0 | 57 | |
michael@0 | 58 | Uri imageUri = Uri.parse(aImageUrl); |
michael@0 | 59 | int icon = BitmapUtils.getResource(imageUri, R.drawable.ic_status_logo); |
michael@0 | 60 | final AlertNotification notification = new AlertNotification(mContext, notificationID, |
michael@0 | 61 | icon, aAlertTitle, aAlertText, System.currentTimeMillis(), imageUri); |
michael@0 | 62 | |
michael@0 | 63 | notification.setLatestEventInfo(mContext, aAlertTitle, aAlertText, contentIntent); |
michael@0 | 64 | |
michael@0 | 65 | mNotificationManager.notify(notificationID, notification); |
michael@0 | 66 | mNotifications.put(notificationID, notification); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * Adds a notification. |
michael@0 | 71 | * |
michael@0 | 72 | * @param id the unique ID of the notification |
michael@0 | 73 | * @param aNotification the Notification to add |
michael@0 | 74 | */ |
michael@0 | 75 | public void add(int id, Notification notification) { |
michael@0 | 76 | mNotificationManager.notify(id, notification); |
michael@0 | 77 | mNotifications.put(id, notification); |
michael@0 | 78 | |
michael@0 | 79 | if (mForegroundNotification == null && isOngoing(notification)) { |
michael@0 | 80 | setForegroundNotification(id, notification); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /** |
michael@0 | 85 | * Updates a notification. |
michael@0 | 86 | * |
michael@0 | 87 | * @param notificationID ID of existing notification |
michael@0 | 88 | * @param aProgress progress of item being updated |
michael@0 | 89 | * @param aProgressMax max progress of item being updated |
michael@0 | 90 | * @param aAlertText text of the notification |
michael@0 | 91 | */ |
michael@0 | 92 | public void update(int notificationID, long aProgress, long aProgressMax, String aAlertText) { |
michael@0 | 93 | final Notification notification = mNotifications.get(notificationID); |
michael@0 | 94 | if (notification == null) { |
michael@0 | 95 | return; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | if (notification instanceof AlertNotification) { |
michael@0 | 99 | AlertNotification alert = (AlertNotification)notification; |
michael@0 | 100 | alert.updateProgress(aAlertText, aProgress, aProgressMax); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | if (mForegroundNotification == null && isOngoing(notification)) { |
michael@0 | 104 | setForegroundNotification(notificationID, notification); |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * Removes a notification. |
michael@0 | 110 | * |
michael@0 | 111 | * @param notificationID ID of existing notification |
michael@0 | 112 | */ |
michael@0 | 113 | public void remove(int notificationID) { |
michael@0 | 114 | final Notification notification = mNotifications.remove(notificationID); |
michael@0 | 115 | if (notification != null) { |
michael@0 | 116 | updateForegroundNotification(notificationID, notification); |
michael@0 | 117 | } |
michael@0 | 118 | mNotificationManager.cancel(notificationID); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Determines whether the service is done. |
michael@0 | 123 | * |
michael@0 | 124 | * The service is considered finished when all notifications have been |
michael@0 | 125 | * removed. |
michael@0 | 126 | * |
michael@0 | 127 | * @return whether all notifications have been removed |
michael@0 | 128 | */ |
michael@0 | 129 | public boolean isDone() { |
michael@0 | 130 | return mNotifications.isEmpty(); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Determines whether a notification should hold a foreground service to keep Gecko alive |
michael@0 | 135 | * |
michael@0 | 136 | * @param notificationID the id of the notification to check |
michael@0 | 137 | * @return whether the notification is ongoing |
michael@0 | 138 | */ |
michael@0 | 139 | public boolean isOngoing(int notificationID) { |
michael@0 | 140 | final Notification notification = mNotifications.get(notificationID); |
michael@0 | 141 | return isOngoing(notification); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Determines whether a notification should hold a foreground service to keep Gecko alive |
michael@0 | 146 | * |
michael@0 | 147 | * @param notification the notification to check |
michael@0 | 148 | * @return whether the notification is ongoing |
michael@0 | 149 | */ |
michael@0 | 150 | public boolean isOngoing(Notification notification) { |
michael@0 | 151 | if (notification != null && (isProgressStyle(notification) || ((notification.flags & Notification.FLAG_ONGOING_EVENT) > 0))) { |
michael@0 | 152 | return true; |
michael@0 | 153 | } |
michael@0 | 154 | return false; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | /** |
michael@0 | 158 | * Helper method to determines whether a notification is an AlertNotification that is showing progress |
michael@0 | 159 | * This method will be deprecated when AlertNotifications are removed (bug 893289). |
michael@0 | 160 | * |
michael@0 | 161 | * @param notification the notification to check |
michael@0 | 162 | * @return whether the notification is an AlertNotification showing progress. |
michael@0 | 163 | */ |
michael@0 | 164 | private boolean isProgressStyle(Notification notification) { |
michael@0 | 165 | if (notification != null && notification instanceof AlertNotification) { |
michael@0 | 166 | return ((AlertNotification)notification).isProgressStyle(); |
michael@0 | 167 | } |
michael@0 | 168 | return false; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | protected void setForegroundNotification(int id, Notification notification) { |
michael@0 | 172 | mForegroundNotificationId = id; |
michael@0 | 173 | mForegroundNotification = notification; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | private void updateForegroundNotification(int oldId, Notification oldNotification) { |
michael@0 | 177 | if (mForegroundNotificationId == oldId) { |
michael@0 | 178 | // If we're removing the notification associated with the |
michael@0 | 179 | // foreground, we need to pick another active notification to act |
michael@0 | 180 | // as the foreground notification. |
michael@0 | 181 | Notification foregroundNotification = null; |
michael@0 | 182 | int foregroundId = 0; |
michael@0 | 183 | for (final Integer id : mNotifications.keySet()) { |
michael@0 | 184 | final Notification notification = mNotifications.get(id); |
michael@0 | 185 | if (isOngoing(notification)) { |
michael@0 | 186 | foregroundNotification = notification; |
michael@0 | 187 | foregroundId = id; |
michael@0 | 188 | break; |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | setForegroundNotification(foregroundId, foregroundNotification); |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | } |