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 0000000000000000000000000000000000000000..c8b7cc7c2d353bc6f72c9e09e5f1bebbf000b868 --- /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 0000000000000000000000000000000000000000..6c96bbf0bcf3894a6f7bff4746cb094d01eadb71 --- /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) + } +}