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