From 7a7d015f8a68f5e0d06fe6e3d9422d5f418f653d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 30 Nov 2014 23:55:54 +0100 Subject: o Initial import of Fiken Status Display app. --- .../java/no/topi/fiken/display/DisplayService.java | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 app/src/main/java/no/topi/fiken/display/DisplayService.java (limited to 'app/src/main/java/no/topi/fiken/display/DisplayService.java') diff --git a/app/src/main/java/no/topi/fiken/display/DisplayService.java b/app/src/main/java/no/topi/fiken/display/DisplayService.java new file mode 100644 index 0000000..c907138 --- /dev/null +++ b/app/src/main/java/no/topi/fiken/display/DisplayService.java @@ -0,0 +1,102 @@ +package no.topi.fiken.display; + +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Binder; + +public interface DisplayService { + + enum IntentExtra { + DEVICE_NAME, + DEVICE_ADDRESS, + DEVICE_IS_DISPLAY, + RSSI, + SCANNING, + CONNECTED, + } + + public static enum IntentAction { + DEVICE_UPDATE; + + private final String key = getClass().getName() + "." + name(); + + public Intent intent() { + return new Intent(key); + } + + public static final IntentFilter ALL_FILTER = new IntentFilter() {{ + for (DefaultDisplayService.IntentAction intentAction : DefaultDisplayService.IntentAction.values()) { + addAction(intentAction.key); + } + }}; + + public static IntentAction valueOf(Intent intent) { + try { + return valueOf(intent.getAction().replaceAll(".*\\.", "")); + } catch (IllegalArgumentException e) { + return null; + } + } + } + + public class LocalBinder extends Binder { + private final DisplayService service; + + public LocalBinder(DisplayService service) { + this.service = service; + } + + DisplayService getService() { + return service; + } + } + + boolean initialize(); + + void startScan(); + + void stopScan(); + + boolean connect(String address); + + public void disconnect(); + + class DeviceInfo { + final String address; + int rssi = 0; + Boolean isDisplay = null; + String name; + + DeviceInfo(String address, int rssi) { + this.address = address; + this.rssi = rssi; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DeviceInfo that = (DeviceInfo) o; + + return address.equals(that.address); + } + + @Override + public int hashCode() { + return address.hashCode(); + } + + public void update(Intent intent) { + if (intent.hasExtra(IntentExtra.DEVICE_IS_DISPLAY.name())) { + isDisplay = intent.getBooleanExtra(IntentExtra.DEVICE_IS_DISPLAY.name(), false); + } + if (intent.hasExtra(IntentExtra.RSSI.name())) { + rssi = intent.getIntExtra(IntentExtra.RSSI.name(), 0); + } + if (intent.hasExtra(IntentExtra.DEVICE_NAME.name())) { + name = intent.getStringExtra(IntentExtra.DEVICE_NAME.name()); + } + } + } +} -- cgit v1.2.3