Example usage for android.service.notification StatusBarNotification isOngoing

List of usage examples for android.service.notification StatusBarNotification isOngoing

Introduction

In this page you can find the example usage for android.service.notification StatusBarNotification isOngoing.

Prototype

public boolean isOngoing() 

Source Link

Document

Convenience method to check the notification's flags for Notification#FLAG_ONGOING_EVENT .

Usage

From source file:com.yangtsaosoftware.pebblemessenger.services.NotificationService.java

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    if (sbn == null)
        return;//from  w  w w  . j  a v a 2 s  .c  om
    Constants.log(LOG_TAG, "New Access Event:" + sbn.getPackageName() + " tag:" + sbn.getTag());
    /*if(event.getEventType()!= AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){
            
    return;
    }
    */
    PowerManager powMan = (PowerManager) this.getSystemService(POWER_SERVICE);
    if (!notifScreenOn && powMan.isScreenOn()) {
        Constants.log(LOG_TAG, "because screen out!");
        return;
    }
    //Parcelable parcelable=event.getParcelableData();
    //if(!(parcelable instanceof Notification)) return;
    Notification notif = sbn.getNotification();
    if (sbn.isOngoing()) {
        Constants.log(LOG_TAG, "because is Ongoing!");
        return;
    }

    String eventPackageName;

    if (sbn.getPackageName() != null) {
        eventPackageName = sbn.getPackageName();
    } else {
        Constants.log(LOG_TAG, "Can't get event package name. Returning.");
        return;
    }

    boolean found = false;
    for (String packageName : packages) {
        if (packageName.equalsIgnoreCase(eventPackageName)) {
            found = true;
            break;
        }
    }
    if (!found) {
        Constants.log(LOG_TAG, eventPackageName + " was not found in the include list. Returning.");
        return;
    }
    String title = eventPackageName.substring(eventPackageName.lastIndexOf('.') + 1);
    // get the notification text
    Bundle notiBundle = notif.extras;
    StringBuilder notifySb = new StringBuilder();
    CharSequence notifyChars = notiBundle.getCharSequence(Notification.EXTRA_TITLE);
    if (notifyChars != null) {
        notifySb.append(notifyChars);
    } else {
        Constants.log(LOG_TAG, "empty message title,return!");
        return;
    }

    CharSequence bodyCS = notiBundle.getCharSequence(Notification.EXTRA_TEXT);
    if (bodyCS != null) {
        notifySb.append(":");
        notifySb.append(bodyCS);
    } else {
        Constants.log(LOG_TAG, "empty message body,return!" + notifySb.toString());
        return;
    }
    bodyCS = notiBundle.getCharSequence(Notification.EXTRA_SUB_TEXT);
    if (bodyCS != null) {
        notifySb.append(bodyCS);
    }

    Message msg = Message.obtain();
    msg.what = MessageProcessingService.MSG_NEW_MESSAGE;
    Bundle b = new Bundle();
    b.putString(MessageDbHandler.COL_MESSAGE_APP, title);
    b.putString(MessageDbHandler.COL_MESSAGE_CONTENT, notifySb.toString());
    Constants.log(LOG_TAG, "Send new message title:" + title + " body:" + notifySb.toString());
    msg.setData(b);
    try {
        rMessageProcessHandler.send(msg);
    } catch (RemoteException e) {
        e.printStackTrace();
        Constants.log(LOG_TAG, "Error when sending message to MessageProcessingService.");
    }
}