-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
LibNotifyWrapper.java
73 lines (63 loc) · 2.56 KB
/
LibNotifyWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ui;
import com.intellij.ide.AppLifecycleListener;
import com.intellij.jna.JnaLoader;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.util.messages.MessageBusConnection;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
final class LibNotifyWrapper implements SystemNotificationsImpl.Notifier {
private static LibNotifyWrapper ourInstance;
static synchronized @Nullable LibNotifyWrapper getInstance() {
if (ourInstance == null && JnaLoader.isLoaded()) {
ourInstance = new LibNotifyWrapper();
}
return ourInstance;
}
@SuppressWarnings({"SpellCheckingInspection", "UnusedReturnValue"})
private interface LibNotify extends Library {
int notify_init(String appName);
void notify_uninit();
Pointer notify_notification_new(String summary, String body, String icon);
int notify_notification_show(Pointer notification, Pointer error);
}
private final LibNotify myLibNotify;
private final String myIcon;
private final Object myLock = new Object();
private boolean myDisposed = false;
private LibNotifyWrapper() {
//noinspection SpellCheckingInspection
myLibNotify = Native.load("libnotify.so.4", LibNotify.class);
String appName = ApplicationNamesInfo.getInstance().getProductName();
if (myLibNotify.notify_init(appName) == 0) {
throw new IllegalStateException("notify_init failed");
}
String icon = AppUIUtilKt.findAppIcon();
myIcon = icon != null ? icon : "dialog-information";
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect();
connection.subscribe(AppLifecycleListener.TOPIC, new AppLifecycleListener() {
@Override
public void appClosing() {
synchronized (myLock) {
myDisposed = true;
myLibNotify.notify_uninit();
}
}
});
}
@Override
public void notify(@NotNull String name, @NotNull String title, @NotNull String description) {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
synchronized (myLock) {
if (!myDisposed) {
Pointer notification = myLibNotify.notify_notification_new(title, description, myIcon);
myLibNotify.notify_notification_show(notification, null);
}
}
});
}
}