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.background.announcements; michael@0: michael@0: import java.net.URI; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.background.common.GlobalConstants; michael@0: michael@0: import android.app.Notification; michael@0: import android.app.NotificationManager; michael@0: import android.app.PendingIntent; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.net.Uri; michael@0: michael@0: /** michael@0: * Handle requests to display a fetched announcement. michael@0: */ michael@0: public class AnnouncementPresenter { michael@0: michael@0: /** michael@0: * Display the provided snippet. michael@0: * @param context michael@0: * The context instance to use when obtaining the NotificationManager. michael@0: * @param notificationID michael@0: * A unique ID for this notification. michael@0: * @param title michael@0: * The *already localized* String title. Must not be null. michael@0: * @param body michael@0: * The *already localized* String body. Must not be null. michael@0: * @param uri michael@0: * The URL to open when the notification is tapped. michael@0: */ michael@0: @SuppressWarnings("deprecation") michael@0: public static void displayAnnouncement(final Context context, michael@0: final int notificationID, michael@0: final String title, michael@0: final String body, michael@0: final URI uri) { michael@0: final String ns = Context.NOTIFICATION_SERVICE; michael@0: final NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns); michael@0: michael@0: // Set pending intent associated with the notification. michael@0: Uri u = Uri.parse(uri.toASCIIString()); michael@0: Intent intent = new Intent(Intent.ACTION_VIEW, u); michael@0: michael@0: // Always open the link with Fennec. michael@0: intent.setClassName(GlobalConstants.BROWSER_INTENT_PACKAGE, GlobalConstants.BROWSER_INTENT_CLASS); michael@0: PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); michael@0: michael@0: final int icon = R.drawable.ic_status_logo; michael@0: michael@0: // Deprecated approach to building a notification. michael@0: final long when = System.currentTimeMillis(); michael@0: Notification notification = new Notification(icon, title, when); michael@0: notification.flags = Notification.FLAG_AUTO_CANCEL; michael@0: notification.setLatestEventInfo(context, title, body, contentIntent); michael@0: michael@0: // Notification.Builder since API 11. michael@0: /* michael@0: Notification notification = new Notification.Builder(context) michael@0: .setContentTitle(title) michael@0: .setContentText(body) michael@0: .setAutoCancel(true) michael@0: .setContentIntent(contentIntent).getNotification(); michael@0: */ michael@0: michael@0: // Send notification. michael@0: notificationManager.notify(notificationID, notification); michael@0: } michael@0: michael@0: public static void displayAnnouncement(final Context context, michael@0: final Announcement snippet) { michael@0: final int notificationID = snippet.getId(); michael@0: final String title = snippet.getTitle(); michael@0: final String body = snippet.getText(); michael@0: final URI uri = snippet.getUri(); michael@0: AnnouncementPresenter.displayAnnouncement(context, notificationID, title, body, uri); michael@0: } michael@0: }