michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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: import org.mozilla.gecko.util.GeckoEventListener; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.app.NotificationManager; michael@0: import android.app.PendingIntent; michael@0: import android.content.BroadcastReceiver; michael@0: import android.content.IntentFilter; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.graphics.Bitmap; michael@0: import android.net.Uri; michael@0: import android.support.v4.app.NotificationCompat; michael@0: import android.util.Log; michael@0: michael@0: import java.util.Iterator; michael@0: import java.util.Set; michael@0: import java.util.HashSet; michael@0: michael@0: public final class NotificationHelper implements GeckoEventListener { michael@0: public static final String NOTIFICATION_ID = "NotificationHelper_ID"; michael@0: private static final String LOGTAG = "GeckoNotificationManager"; michael@0: private static final String HELPER_NOTIFICATION = "helperNotif"; michael@0: private static final String HELPER_BROADCAST_ACTION = AppConstants.ANDROID_PACKAGE_NAME + ".helperBroadcastAction"; michael@0: michael@0: // Attributes mandatory to be used while sending a notification from js. michael@0: private static final String TITLE_ATTR = "title"; michael@0: private static final String TEXT_ATTR = "text"; michael@0: private static final String ID_ATTR = "id"; michael@0: private static final String SMALLICON_ATTR = "smallIcon"; michael@0: michael@0: // Attributes that can be used while sending a notification from js. michael@0: private static final String PROGRESS_VALUE_ATTR = "progress_value"; michael@0: private static final String PROGRESS_MAX_ATTR = "progress_max"; michael@0: private static final String PROGRESS_INDETERMINATE_ATTR = "progress_indeterminate"; michael@0: private static final String LIGHT_ATTR = "light"; michael@0: private static final String ONGOING_ATTR = "ongoing"; michael@0: private static final String WHEN_ATTR = "when"; michael@0: private static final String PRIORITY_ATTR = "priority"; michael@0: private static final String LARGE_ICON_ATTR = "largeIcon"; michael@0: private static final String EVENT_TYPE_ATTR = "eventType"; michael@0: private static final String ACTIONS_ATTR = "actions"; michael@0: private static final String ACTION_ID_ATTR = "buttonId"; michael@0: private static final String ACTION_TITLE_ATTR = "title"; michael@0: private static final String ACTION_ICON_ATTR = "icon"; michael@0: private static final String PERSISTENT_ATTR = "persistent"; michael@0: michael@0: private static final String NOTIFICATION_SCHEME = "moz-notification"; michael@0: michael@0: private static final String BUTTON_EVENT = "notification-button-clicked"; michael@0: private static final String CLICK_EVENT = "notification-clicked"; michael@0: private static final String CLEARED_EVENT = "notification-cleared"; michael@0: private static final String CLOSED_EVENT = "notification-closed"; michael@0: michael@0: private static Context mContext; michael@0: private static Set mClearableNotifications; michael@0: private static BroadcastReceiver mReceiver; michael@0: private static NotificationHelper mInstance; michael@0: michael@0: private NotificationHelper() { michael@0: } michael@0: michael@0: public static void init(Context context) { michael@0: if (mInstance != null) { michael@0: Log.w(LOGTAG, "NotificationHelper.init() called twice!"); michael@0: return; michael@0: } michael@0: mInstance = new NotificationHelper(); michael@0: mContext = context; michael@0: mClearableNotifications = new HashSet(); michael@0: registerEventListener("Notification:Show"); michael@0: registerEventListener("Notification:Hide"); michael@0: registerReceiver(context); michael@0: } michael@0: michael@0: private static void registerEventListener(String event) { michael@0: GeckoAppShell.getEventDispatcher().registerEventListener(event, mInstance); michael@0: } michael@0: michael@0: @Override michael@0: public void handleMessage(String event, JSONObject message) { michael@0: if (event.equals("Notification:Show")) { michael@0: showNotification(message); michael@0: } else if (event.equals("Notification:Hide")) { michael@0: hideNotification(message); michael@0: } michael@0: } michael@0: michael@0: public boolean isHelperIntent(Intent i) { michael@0: return i.getBooleanExtra(HELPER_NOTIFICATION, false); michael@0: } michael@0: michael@0: private static void registerReceiver(Context context) { michael@0: IntentFilter filter = new IntentFilter(HELPER_BROADCAST_ACTION); michael@0: // Scheme is needed, otherwise only broadcast with no data will be catched. michael@0: filter.addDataScheme(NOTIFICATION_SCHEME); michael@0: mReceiver = new BroadcastReceiver() { michael@0: @Override michael@0: public void onReceive(Context context, Intent intent) { michael@0: mInstance.handleNotificationIntent(intent); michael@0: } michael@0: }; michael@0: context.registerReceiver(mReceiver, filter); michael@0: } michael@0: michael@0: michael@0: private void handleNotificationIntent(Intent i) { michael@0: final Uri data = i.getData(); michael@0: if (data == null) { michael@0: Log.w(LOGTAG, "handleNotificationEvent: empty data"); michael@0: return; michael@0: } michael@0: final String id = data.getQueryParameter(ID_ATTR); michael@0: final String notificationType = data.getQueryParameter(EVENT_TYPE_ATTR); michael@0: if (id == null || notificationType == null) { michael@0: Log.w(LOGTAG, "handleNotificationEvent: invalid intent parameters"); michael@0: return; michael@0: } michael@0: michael@0: // In case the user swiped out the notification, we empty the id michael@0: // set. michael@0: if (CLEARED_EVENT.equals(notificationType)) { michael@0: mClearableNotifications.remove(id); michael@0: } michael@0: michael@0: if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { michael@0: JSONObject args = new JSONObject(); michael@0: try { michael@0: args.put(ID_ATTR, id); michael@0: args.put(EVENT_TYPE_ATTR, notificationType); michael@0: michael@0: if (BUTTON_EVENT.equals(notificationType)) { michael@0: final String actionName = data.getQueryParameter(ACTION_ID_ATTR); michael@0: args.put(ACTION_ID_ATTR, actionName); michael@0: } michael@0: michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Notification:Event", args.toString())); michael@0: } catch (JSONException e) { michael@0: Log.w(LOGTAG, "Error building JSON notification arguments.", e); michael@0: } michael@0: } michael@0: // If the notification was clicked, we are closing it. This must be executed after michael@0: // sending the event to js side because when the notification is canceled no event can be michael@0: // handled. michael@0: if (CLICK_EVENT.equals(notificationType) && !i.getBooleanExtra(ONGOING_ATTR, false)) { michael@0: hideNotification(id); michael@0: } michael@0: michael@0: } michael@0: michael@0: private Uri.Builder getNotificationBuilder(JSONObject message, String type) { michael@0: Uri.Builder b = new Uri.Builder(); michael@0: b.scheme(NOTIFICATION_SCHEME).appendQueryParameter(EVENT_TYPE_ATTR, type); michael@0: michael@0: try { michael@0: final String id = message.getString(ID_ATTR); michael@0: b.appendQueryParameter(ID_ATTR, id); michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "buildNotificationPendingIntent, error parsing", ex); michael@0: } michael@0: return b; michael@0: } michael@0: michael@0: private Intent buildNotificationIntent(JSONObject message, Uri.Builder builder) { michael@0: Intent notificationIntent = new Intent(HELPER_BROADCAST_ACTION); michael@0: final boolean ongoing = message.optBoolean(ONGOING_ATTR); michael@0: notificationIntent.putExtra(ONGOING_ATTR, ongoing); michael@0: michael@0: final Uri dataUri = builder.build(); michael@0: notificationIntent.setData(dataUri); michael@0: notificationIntent.putExtra(HELPER_NOTIFICATION, true); michael@0: return notificationIntent; michael@0: } michael@0: michael@0: private PendingIntent buildNotificationPendingIntent(JSONObject message, String type) { michael@0: Uri.Builder builder = getNotificationBuilder(message, type); michael@0: final Intent notificationIntent = buildNotificationIntent(message, builder); michael@0: PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); michael@0: return pi; michael@0: } michael@0: michael@0: private PendingIntent buildButtonClickPendingIntent(JSONObject message, JSONObject action) { michael@0: Uri.Builder builder = getNotificationBuilder(message, BUTTON_EVENT); michael@0: try { michael@0: // Action name must be in query uri, otherwise buttons pending intents michael@0: // would be collapsed. michael@0: if(action.has(ACTION_ID_ATTR)) { michael@0: builder.appendQueryParameter(ACTION_ID_ATTR, action.getString(ACTION_ID_ATTR)); michael@0: } else { michael@0: Log.i(LOGTAG, "button event with no name"); michael@0: } michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "buildNotificationPendingIntent, error parsing", ex); michael@0: } michael@0: final Intent notificationIntent = buildNotificationIntent(message, builder); michael@0: PendingIntent res = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); michael@0: return res; michael@0: } michael@0: michael@0: private void showNotification(JSONObject message) { michael@0: NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext); michael@0: michael@0: // These attributes are required michael@0: final String id; michael@0: try { michael@0: builder.setContentTitle(message.getString(TITLE_ATTR)); michael@0: builder.setContentText(message.getString(TEXT_ATTR)); michael@0: id = message.getString(ID_ATTR); michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "Error parsing", ex); michael@0: return; michael@0: } michael@0: michael@0: Uri imageUri = Uri.parse(message.optString(SMALLICON_ATTR)); michael@0: builder.setSmallIcon(BitmapUtils.getResource(imageUri, R.drawable.ic_status_logo)); michael@0: michael@0: JSONArray light = message.optJSONArray(LIGHT_ATTR); michael@0: if (light != null && light.length() == 3) { michael@0: try { michael@0: builder.setLights(light.getInt(0), michael@0: light.getInt(1), michael@0: light.getInt(2)); michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "Error parsing", ex); michael@0: } michael@0: } michael@0: michael@0: boolean ongoing = message.optBoolean(ONGOING_ATTR); michael@0: builder.setOngoing(ongoing); michael@0: michael@0: if (message.has(WHEN_ATTR)) { michael@0: long when = message.optLong(WHEN_ATTR); michael@0: builder.setWhen(when); michael@0: } michael@0: michael@0: if (message.has(PRIORITY_ATTR)) { michael@0: int priority = message.optInt(PRIORITY_ATTR); michael@0: builder.setPriority(priority); michael@0: } michael@0: michael@0: if (message.has(LARGE_ICON_ATTR)) { michael@0: Bitmap b = BitmapUtils.getBitmapFromDataURI(message.optString(LARGE_ICON_ATTR)); michael@0: builder.setLargeIcon(b); michael@0: } michael@0: michael@0: if (message.has(PROGRESS_VALUE_ATTR) && michael@0: message.has(PROGRESS_MAX_ATTR) && michael@0: message.has(PROGRESS_INDETERMINATE_ATTR)) { michael@0: try { michael@0: final int progress = message.getInt(PROGRESS_VALUE_ATTR); michael@0: final int progressMax = message.getInt(PROGRESS_MAX_ATTR); michael@0: final boolean progressIndeterminate = message.getBoolean(PROGRESS_INDETERMINATE_ATTR); michael@0: builder.setProgress(progressMax, progress, progressIndeterminate); michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "Error parsing", ex); michael@0: } michael@0: } michael@0: michael@0: JSONArray actions = message.optJSONArray(ACTIONS_ATTR); michael@0: if (actions != null) { michael@0: try { michael@0: for (int i = 0; i < actions.length(); i++) { michael@0: JSONObject action = actions.getJSONObject(i); michael@0: final PendingIntent pending = buildButtonClickPendingIntent(message, action); michael@0: final String actionTitle = action.getString(ACTION_TITLE_ATTR); michael@0: final Uri actionImage = Uri.parse(action.optString(ACTION_ICON_ATTR)); michael@0: builder.addAction(BitmapUtils.getResource(actionImage, R.drawable.ic_status_logo), michael@0: actionTitle, michael@0: pending); michael@0: } michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "Error parsing", ex); michael@0: } michael@0: } michael@0: michael@0: PendingIntent pi = buildNotificationPendingIntent(message, CLICK_EVENT); michael@0: builder.setContentIntent(pi); michael@0: PendingIntent deletePendingIntent = buildNotificationPendingIntent(message, CLEARED_EVENT); michael@0: builder.setDeleteIntent(deletePendingIntent); michael@0: michael@0: GeckoAppShell.notificationClient.add(id.hashCode(), builder.build()); michael@0: michael@0: boolean persistent = message.optBoolean(PERSISTENT_ATTR); michael@0: // We add only not persistent notifications to the list since we want to purge only michael@0: // them when geckoapp is destroyed. michael@0: if (!persistent && !mClearableNotifications.contains(id)) { michael@0: mClearableNotifications.add(id); michael@0: } michael@0: } michael@0: michael@0: private void hideNotification(JSONObject message) { michael@0: String id; michael@0: try { michael@0: id = message.getString("id"); michael@0: } catch (JSONException ex) { michael@0: Log.i(LOGTAG, "Error parsing", ex); michael@0: return; michael@0: } michael@0: michael@0: hideNotification(id); michael@0: } michael@0: michael@0: private void sendNotificationWasClosed(String id) { michael@0: if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { michael@0: return; michael@0: } michael@0: JSONObject args = new JSONObject(); michael@0: try { michael@0: args.put(ID_ATTR, id); michael@0: args.put(EVENT_TYPE_ATTR, CLOSED_EVENT); michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Notification:Event", args.toString())); michael@0: } catch (JSONException ex) { michael@0: Log.w(LOGTAG, "sendNotificationWasClosed: error building JSON notification arguments.", ex); michael@0: } michael@0: } michael@0: michael@0: private void closeNotification(String id) { michael@0: GeckoAppShell.notificationClient.remove(id.hashCode()); michael@0: sendNotificationWasClosed(id); michael@0: } michael@0: michael@0: public void hideNotification(String id) { michael@0: mClearableNotifications.remove(id); michael@0: closeNotification(id); michael@0: } michael@0: michael@0: private void clearAll() { michael@0: for (Iterator i = mClearableNotifications.iterator(); i.hasNext();) { michael@0: final String id = i.next(); michael@0: i.remove(); michael@0: closeNotification(id); michael@0: } michael@0: } michael@0: michael@0: public static void destroy() { michael@0: if (mInstance != null) { michael@0: mInstance.clearAll(); michael@0: } michael@0: } michael@0: }