From a451b5367b06b2f292487a907d228933d3d46dfe Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 24 Jan 2017 19:02:45 +0100 Subject: [PATCH] Pebble: dynamic key support for Square handler --- .../pebble/AppMessageHandlerHealthify.java | 2 - .../pebble/AppMessageHandlerMorpheuz.java | 2 - .../pebble/AppMessageHandlerSquare.java | 47 +++++++++++-------- .../pebble/AppMessageHandlerTrekVolle.java | 2 - 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerHealthify.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerHealthify.java index 1ae46cdbc..e45c4a16f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerHealthify.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerHealthify.java @@ -9,7 +9,6 @@ import org.json.JSONObject; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.HashMap; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; @@ -25,7 +24,6 @@ class AppMessageHandlerHealthify extends AppMessageHandler { AppMessageHandlerHealthify(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); - messageKeys = new HashMap<>(); try { JSONObject appKeys = getAppKeys(); KEY_TEMPERATURE = appKeys.getInt("TEMPERATURE"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java index 37122ec65..0e05d0bf6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMorpheuz.java @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.Objects; import java.util.SimpleTimeZone; import java.util.TimeZone; @@ -59,7 +58,6 @@ class AppMessageHandlerMorpheuz extends AppMessageHandler { public AppMessageHandlerMorpheuz(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); - messageKeys = new HashMap<>(); try { JSONObject appKeys = getAppKeys(); keyPoint = appKeys.getInt("keyPoint"); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSquare.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSquare.java index bcb776795..8a0800375 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSquare.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerSquare.java @@ -1,7 +1,12 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; import android.util.Pair; +import android.widget.Toast; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.UUID; @@ -10,26 +15,28 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; +import nodomain.freeyourgadget.gadgetbridge.util.GB; class AppMessageHandlerSquare extends AppMessageHandler { - // "CfgKeyCelsiusTemperature":10001, - // CfgKeyConditions":10002, - //"CfgKeyWeatherError":10003, - // "CfgKeyWeatherMode":10004, - // "CfgKeyUseCelsius":10005," - // CfgKeyWeatherLocation":10006," - // "CfgKeyTemperature":10000, - // - // - private static final int KEY_TEMP = 10001; //celsius - private static final int KEY_WEATHER = 10002; - private static final int KEY_WEATHER_MODE = 10004; - private static final int KEY_USE_CELSIUS = 10005; //celsius - private static final int KEY_LOCATION = 10006; - private static final int KEY_TEMP_F = 10000; //fahrenheit + private int CfgKeyCelsiusTemperature; + private int CfgKeyConditions; + private int CfgKeyWeatherMode; + private int CfgKeyUseCelsius; + private int CfgKeyWeatherLocation; AppMessageHandlerSquare(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); + + try { + JSONObject appKeys = getAppKeys(); + CfgKeyCelsiusTemperature = appKeys.getInt("CfgKeyCelsiusTemperature"); + CfgKeyConditions = appKeys.getInt("CfgKeyConditions"); + CfgKeyWeatherMode = appKeys.getInt("CfgKeyWeatherMode"); + CfgKeyUseCelsius = appKeys.getInt("CfgKeyUseCelsius"); + CfgKeyWeatherLocation = appKeys.getInt("CfgKeyWeatherLocation"); + } catch (IOException | JSONException e) { + GB.toast("There was an error accessing the watchface configuration.", Toast.LENGTH_LONG, GB.ERROR); + } } private byte[] encodeSquareWeatherMessage(WeatherSpec weatherSpec) { @@ -38,11 +45,11 @@ class AppMessageHandlerSquare extends AppMessageHandler { } ArrayList> pairs = new ArrayList<>(2); - pairs.add(new Pair<>(KEY_WEATHER_MODE, (Object) 1)); - pairs.add(new Pair<>(KEY_WEATHER, (Object) weatherSpec.currentCondition)); - pairs.add(new Pair<>(KEY_USE_CELSIUS, (Object) 1)); - pairs.add(new Pair<>(KEY_TEMP, (Object) (weatherSpec.currentTemp - 273))); - pairs.add(new Pair<>(KEY_LOCATION, (Object) (weatherSpec.location))); + pairs.add(new Pair<>(CfgKeyWeatherMode, (Object) 1)); + pairs.add(new Pair<>(CfgKeyConditions, (Object) weatherSpec.currentCondition)); + pairs.add(new Pair<>(CfgKeyUseCelsius, (Object) 1)); + pairs.add(new Pair<>(CfgKeyCelsiusTemperature, (Object) (weatherSpec.currentTemp - 273))); + pairs.add(new Pair<>(CfgKeyWeatherLocation, (Object) (weatherSpec.location))); byte[] weatherMessage = mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); ByteBuffer buf = ByteBuffer.allocate(weatherMessage.length); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTrekVolle.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTrekVolle.java index 6d28fa8e3..e1f304634 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTrekVolle.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTrekVolle.java @@ -8,7 +8,6 @@ import org.json.JSONObject; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; @@ -28,7 +27,6 @@ class AppMessageHandlerTrekVolle extends AppMessageHandler { AppMessageHandlerTrekVolle(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); - messageKeys = new HashMap<>(); try { JSONObject appKeys = getAppKeys(); MESSAGE_KEY_WEATHER_TEMPERATURE = appKeys.getInt("WEATHER_TEMPERATURE");