Merge pull request #3641 from deathbeam/use-terminal-notifier-if-available

Use terminal-notifier when available for OSX
This commit is contained in:
Tomas Slusny
2018-10-15 23:45:55 +02:00
committed by GitHub

View File

@@ -24,6 +24,7 @@
*/ */
package net.runelite.client; package net.runelite.client;
import com.google.common.base.Strings;
import com.google.common.escape.Escaper; import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers; import com.google.common.escape.Escapers;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -73,6 +74,7 @@ public class Notifier
private final ClientUI clientUI; private final ClientUI clientUI;
private final ScheduledExecutorService executorService; private final ScheduledExecutorService executorService;
private final Path notifyIconPath; private final Path notifyIconPath;
private final boolean terminalNotifierAvailable;
private Instant flashStart; private Instant flashStart;
@Inject @Inject
@@ -89,6 +91,12 @@ public class Notifier
this.runeLiteConfig = runeliteConfig; this.runeLiteConfig = runeliteConfig;
this.executorService = executorService; this.executorService = executorService;
this.notifyIconPath = RuneLite.RUNELITE_DIR.toPath().resolve("icon.png"); this.notifyIconPath = RuneLite.RUNELITE_DIR.toPath().resolve("icon.png");
// First check if we are running in launcher
this.terminalNotifierAvailable =
!Strings.isNullOrEmpty(RuneLiteProperties.getLauncherVersion())
&& isTerminalNotifierAvailable();
storeIcon(); storeIcon();
} }
@@ -164,7 +172,6 @@ public class Notifier
{ {
final String escapedTitle = SHELL_ESCAPE.escape(title); final String escapedTitle = SHELL_ESCAPE.escape(title);
final String escapedMessage = SHELL_ESCAPE.escape(message); final String escapedMessage = SHELL_ESCAPE.escape(message);
final String escapedSubtitle = null;
switch (OSType.getOSType()) switch (OSType.getOSType())
{ {
@@ -172,7 +179,7 @@ public class Notifier
sendLinuxNotification(escapedTitle, escapedMessage, type); sendLinuxNotification(escapedTitle, escapedMessage, type);
break; break;
case MacOS: case MacOS:
sendMacNotification(escapedTitle, escapedMessage, escapedSubtitle); sendMacNotification(escapedTitle, escapedMessage);
break; break;
default: default:
sendTrayNotification(title, message, type); sendTrayNotification(title, message, type);
@@ -219,40 +226,45 @@ public class Notifier
}); });
} }
private void sendMacNotification( private void sendMacNotification(final String title, final String message)
final String title,
final String message,
final String subtitle)
{ {
final List<String> commands = new ArrayList<>(); final List<String> commands = new ArrayList<>();
commands.add("osascript");
commands.add("-e");
final StringBuilder script = new StringBuilder("display notification "); if (terminalNotifierAvailable)
script.append(DOUBLE_QUOTE)
.append(message)
.append(DOUBLE_QUOTE);
script.append(" with title ")
.append(DOUBLE_QUOTE)
.append(title)
.append(DOUBLE_QUOTE);
if (subtitle != null)
{ {
script.append(" subtitle ") commands.add("terminal-notifier");
.append(DOUBLE_QUOTE) commands.add("-group");
.append(subtitle) commands.add("net.runelite.launcher");
.append(DOUBLE_QUOTE); commands.add("-sender");
commands.add("net.runelite.launcher");
commands.add("-message");
commands.add(DOUBLE_QUOTE + message + DOUBLE_QUOTE);
commands.add("-title");
commands.add(DOUBLE_QUOTE + title + DOUBLE_QUOTE);
}
else
{
commands.add("osascript");
commands.add("-e");
final String script = "display notification " + DOUBLE_QUOTE +
message +
DOUBLE_QUOTE +
" with title " +
DOUBLE_QUOTE +
title +
DOUBLE_QUOTE;
commands.add(script);
} }
commands.add(script.toString());
sendCommand(commands); sendCommand(commands);
} }
private Optional<Process> sendCommand(final List<String> commands) private Optional<Process> sendCommand(final List<String> commands)
{ {
log.debug("Sending command {}", commands);
try try
{ {
return Optional.of(new ProcessBuilder(commands.toArray(new String[commands.size()])) return Optional.of(new ProcessBuilder(commands.toArray(new String[commands.size()]))
@@ -282,6 +294,25 @@ public class Notifier
} }
} }
private boolean isTerminalNotifierAvailable()
{
if (OSType.getOSType() == OSType.MacOS)
{
try
{
final Process exec = Runtime.getRuntime().exec(new String[]{"terminal-notifier", "-help"});
exec.waitFor();
return exec.exitValue() == 0;
}
catch (IOException | InterruptedException e)
{
return false;
}
}
return false;
}
private static String toUrgency(TrayIcon.MessageType type) private static String toUrgency(TrayIcon.MessageType type)
{ {
switch (type) switch (type)