From 1766c82ab8c667a2c0dcb4d03ee2212a676640b9 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 4 Apr 2015 23:20:28 +0200 Subject: [PATCH] In NotificationListener return early if BluetoothCommunicationService is not running This is to pervent our service from being started when we get a notification even if Gadgetbridge is not in use and no devices are connected. 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 --- .../gadgetbridge/NotificationListener.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java index f1d936cef..5334b1e67 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java @@ -1,5 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge; +import android.app.ActivityManager; import android.app.Notification; import android.content.Intent; import android.content.SharedPreferences; @@ -26,6 +27,24 @@ public class NotificationListener extends NotificationListenerService { @Override public void onNotificationPosted(StatusBarNotification sbn) { + /* + * return early if BluetoothCommunicationService 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 (BluetoothCommunicationService.class.getName().equals(service.service.getClassName())) { + isServiceRunning = true; + } + } + + if (!isServiceRunning) { + return; + } String source = sbn.getPackageName(); Log.i(TAG, source);