mobile/android/base/background/announcements/AnnouncementPresenter.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.background.announcements;
michael@0 6
michael@0 7 import java.net.URI;
michael@0 8
michael@0 9 import org.mozilla.gecko.R;
michael@0 10 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 11
michael@0 12 import android.app.Notification;
michael@0 13 import android.app.NotificationManager;
michael@0 14 import android.app.PendingIntent;
michael@0 15 import android.content.Context;
michael@0 16 import android.content.Intent;
michael@0 17 import android.net.Uri;
michael@0 18
michael@0 19 /**
michael@0 20 * Handle requests to display a fetched announcement.
michael@0 21 */
michael@0 22 public class AnnouncementPresenter {
michael@0 23
michael@0 24 /**
michael@0 25 * Display the provided snippet.
michael@0 26 * @param context
michael@0 27 * The context instance to use when obtaining the NotificationManager.
michael@0 28 * @param notificationID
michael@0 29 * A unique ID for this notification.
michael@0 30 * @param title
michael@0 31 * The *already localized* String title. Must not be null.
michael@0 32 * @param body
michael@0 33 * The *already localized* String body. Must not be null.
michael@0 34 * @param uri
michael@0 35 * The URL to open when the notification is tapped.
michael@0 36 */
michael@0 37 @SuppressWarnings("deprecation")
michael@0 38 public static void displayAnnouncement(final Context context,
michael@0 39 final int notificationID,
michael@0 40 final String title,
michael@0 41 final String body,
michael@0 42 final URI uri) {
michael@0 43 final String ns = Context.NOTIFICATION_SERVICE;
michael@0 44 final NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
michael@0 45
michael@0 46 // Set pending intent associated with the notification.
michael@0 47 Uri u = Uri.parse(uri.toASCIIString());
michael@0 48 Intent intent = new Intent(Intent.ACTION_VIEW, u);
michael@0 49
michael@0 50 // Always open the link with Fennec.
michael@0 51 intent.setClassName(GlobalConstants.BROWSER_INTENT_PACKAGE, GlobalConstants.BROWSER_INTENT_CLASS);
michael@0 52 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
michael@0 53
michael@0 54 final int icon = R.drawable.ic_status_logo;
michael@0 55
michael@0 56 // Deprecated approach to building a notification.
michael@0 57 final long when = System.currentTimeMillis();
michael@0 58 Notification notification = new Notification(icon, title, when);
michael@0 59 notification.flags = Notification.FLAG_AUTO_CANCEL;
michael@0 60 notification.setLatestEventInfo(context, title, body, contentIntent);
michael@0 61
michael@0 62 // Notification.Builder since API 11.
michael@0 63 /*
michael@0 64 Notification notification = new Notification.Builder(context)
michael@0 65 .setContentTitle(title)
michael@0 66 .setContentText(body)
michael@0 67 .setAutoCancel(true)
michael@0 68 .setContentIntent(contentIntent).getNotification();
michael@0 69 */
michael@0 70
michael@0 71 // Send notification.
michael@0 72 notificationManager.notify(notificationID, notification);
michael@0 73 }
michael@0 74
michael@0 75 public static void displayAnnouncement(final Context context,
michael@0 76 final Announcement snippet) {
michael@0 77 final int notificationID = snippet.getId();
michael@0 78 final String title = snippet.getTitle();
michael@0 79 final String body = snippet.getText();
michael@0 80 final URI uri = snippet.getUri();
michael@0 81 AnnouncementPresenter.displayAnnouncement(context, notificationID, title, body, uri);
michael@0 82 }
michael@0 83 }

mercurial