NotificationHandler.java :  » RSS » andreader » com » froger » andreader » Handlers » Android Open Source

Android Open Source » RSS » andreader 
andreader » com » froger » andreader » Handlers » NotificationHandler.java
package com.froger.andreader.Handlers;

import com.froger.andreader.Activities.AppPrefs;

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 android.content.SharedPreferences;

public class NotificationHandler {
  private Notification notification;
  private NotificationManager notificationManager;
  
  private Activity callerActivity;
  private Intent intentToStart;
  private long intentExtra;
  private SharedPreferences preferences;
  private String notificationHeader;
  private String notificationContent;
  
  private boolean vibrate = false;
  private boolean led = false;
  private boolean sound = false;
  
  public static final int VIBRATE = 1;
  public static final int LED = 2;
  public static final int SOUND = 3;
  public static final int ALL = 4;
  
  public NotificationHandler(Activity callerActivity, SharedPreferences preferences,
                 Intent intentToStart, Long intentExtra, 
                 int ico, String title, String header, String content) {
    this.callerActivity    = callerActivity;
    this.intentToStart     = intentToStart;
    this.preferences     = preferences;
    this.intentExtra     = intentExtra;
    this.notificationHeader  = header;
    this.notificationContent = content;
    
    notificationManager = 
      (NotificationManager)callerActivity
      .getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(ico, title, System.currentTimeMillis());
  }
  
  //Wcz/wycz wibracj/diod/dwik w powiadomieniu
  public void setOption(int option, boolean value) {
    switch (option) {
    case VIBRATE:
      vibrate = value;
      break;
    case LED:
      led = value;
      break;
    case SOUND:
      sound = value;
      break;
    case ALL:
      vibrate = led = sound = value;
      break;
  
    default:
      break;
    }
  }
  
  //Przygotuj powiadomienie
  private void cookNotification() {
    //Ustaw flag odwoujc powiadomienie po klikniciu
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    
    //Ustaw dodatkowe opcje
    notification.defaults = 0;
    if(vibrate || preferences.getBoolean(AppPrefs.PREF_NOTIFY_VIBRATE, false)) 
      notification.defaults |= Notification.DEFAULT_VIBRATE;
    if(led || preferences.getBoolean(AppPrefs.PREF_NOTIFY_LED, false)) {
      notification.flags |= Notification.FLAG_SHOW_LIGHTS;
      notification.defaults |= Notification.DEFAULT_LIGHTS;
    }
    if(sound || preferences.getBoolean(AppPrefs.PREF_NOTIFY_SOUND, false))
      notification.defaults |= Notification.DEFAULT_SOUND;
    
    //Dodaj wartosc feedId do Intencji
    //aby poprawnie uruchomi aktywno listy artykuw
    intentToStart.putExtra("feedId", intentExtra);
    
    //Stwrz PendingIntent
    PendingIntent pendingIntent = 
      PendingIntent.getActivity(callerActivity, 0, intentToStart, 
                    PendingIntent.FLAG_UPDATE_CURRENT);
    //Ustaw informacje o wydarzeniu
    notification.setLatestEventInfo(callerActivity, notificationHeader, 
                    notificationContent, pendingIntent);
  }

  //Wywietl powiadomienie
  public void showNotification() {
    cookNotification();
    notificationManager.notify(0, notification);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.