remote notifications

12
Josef Cacek Josef Cacek Siemens IT Solutions and Services Siemens IT Solutions and Services Brno Brno Remote Notifications Remote Notifications Integrace desktopu a webu v Javě – jednoduše a bez závislostí! Integrace desktopu a webu v Javě – jednoduše a bez závislostí!

Upload: josef-cacek

Post on 01-Jul-2015

1.280 views

Category:

Technology


1 download

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/Notifier

TRANSCRIPT

Page 1: Remote Notifications

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í!

Page 2: Remote Notifications

Remote Notifications 2

System Tray Icon

Page 3: Remote Notifications

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);}

Page 4: Remote Notifications

Remote Notifications 4

Windows Sounds

Page 5: Remote Notifications

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

Page 6: Remote Notifications

Remote Notifications 6

HTTP server

Page 7: Remote Notifications

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();

Page 8: Remote Notifications

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();

Page 9: Remote Notifications

Remote Notifications 9

Notifierhttps://github.com/kwart/Notifier

Page 10: Remote Notifications

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

Page 11: Remote Notifications

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

Page 12: Remote Notifications

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/