Subversion Repositories ES

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

1
/**
 * A simple notifier for Activities, encapsulating all the work necessary
 * to send a notification message on Android.
 */

package de.pointedears.converter.helpers;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import de.pointedears.converter.R;
import de.pointedears.converter.R.drawable;

/**
 * Sends notification messages for {@link Activity Activities}
 *
 * @author Thomas 'PointedEars' Lahn
 */

public class Notifier
{
  private static int nextId = 1;

  /**
   *
   */

  public Context activityContext;

  /**
   * @param activityContext
   *          The activity for which a notification will be sent. This
   *          allows the user to select the notification and go back
   *          directly to the activity that issued it.
   * @param tickerText
   *          The text to be displayed as notification, both as ticker message
   *          and in the notification list.
   * @param notificationTitle
   *          The title for the notification message. The default (
   *          <code>null</code>)
   *          is the application name (R.strings.app_name).
   * @see Notifier#sendMessage(Context, CharSequence)
   */

  public static void sendMessage(Context activityContext,
    CharSequence tickerText, CharSequence notificationTitle)
  {
    NotificationManager mNotificationManager =
      (NotificationManager) activityContext
        .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification =
      new Notification(drawable.icon, tickerText, System.currentTimeMillis());

    Context applicationContext =
      activityContext.getApplicationContext();

    Intent notificationIntent =
      new Intent(activityContext, activityContext.getClass());

    PendingIntent contentIntent =
      PendingIntent.getActivity(activityContext, 0,
        notificationIntent, 0);

    if (notificationTitle == null)
    {
      notificationTitle = activityContext.getString(R.string.app_name);
    }

    CharSequence contentTitle = notificationTitle;
    CharSequence contentText = tickerText;

    notification.setLatestEventInfo(applicationContext, contentTitle,
      contentText,
      contentIntent);

    mNotificationManager.notify(Notifier.nextId, notification);
    ++Notifier.nextId;
  }

  /**
   * Sends a notification message where the the application name
   * (R.strings.app_name)
   * is used as message title.
   *
   * @param activityContext
   *          The activity for which a notification will be sent
   * @param tickerText
   *          The text to be displayed as notification
   * @see Notifier#sendMessage(Context, CharSequence, CharSequence)
   */

  public static void sendMessage(Context activityContext,
    CharSequence tickerText)
  {
    Notifier.sendMessage(activityContext, tickerText, null);
  }
}