Android Open Source - led-notifier Notification Service






From Project

Back to project page led-notifier.

License

The source code is released under:

Apache License

If you think the Android project led-notifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ciubotariu_levy.lednotifier;
//ww w .  ja v  a 2  s .c  om
import android.annotation.TargetApi;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.provider.Telephony;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationService extends NotificationListenerService {
  private static final String TAG = NotificationService.class.getName();
  private static final String KEY_TIE_NOTIFICATION = "tie_to_sms_app";
  private static final String KEY_DELAY_DISMISS = "delay_dismissal";
  private static final int DELAY_MILLIS = 5000;

  protected static boolean isNotificationListenerServiceOn = false;
  private Notification mCurrentNotification = null;
  private boolean mTieNotification = false;
  private boolean mDelayDismissal = false;

  private Handler mHandler = new Handler();
  private Runnable mDismissNotification = new Runnable(){
    @Override
    public void run(){
      NotificationUtils.cancel(NotificationService.this);
      mCurrentNotification = null;
    }
  };

  private SharedPreferences.OnSharedPreferenceChangeListener prefListener = new OnSharedPreferenceChangeListener (){

    @Override
    public void onSharedPreferenceChanged(
        SharedPreferences sharedPreferences, String key) {
      if (KEY_TIE_NOTIFICATION.equals(key)){
        mTieNotification = sharedPreferences.getBoolean(KEY_TIE_NOTIFICATION, false);
      }
      else if (KEY_DELAY_DISMISS.equals(key)){
        mDelayDismissal = sharedPreferences.getBoolean(KEY_DELAY_DISMISS, false);
        mHandler.removeCallbacks(mDismissNotification);
      }
    }
  };

  private SMSReceiver mSmsReceiver = new SMSReceiver(){

    @Override
    public void onNotificationReady(Context context, Notification notif, boolean ledTimeout){
      mCurrentNotification = notif;
      if (notif != null && context != null){
        NotificationUtils.notify (context, notif,ledTimeout);
      }
    }
  };

  @TargetApi(19)
  @Override
  public void onCreate(){
    super.onCreate();
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPrefs.registerOnSharedPreferenceChangeListener(prefListener);
    mTieNotification = sharedPrefs.getBoolean(KEY_TIE_NOTIFICATION, false);
    mDelayDismissal = sharedPrefs.getBoolean(KEY_DELAY_DISMISS, false);
    String filterAction = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ?
        Telephony.Sms.Intents.SMS_RECEIVED_ACTION :
          "android.provider.Telephony.SMS_RECEIVED";
    IntentFilter filter = new IntentFilter (filterAction);
    filter.setPriority(999);
    registerReceiver(mSmsReceiver, filter);
  }

  @Override
  public IBinder onBind(Intent intent){
    isNotificationListenerServiceOn = true;
    return super.onBind(intent);
  }

  @Override
  public void onDestroy (){
    PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefListener);
    unregisterReceiver(mSmsReceiver);
    isNotificationListenerServiceOn = false;
    super.onDestroy();
  }

  @Override
  public void onNotificationPosted(StatusBarNotification sbn) {
    Log.v(TAG, "Notification received by system");
    if (sbn.getPackageName().equals(getPackageName())){
      mCurrentNotification = sbn.getNotification();
    }
    if (isMessagingApp(sbn.getPackageName())){
      mHandler.removeCallbacks(mDismissNotification);
    }
//    if (mCurrentNotification != null && isMessagingApp(sbn.getPackageName())){
//      boolean ledTimeout = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(SMSReceiver.KEY_TIMEOUT_LED, false);
//      NotificationUtils.notify (this, mCurrentNotification, ledTimeout);//TODO check if this is needed
//    }
  }


  @Override
  public void onNotificationRemoved(StatusBarNotification sbn) {
    if (mTieNotification && isMessagingApp(sbn.getPackageName())){
      /*((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(SMSReceiver.NOTIFICATION_ID);
      mCurrentNotification = null;*/
      if (mDelayDismissal){
        mHandler.postAtTime(mDismissNotification, SystemClock.uptimeMillis()+DELAY_MILLIS);
      }
      else{
        mDismissNotification.run();
      }
    }
  }

  @TargetApi(Build.VERSION_CODES.KITKAT)
  private boolean isMessagingApp (String packageName){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
      return packageName.equals(Telephony.Sms.getDefaultSmsPackage(this));
    }
    return !packageName.equals(getPackageName())
        &&(packageName.equals(PreferenceManager.getDefaultSharedPreferences(this).getString(SmsAppChooserDialog.KEY_SMS_APP_PACKAGE, null))
            ||packageName.contains("mms")
            ||packageName.contains("sms") 
            ||packageName.contains("messaging")
            ||packageName.contains("message")
            ||packageName.contains("talk"));
  }
}




Java Source Code List

com.ciubotariu_levy.lednotifier.AboutActivity.java
com.ciubotariu_levy.lednotifier.AlarmDismissReceiver.java
com.ciubotariu_levy.lednotifier.BoldCheckedView.java
com.ciubotariu_levy.lednotifier.BootBroadcastReceiver.java
com.ciubotariu_levy.lednotifier.CircularColorView.java
com.ciubotariu_levy.lednotifier.ColorDialog.java
com.ciubotariu_levy.lednotifier.ColorVibrateDialog.java
com.ciubotariu_levy.lednotifier.ContactsFragment.java
com.ciubotariu_levy.lednotifier.CustomContactsFragment.java
com.ciubotariu_levy.lednotifier.DataFetcher.java
com.ciubotariu_levy.lednotifier.DefaultColorChooserContainer.java
com.ciubotariu_levy.lednotifier.FastScrollThemedListView.java
com.ciubotariu_levy.lednotifier.LEDCancelReceiver.java
com.ciubotariu_levy.lednotifier.MainActivity.java
com.ciubotariu_levy.lednotifier.NotificationService.java
com.ciubotariu_levy.lednotifier.NotificationUtils.java
com.ciubotariu_levy.lednotifier.ObserverService.java
com.ciubotariu_levy.lednotifier.SMSAppAdapter.java
com.ciubotariu_levy.lednotifier.SMSAppChooserContainer.java
com.ciubotariu_levy.lednotifier.SMSReceiver.java
com.ciubotariu_levy.lednotifier.SectionedCursorAdapter.java
com.ciubotariu_levy.lednotifier.SettingsActivity.java
com.ciubotariu_levy.lednotifier.SmsAppChooserDialog.java
com.ciubotariu_levy.lednotifier.providers.LedContactInfo.java
com.ciubotariu_levy.lednotifier.providers.LedContactProvider.java
com.ciubotariu_levy.lednotifier.providers.LedContacts.java
com.larswerkman.holocolorpicker.ColorPicker.java
com.larswerkman.holocolorpicker.EndColorPicker.java
com.larswerkman.holocolorpicker.LinearColorPicker.java
com.larswerkman.holocolorpicker.OnColorChangedListener.java
com.larswerkman.holocolorpicker.OpacityBar.java
com.larswerkman.holocolorpicker.SVBar.java
com.larswerkman.holocolorpicker.SaturationBar.java
com.larswerkman.holocolorpicker.ValueBar.java
com.makeramen.RoundedDrawable.java
com.makeramen.RoundedTransformationBuilder.java