From 9cb32c1574a3c7969946fb92d79b0faac0fa2eb6 Mon Sep 17 00:00:00 2001 From: Vagrant User Date: Fri, 1 Dec 2017 11:03:09 +0100 Subject: [PATCH] Add support for native notifications Replace current TrayIcon notifications with system-native notifications. Supports most of the operating systems (the important ones are Windows, Linux and OS X). Resolves: #230 Signed-off-by: Tomas Slusny --- runelite-client/pom.xml | 15 ++++++- .../java/net/runelite/client/RuneLite.java | 45 ++++++++++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/runelite-client/pom.xml b/runelite-client/pom.xml index e0ee002cb8..4bae77ec9e 100644 --- a/runelite-client/pom.xml +++ b/runelite-client/pom.xml @@ -75,6 +75,17 @@ gson 2.4 + + fr.jcgay.send-notification + send-notification + 0.14.0 + + + com.squareup.okhttp + okhttp + + + org.pushingpixels substance @@ -92,7 +103,7 @@ 1.16.18 provided - + net.runelite api @@ -115,7 +126,7 @@ http-api ${project.version} - + junit junit diff --git a/runelite-client/src/main/java/net/runelite/client/RuneLite.java b/runelite-client/src/main/java/net/runelite/client/RuneLite.java index 80ed82b309..c96fb1297e 100644 --- a/runelite-client/src/main/java/net/runelite/client/RuneLite.java +++ b/runelite-client/src/main/java/net/runelite/client/RuneLite.java @@ -43,6 +43,7 @@ import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; +import java.net.URL; import java.util.concurrent.ScheduledExecutorService; import javax.imageio.ImageIO; import javax.inject.Singleton; @@ -51,6 +52,12 @@ import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; + +import fr.jcgay.notification.Application; +import fr.jcgay.notification.Icon; +import fr.jcgay.notification.Notification; +import fr.jcgay.notification.Notifier; +import fr.jcgay.notification.SendNotification; import joptsimple.OptionParser; import joptsimple.OptionSet; import lombok.extern.slf4j.Slf4j; @@ -74,8 +81,10 @@ public class RuneLite public static final File PROFILES_DIR = new File(RUNELITE_DIR, "profiles"); public static final File SESSION_FILE = new File(RUNELITE_DIR, "session"); public static final File PLUGIN_DIR = new File(RUNELITE_DIR, "plugins"); + public static final String APP_NAME = "RuneLite"; public static Image ICON; + public static Icon NOTIFY_ICON; private static Injector injector; @@ -106,11 +115,16 @@ public class RuneLite private AccountSession accountSession; + private Notifier notifier; + + static { try { - ICON = ImageIO.read(ClientUI.class.getResourceAsStream("/runelite.png")); + final URL icon = ClientUI.class.getResource("/runelite.png"); + ICON = ImageIO.read(icon.openStream()); + NOTIFY_ICON = Icon.create(icon, APP_NAME); } catch (IOException ex) { @@ -162,6 +176,16 @@ public class RuneLite eventBus.register(menuManager); + // Setup the notifier + notifier = new SendNotification() + .setApplication(Application + .builder() + .icon(NOTIFY_ICON) + .name(APP_NAME) + .id(APP_NAME) + .build()) + .initNotifier(); + // Load the plugins, but does not start them yet. // This will initialize configuration pluginManager.loadCorePlugins(); @@ -184,11 +208,11 @@ public class RuneLite { if (!Strings.isNullOrEmpty(extra)) { - gui.setTitle("RuneLite " + properties.getVersion() + " " + extra); + gui.setTitle(APP_NAME + " " + properties.getVersion() + " " + extra); } else { - gui.setTitle("RuneLite " + properties.getVersion()); + gui.setTitle(APP_NAME + " " + properties.getVersion()); } } @@ -201,7 +225,7 @@ public class RuneLite SystemTray systemTray = SystemTray.getSystemTray(); - trayIcon = new TrayIcon(ICON, "RuneLite"); + trayIcon = new TrayIcon(ICON, APP_NAME); trayIcon.setImageAutoSize(true); try @@ -390,7 +414,18 @@ public class RuneLite public void notify(String message, TrayIcon.MessageType type) { - getTrayIcon().displayMessage("RuneLite", message, type); + final Notification.Level notificationLevel = Notification.Level + .valueOf((TrayIcon.MessageType.NONE.equals(type) + ? TrayIcon.MessageType.INFO + : type).toString()); + + notifier.send(Notification.builder() + .title(APP_NAME) + .message(message) + .icon(NOTIFY_ICON) + .level(notificationLevel) + .build()); + Toolkit.getDefaultToolkit().beep(); }