From ed4c0d24df7824e2ecbb843b29bb52227daac9c6 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Mon, 5 Dec 2016 23:42:58 +0100 Subject: [PATCH] Introduced NotificationHelper --- src/org/kde/kdeconnect/Device.java | 10 ++----- .../Helpers/NotificationHelper.java | 25 ++++++++++++++++ .../Plugins/PingPlugin/PingPlugin.java | 8 ++--- .../ReceiveNotificationsPlugin.java | 10 ++----- .../Plugins/SharePlugin/SharePlugin.java | 29 ++++--------------- 5 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 src/org/kde/kdeconnect/Helpers/NotificationHelper.java diff --git a/src/org/kde/kdeconnect/Device.java b/src/org/kde/kdeconnect/Device.java index 004c8bed..41b8a1e9 100644 --- a/src/org/kde/kdeconnect/Device.java +++ b/src/org/kde/kdeconnect/Device.java @@ -37,6 +37,7 @@ import android.util.Log; import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BasePairingHandler; import org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider; +import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.Plugins.PluginFactory; @@ -382,14 +383,9 @@ public class Device implements BaseLink.PackageReceiver { .build(); final NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); + NotificationHelper.notifyCompat(notificationManager, notificationId, noti); - try { - BackgroundService.addGuiInUseCounter(context); - notificationManager.notify(notificationId, noti); - } catch(Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + BackgroundService.addGuiInUseCounter(context); } public void hidePairingNotification() { diff --git a/src/org/kde/kdeconnect/Helpers/NotificationHelper.java b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java new file mode 100644 index 00000000..dbb776b7 --- /dev/null +++ b/src/org/kde/kdeconnect/Helpers/NotificationHelper.java @@ -0,0 +1,25 @@ +package org.kde.kdeconnect.Helpers; + +import android.app.Notification; +import android.app.NotificationManager; + +public class NotificationHelper { + + public static void notifyCompat(NotificationManager notificationManager, int notificationId, Notification notification) { + try { + notificationManager.notify(notificationId, notification); + } catch(Exception e) { + //4.1 will throw an exception about not having the VIBRATE permission, ignore it. + //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ + } + } + + public static void notifyCompat(NotificationManager notificationManager, String tag, int notificationId, Notification notification) { + try { + notificationManager.notify(tag, notificationId, notification); + } catch(Exception e) { + //4.1 will throw an exception about not having the VIBRATE permission, ignore it. + //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ + } + } +} diff --git a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java index b2e9f70d..b52aeb6f 100644 --- a/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/PingPlugin/PingPlugin.java @@ -30,6 +30,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.util.Log; +import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.UserInterface.MaterialActivity; @@ -89,12 +90,7 @@ public class PingPlugin extends Plugin { .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - try { - notificationManager.notify(id, noti); - } catch(Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager, id, noti); return true; diff --git a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java index 0a0f751d..d22da5f1 100644 --- a/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/ReceiveNotificationsPlugin/ReceiveNotificationsPlugin.java @@ -32,6 +32,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.util.Log; +import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect.UserInterface.MaterialActivity; @@ -114,13 +115,8 @@ public class ReceiveNotificationsPlugin extends Plugin { .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - try { - // tag all incoming notifications - notificationManager.notify("kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti); - } catch (Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager, "kdeconnectId:" + np.getString("id", "0"), np.getInt("id", 0), noti); + } return true; diff --git a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java index c2c96125..75aa057d 100644 --- a/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java +++ b/src/org/kde/kdeconnect/Plugins/SharePlugin/SharePlugin.java @@ -46,6 +46,7 @@ import android.widget.Toast; import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Helpers.FilesHelper; +import org.kde.kdeconnect.Helpers.NotificationHelper; import org.kde.kdeconnect.NetworkPackage; import org.kde.kdeconnect.Plugins.Plugin; import org.kde.kdeconnect_tp.R; @@ -139,12 +140,7 @@ public class SharePlugin extends Plugin { .setOngoing(true) .setProgress(100,0,true); - try { - notificationManager.notify(notificationId,builder.build()); - } catch(Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager,notificationId, builder.build()); new Thread(new Runnable() { @Override @@ -165,12 +161,7 @@ public class SharePlugin extends Plugin { if (progressPercentage != prevProgressPercentage) { prevProgressPercentage = progressPercentage; builder.setProgress(100, (int) progressPercentage, false); - try { - notificationManager.notify(notificationId,builder.build()); - } catch(Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build()); } } //else Log.e("SharePlugin", "Infinite loop? :D"); @@ -225,12 +216,7 @@ public class SharePlugin extends Plugin { builder.setDefaults(Notification.DEFAULT_ALL); } - try { - notificationManager.notify(notificationId,builder.build()); - } catch(Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build()); } catch (Exception e) { Log.e("SharePlugin", "Receiver thread exception"); @@ -283,12 +269,7 @@ public class SharePlugin extends Plugin { .build(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - try { - notificationManager.notify((int) System.currentTimeMillis(), noti); - } catch (Exception e) { - //4.1 will throw an exception about not having the VIBRATE permission, ignore it. - //https://android.googlesource.com/platform/frameworks/base/+/android-4.2.1_r1.2%5E%5E!/ - } + NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti); } } else { Log.e("SharePlugin", "Error: Nothing attached!");