diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationReceiver.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationReceiver.java --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationReceiver.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationReceiver.java @@ -35,10 +35,14 @@ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) public class NotificationReceiver extends NotificationListenerService { + private boolean connected; + public interface NotificationListener { void onNotificationPosted(StatusBarNotification statusBarNotification); void onNotificationRemoved(StatusBarNotification statusBarNotification); + + void onListenerConnected(NotificationReceiver service); } private final ArrayList listeners = new ArrayList<>(); @@ -66,6 +70,24 @@ } } + @Override + public void onListenerConnected() { + super.onListenerConnected(); + for (NotificationListener listener : listeners) { + listener.onListenerConnected(this); + } + connected = true; + } + + @Override + public void onListenerDisconnected() { + super.onListenerDisconnected(); + connected = false; + } + + public boolean isConnected() { + return connected; + } //To use the service from the outer (name)space diff --git a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java --- a/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/NotificationsPlugin/NotificationsPlugin.java @@ -58,6 +58,7 @@ private final static String PACKAGE_TYPE_NOTIFICATION_REPLY = "kdeconnect.notification.reply"; private Map pendingIntents; + private boolean serviceReady; @Override public String getDisplayName() { @@ -91,27 +92,25 @@ @Override public boolean onCreate() { + + if (!hasPermission()) return false; + pendingIntents = new HashMap<>(); - if (hasPermission()) { - NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() { - @Override - public void onServiceStart(NotificationReceiver service) { - try { - service.addListener(NotificationsPlugin.this); - StatusBarNotification[] notifications = service.getActiveNotifications(); - for (StatusBarNotification notification : notifications) { - sendNotification(notification, true); - } - } catch (Exception e) { - Log.e("NotificationsPlugin", "Exception"); - e.printStackTrace(); - } + NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() { + @Override + public void onServiceStart(NotificationReceiver service) { + + service.addListener(NotificationsPlugin.this); + + serviceReady = service.isConnected(); + + if (serviceReady) { + sendCurrentNotifications(service); } - }); - } else { - return false; - } + } + }); + return true; } @@ -126,6 +125,11 @@ }); } + @Override + public void onListenerConnected(NotificationReceiver service) { + serviceReady = true; + sendCurrentNotifications(service); + } @Override public void onNotificationRemoved(StatusBarNotification statusBarNotification) { @@ -142,10 +146,10 @@ @Override public void onNotificationPosted(StatusBarNotification statusBarNotification) { - sendNotification(statusBarNotification, false); + sendNotification(statusBarNotification); } - private void sendNotification(StatusBarNotification statusBarNotification, boolean requestAnswer) { + private void sendNotification(StatusBarNotification statusBarNotification) { Notification notification = statusBarNotification.getNotification(); AppDatabase appDatabase = new AppDatabase(context); @@ -225,10 +229,6 @@ np.set("title", getNotificationTitle(notification)); np.set("text", getNotificationText(notification)); np.set("time", Long.toString(statusBarNotification.getPostTime())); - if (requestAnswer) { - np.set("requestAnswer", true); - np.set("silent", true); - } device.sendPackage(np); } @@ -399,44 +399,26 @@ return ticker; } + private void sendCurrentNotifications(NotificationReceiver service) { + StatusBarNotification[] notifications = service.getActiveNotifications(); + for (StatusBarNotification notification : notifications) { + sendNotification(notification); + } + } @Override public boolean onPackageReceived(final NetworkPackage np) { if (np.getBoolean("request")) { - NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() { - private void sendCurrentNotifications(NotificationReceiver service) { - StatusBarNotification[] notifications = service.getActiveNotifications(); - for (StatusBarNotification notification : notifications) { - sendNotification(notification, true); - } - } - - - @Override - public void onServiceStart(final NotificationReceiver service) { - try { - //If service just started, this call will throw an exception because the answer is not ready yet + if (serviceReady) { + NotificationReceiver.RunCommand(context, new NotificationReceiver.InstanceCallback() { + @Override + public void onServiceStart(NotificationReceiver service) { sendCurrentNotifications(service); - } catch (Exception e) { - Log.e("onPackageReceived", "Error when answering 'request': Service failed to start. Retrying in 100ms..."); - new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(100); - Log.e("onPackageReceived", "Error when answering 'request': Service failed to start. Retrying..."); - sendCurrentNotifications(service); - } catch (Exception e) { - Log.e("onPackageReceived", "Error when answering 'request': Service failed to start twice!"); - e.printStackTrace(); - } - } - }).start(); } - } - }); + }); + } } else if (np.has("cancel")) {