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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/background/announcements/AnnouncementPresenter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.background.announcements;
     1.9 +
    1.10 +import java.net.URI;
    1.11 +
    1.12 +import org.mozilla.gecko.R;
    1.13 +import org.mozilla.gecko.background.common.GlobalConstants;
    1.14 +
    1.15 +import android.app.Notification;
    1.16 +import android.app.NotificationManager;
    1.17 +import android.app.PendingIntent;
    1.18 +import android.content.Context;
    1.19 +import android.content.Intent;
    1.20 +import android.net.Uri;
    1.21 +
    1.22 +/**
    1.23 + * Handle requests to display a fetched announcement.
    1.24 + */
    1.25 +public class AnnouncementPresenter {
    1.26 +
    1.27 +  /**
    1.28 +   * Display the provided snippet.
    1.29 +   * @param context
    1.30 +   *        The context instance to use when obtaining the NotificationManager.
    1.31 +   * @param notificationID
    1.32 +   *        A unique ID for this notification.
    1.33 +   * @param title
    1.34 +   *        The *already localized* String title. Must not be null.
    1.35 +   * @param body
    1.36 +   *        The *already localized* String body. Must not be null.
    1.37 +   * @param uri
    1.38 +   *        The URL to open when the notification is tapped.
    1.39 +   */
    1.40 +  @SuppressWarnings("deprecation")
    1.41 +  public static void displayAnnouncement(final Context context,
    1.42 +                                         final int notificationID,
    1.43 +                                         final String title,
    1.44 +                                         final String body,
    1.45 +                                         final URI uri) {
    1.46 +    final String ns = Context.NOTIFICATION_SERVICE;
    1.47 +    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
    1.48 +
    1.49 +    // Set pending intent associated with the notification.
    1.50 +    Uri u = Uri.parse(uri.toASCIIString());
    1.51 +    Intent intent = new Intent(Intent.ACTION_VIEW, u);
    1.52 +
    1.53 +    // Always open the link with Fennec.
    1.54 +    intent.setClassName(GlobalConstants.BROWSER_INTENT_PACKAGE, GlobalConstants.BROWSER_INTENT_CLASS);
    1.55 +    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
    1.56 +
    1.57 +    final int icon = R.drawable.ic_status_logo;
    1.58 +
    1.59 +    // Deprecated approach to building a notification.
    1.60 +    final long when = System.currentTimeMillis();
    1.61 +    Notification notification = new Notification(icon, title, when);
    1.62 +    notification.flags = Notification.FLAG_AUTO_CANCEL;
    1.63 +    notification.setLatestEventInfo(context, title, body, contentIntent);
    1.64 +
    1.65 +    // Notification.Builder since API 11.
    1.66 +    /*
    1.67 +    Notification notification = new Notification.Builder(context)
    1.68 +        .setContentTitle(title)
    1.69 +        .setContentText(body)
    1.70 +        .setAutoCancel(true)
    1.71 +        .setContentIntent(contentIntent).getNotification();
    1.72 +     */
    1.73 +
    1.74 +    // Send notification.
    1.75 +    notificationManager.notify(notificationID, notification);
    1.76 +  }
    1.77 +
    1.78 +  public static void displayAnnouncement(final Context context,
    1.79 +                                         final Announcement snippet) {
    1.80 +    final int notificationID = snippet.getId();
    1.81 +    final String title = snippet.getTitle();
    1.82 +    final String body = snippet.getText();
    1.83 +    final URI uri = snippet.getUri();
    1.84 +    AnnouncementPresenter.displayAnnouncement(context, notificationID, title, body, uri);
    1.85 +  }
    1.86 +}

mercurial