package nodomain.freeyourgadget.gadgetbridge.externalevents;

import android.app.ActivityManager;
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.v4.content.LocalBroadcastManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;

public class NotificationListener extends NotificationListenerService {

    private static final Logger LOG = LoggerFactory.getLogger(NotificationListener.class);

    public static final String ACTION_DISMISS
            = "nodomain.freeyourgadget.gadgetbridge.notificationlistener.action.dismiss";

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_DISMISS)) {
                NotificationListener.this.cancelAllNotifications();
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filterLocal = new IntentFilter();
        filterLocal.addAction(ACTION_DISMISS);
        LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
    }

    @Override
    public void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
        super.onDestroy();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
       /*
        * return early if DeviceCommunicationService is not running,
        * else the service would get started every time we get a notification.
        * unfortunately we cannot enable/disable NotificationListener at runtime like we do with
        * broadcast receivers because it seems to invalidate the permissions that are
        * neccessery for NotificationListenerService
        */
        boolean isServiceRunning = false;
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (DeviceCommunicationService.class.getName().equals(service.service.getClassName())) {
                isServiceRunning = true;
            }
        }

        if (!isServiceRunning) {
            return;
        }

        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        if (!sharedPrefs.getBoolean("notifications_generic_whenscreenon", false)) {
            PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
            if (powermanager.isScreenOn()) {
                return;
            }
        }

        String source = sbn.getPackageName();
        Notification notification = sbn.getNotification();

        if ((notification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
            return;
        }

        /* do not display messages from "android"
         * This includes keyboard selection message, usb connection messages, etc
         * Hope it does not filter out too much, we will see...
         */

        if (source.equals("android") ||
                source.equals("com.android.systemui") ||
                source.equals("com.android.dialer") ||
                source.equals("com.android.mms") ||
                source.equals("com.cyanogenmod.eleven") ||
                source.equals("com.fsck.k9")) {
            return;
        }

        if (source.equals("eu.siacs.conversations")) {
            if (!"never".equals(sharedPrefs.getString("notification_mode_pebblemsg", "when_screen_off"))) {
                return;
            }
        }

        LOG.info("Processing notification from source " + source);

        Bundle extras = notification.extras;
        String title = extras.getCharSequence(Notification.EXTRA_TITLE).toString();
        String content = null;
        if (extras.containsKey(Notification.EXTRA_TEXT)) {
            CharSequence contentCS = extras.getCharSequence(Notification.EXTRA_TEXT);
            if (contentCS != null) {
                content = contentCS.toString();
            }
        }

        if (content != null) {
            Intent startIntent = new Intent(NotificationListener.this, DeviceCommunicationService.class);
            startIntent.setAction(DeviceCommunicationService.ACTION_NOTIFICATION_GENERIC);
            startIntent.putExtra("notification_title", title);
            startIntent.putExtra("notification_body", content);
            startService(startIntent);
        }

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

    }
}
@@ -2244,13 +2244,12 @@
  nec
-c
 ess
-e
+a
 ry f
@@ -2302,412 +2302,8 @@
     
-boolean isServiceRunning = false;%0A        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);%0A        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) %7B%0A            if (DeviceCommunicationService.class.getName().equals(service.service.getClassName())) %7B%0A                isServiceRunning = true;%0A            %7D%0A        %7D%0A%0A        
 if (
@@ -2319,16 +2319,18 @@
 eRunning
+()
 ) %7B%0A    
@@ -4607,32 +4607,451 @@
       %7D%0A%0A    %7D%0A%0A
+    private boolean isServiceRunning() %7B%0A        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);%0A        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) %7B%0A            if (DeviceCommunicationService.class.getName().equals(service.service.getClassName())) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
     @Override%0A  

Extract a method for easier reuse if necessary