From 0b05a92abea9d3dfb9c4028e58b22cd2876a5924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de> Date: Thu, 22 Sep 2022 22:35:36 +0200 Subject: [PATCH] Experiment with notifications --- .../windows/SystemTrayNotifications.kt | 68 +++++++++++++++++++ .../windows/TestJavaNotifications.kt | 35 ++++++++++ 2 files changed, 103 insertions(+) create mode 100644 briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/SystemTrayNotifications.kt create mode 100644 briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/notification/windows/TestJavaNotifications.kt diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/SystemTrayNotifications.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/SystemTrayNotifications.kt new file mode 100644 index 0000000000..c8b7cc7c2d --- /dev/null +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/notification/windows/SystemTrayNotifications.kt @@ -0,0 +1,68 @@ +/* + * Briar Desktop + * Copyright (C) 2021-2022 The Briar Project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package org.briarproject.briar.desktop.notification.windows + +import java.awt.AWTException +import java.awt.Image +import java.awt.SystemTray +import java.awt.SystemTray.getSystemTray +import java.awt.TrayIcon +import java.awt.event.ActionEvent +import java.io.IOException +import javax.imageio.ImageIO + +class SystemTrayNotifications private constructor(image: Image?) { + + companion object { + private var ourInstance: SystemTrayNotifications? = null + + @get:Throws(AWTException::class) + @get:Synchronized + val instance: SystemTrayNotifications? + get() { + if (ourInstance == null && SystemTray.isSupported()) { + ourInstance = SystemTrayNotifications(createImage()) + } + return ourInstance + } + + private fun createImage(): Image? { + try { + Thread.currentThread().contextClassLoader.getResourceAsStream("images/logo_circle.png") + .use { stream -> return ImageIO.read(stream) } + } catch (e: IOException) { + return null + } + } + } + + private val trayIcon: TrayIcon + + init { + val tooltip = "Briar" // NON-NLS + trayIcon = TrayIcon(image, tooltip) + trayIcon.isImageAutoSize = true + getSystemTray().add(trayIcon) + trayIcon.addActionListener { _: ActionEvent -> } + } + + fun notify(title: String?, description: String?) { + trayIcon.displayMessage(title, description, TrayIcon.MessageType.INFO) + } + +} \ No newline at end of file diff --git a/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/notification/windows/TestJavaNotifications.kt b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/notification/windows/TestJavaNotifications.kt new file mode 100644 index 0000000000..6c96bbf0bc --- /dev/null +++ b/briar-desktop/src/test/kotlin/org/briarproject/briar/desktop/notification/windows/TestJavaNotifications.kt @@ -0,0 +1,35 @@ +/* + * Briar Desktop + * Copyright (C) 2021-2022 The Briar Project + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +package org.briarproject.briar.desktop.notification.windows + +import kotlin.system.exitProcess + +/** + * Small program to test notification support for Windows. + */ +fun main() { + SystemTrayNotifications.instance?.apply { + notify("This is the title", "This is the description") + Thread.sleep(3000) + + notify("This is the title", "This is the description") + Thread.sleep(3000) + + exitProcess(0) + } +} -- GitLab