remote notifications
DESCRIPTION
Prezentace z jOpenSpace 2011. Jak pomocí jedné třídy vytvořit system tray notifikátor pro vzdálené použití (běží jako HTTP server). Více na: https://github.com/kwart/NotifierTRANSCRIPT
Josef CacekJosef CacekSiemens IT Solutions and ServicesSiemens IT Solutions and Services
BrnoBrno
Remote NotificationsRemote NotificationsIntegrace desktopu a webu v Javě – jednoduše a bez závislostí!Integrace desktopu a webu v Javě – jednoduše a bez závislostí!
Remote Notifications 2
System Tray Icon
Remote Notifications 3
System Tray Icon
import java.awt.*;
if (SystemTray.isSupported()) { Image img = Toolkit.getDefaultToolkit().getImage("coffee.png"); PopupMenu popup = // ... ;
TrayIcon trayIcon = new TrayIcon(img, "My own tooltip", popup); trayIcon.setImageAutoSize(true); //trayIcon.addMouseListener(...); //The MOUSE_ENTERED and MOUSE_EXITED mouse events are not supported.
SystemTray.getSystemTray().add(trayIcon); //...
trayIcon.displayMessage("Title", "I like it.", TrayIcon.MessageType.INFO);}
Remote Notifications 4
Windows Sounds
Remote Notifications 5
Windows sounds
import java.awt.Toolkit;
Runnable sound = (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.asterisk");
if (sound != null) { sound.run();}
// "win.sound.*" properties:// default asterisk exclamation hand question// maximize minimize// menuCommand menuPopup// open close start exit// restoreDown restoreUp
Remote Notifications 6
HTTP server
Remote Notifications 7
HTTP server
import com.sun.net.httpserver.*;
HttpServer httpServer = HttpServer.create(new InetSocketAddress(80), 0);HttpHandler handler = new HttpHandler() { public void handle(HttpExchange exchange) throws IOException {
InputStream is = exchange.getRequestBody();
//... some logic here
exchange.getResponseHeaders().add("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0); // status - SC_OK, contentLength - chunked
OutputStreamWriter osw = new OutputStreamWriter(exchange.getResponseBody());
osw.append("Hello world wide web!\nQuery: " + exchange.getRequestURI().getQuery());
is.close();
osw.close();
}
};httpServer.createContext("/", handler);httpServer.start();
Remote Notifications 8
HTTP server
import com.sun.net.httpserver.*;
HttpServer httpServer = HttpServer.create(new InetSocketAddress(80), 0);HttpHandler handler = new HttpHandler() {
public void handle(HttpExchange exchange) throws IOException { InputStream is = exchange.getRequestBody(); //... some logic here exchange.getResponseHeaders().add("Content-Type", "text/plain"); exchange.sendResponseHeaders(200, 0); // status - SC_OK, contentLength - chunked OutputStreamWriter osw = new OutputStreamWriter(exchange.getResponseBody()); osw.append("Hello world wide web!\nQuery: " + exchange.getRequestURI().getQuery());
is.close(); osw.close(); }};httpServer.createContext("/", handler);httpServer.start();
Remote Notifications 9
Notifierhttps://github.com/kwart/Notifier
Remote Notifications 10
Notifier – server
spustitelný JAR– $java -jar Notifier.jar [port [defaultIcon [soundDesktopProperty]]$java -jar Notifier.jar [port [defaultIcon [soundDesktopProperty]]
výchozí nastavení– port: 8811port: 8811– ikona: sunikona: sun– zvuk: win.sound.asteriskzvuk: win.sound.asterisk
události– jednoduchý klik – návrat k výchozí ikonějednoduchý klik – návrat k výchozí ikoně– dvojklik – ukončení programudvojklik – ukončení programu
Remote Notifications 11
Notifier – klienti
jakýkoliv běžný HTTP klient– firefox http://localhost:8811/teddy-bearfirefox http://localhost:8811/teddy-bear
příkazová řádka– wget -O - http://localhost:8811/warning wget -O - http://localhost:8811/warning – curl -d "Build finished succesfully" http://localhost:8811/okcurl -d "Build finished succesfully" http://localhost:8811/ok– curl -d "%sname: %smessage" --connect-timeout 3 http://10.0.2.2:8811/bubblecurl -d "%sname: %smessage" --connect-timeout 3 http://10.0.2.2:8811/bubble
Remote Notifications 12
Zdrojové kódy ke stažení– https://github.com/kwart/Notifierhttps://github.com/kwart/Notifier
API dokumentace– System TraySystem Tray
http://download.oracle.com/javase/6/docs/api/java/awt/SystemTray.htmlhttp://download.oracle.com/javase/6/docs/api/java/awt/SystemTray.html– Windows SoundsWindows Sounds
http://download.oracle.com/javase/1.4.2/docs/guide/swing/1.4/w2k_props.htmlhttp://download.oracle.com/javase/1.4.2/docs/guide/swing/1.4/w2k_props.html– HTTP serverHTTP server
http://download.oracle.com/javase/6/docs/jre/api/net/httpserver/spec/http://download.oracle.com/javase/6/docs/jre/api/net/httpserver/spec/
šablona prezentace od Chih-Hao Tsai http://technology.chtsai.org/impress/