From a7a37fd9c8841d0e41879222bd4c13b4020d20ab Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Fri, 20 Jan 2017 19:17:00 +0100 Subject: [PATCH] Pebble: add a method to use the JSON keys instead of hardcoding the key ID. This needs parsing the json but it is only done once. So far only Timestyle apphandler uses the new approach and this fi_xes the issue reported here https://github.com/Freeyourgadget/Gadgetbridge/issues/482#issuecomment-273757492 Fixes also a potential crash when the message for pebble contained a null key in one of the Pairs --- CHANGELOG.md | 3 ++ .../devices/pebble/AppMessageHandler.java | 19 +++++++ .../AppMessageHandlerTimeStylePebble.java | 50 +++++++++++++------ .../devices/pebble/PebbleProtocol.java | 2 + app/src/main/res/xml/changelog_master.xml | 3 ++ 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86b321c0..c69264c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ###Changelog +####Version next +* Pebble: Fix temperature unit in Timestyle Pebble watchface + ####Version 0.17.1 * Pebble: Fix installation of some watchapps * Pebble: Try to improve PebbleKit compatibility diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java index 0e0ae701..8705cb23 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandler.java @@ -3,17 +3,25 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.pebble; import android.util.Pair; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.File; +import java.io.IOException; import java.util.ArrayList; +import java.util.Map; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; +import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; class AppMessageHandler { final PebbleProtocol mPebbleProtocol; final UUID mUUID; + protected Map messageKeys; AppMessageHandler(UUID uuid, PebbleProtocol pebbleProtocol) { mUUID = uuid; @@ -46,4 +54,15 @@ class AppMessageHandler { protected GBDevice getDevice() { return mPebbleProtocol.getDevice(); } + + protected JSONObject getAppKeys() throws IOException, JSONException { + File destDir = new File(FileUtils.getExternalFilesDir() + "/pbw-cache"); + File configurationFile = new File(destDir, mUUID.toString() + ".json"); + if (configurationFile.exists()) { + String jsonstring = FileUtils.getStringFromFile(configurationFile); + JSONObject json = new JSONObject(jsonstring); + return json.getJSONObject("appKeys"); + } + throw new IOException(); + } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java index 25d995ab..6ca62975 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerTimeStylePebble.java @@ -1,23 +1,24 @@ 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.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; import java.util.UUID; 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 AppMessageHandlerTimeStylePebble extends AppMessageHandler { - private static final int MESSAGE_KEY_WeatherCondition = 10000; - private static final int MESSAGE_KEY_WeatherForecastCondition = 10002; - private static final int MESSAGE_KEY_WeatherForecastHighTemp = 10003; - private static final int MESSAGE_KEY_WeatherForecastLowTemp = 10004; - private static final int MESSAGE_KEY_WeatherTemperature = 10001; - private static final int MESSAGE_KEY_WeatherUseNightIcon = 10025; - private static final int ICON_CLEAR_DAY = 0; private static final int ICON_CLEAR_NIGHT = 1; @@ -34,6 +35,27 @@ class AppMessageHandlerTimeStylePebble extends AppMessageHandler { AppMessageHandlerTimeStylePebble(UUID uuid, PebbleProtocol pebbleProtocol) { super(uuid, pebbleProtocol); + messageKeys = new HashMap<>(); + try { + JSONObject appKeys = getAppKeys(); + Iterator appKeysIterator = appKeys.keys(); + while (appKeysIterator.hasNext()) { + String current = appKeysIterator.next(); + switch (current) { + case "WeatherCondition": + case "WeatherForecastCondition": + case "WeatherForecastHighTemp": + case "WeatherForecastLowTemp": + case "WeatherTemperature": + case "SettingUseMetric": + case "WeatherUseNightIcon": + messageKeys.put(current, appKeys.getInt(current)); + break; + } + } + } catch (IOException | JSONException e) { + GB.toast("There was an error accessing the watchface configuration.", Toast.LENGTH_LONG, GB.ERROR); + } } /* @@ -98,13 +120,13 @@ class AppMessageHandlerTimeStylePebble extends AppMessageHandler { ArrayList> pairs = new ArrayList<>(); boolean isNight = false; //TODO: use the night icons when night - pairs.add(new Pair<>(MESSAGE_KEY_WeatherUseNightIcon, (Object) (isNight ? 1 : 0))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherTemperature, (Object) (weatherSpec.currentTemp - 273))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherCondition, (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight)))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastCondition, (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight)))); - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastHighTemp, (Object) (weatherSpec.todayMaxTemp - 273))); - - pairs.add(new Pair<>(MESSAGE_KEY_WeatherForecastLowTemp, (Object) (weatherSpec.todayMinTemp - 273))); + pairs.add(new Pair<>(messageKeys.get("SettingUseMetric"), (Object) 1)); //celsius + pairs.add(new Pair<>(messageKeys.get("WeatherUseNightIcon"), (Object) (isNight ? 1 : 0))); + pairs.add(new Pair<>(messageKeys.get("WeatherTemperature"), (Object) (weatherSpec.currentTemp - 273))); + pairs.add(new Pair<>(messageKeys.get("WeatherCondition"), (Object) (getIconForConditionCode(weatherSpec.currentConditionCode, isNight)))); + pairs.add(new Pair<>(messageKeys.get("WeatherForecastCondition"), (Object) (getIconForConditionCode(weatherSpec.tomorrowConditionCode, isNight)))); + pairs.add(new Pair<>(messageKeys.get("WeatherForecastHighTemp"), (Object) (weatherSpec.todayMaxTemp - 273))); + pairs.add(new Pair<>(messageKeys.get("WeatherForecastLowTemp"), (Object) (weatherSpec.todayMinTemp - 273))); return mPebbleProtocol.encodeApplicationMessagePush(PebbleProtocol.ENDPOINT_APPLICATIONMESSAGE, mUUID, pairs); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index b271a0bf..6ef013bb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -1874,6 +1874,8 @@ public class PebbleProtocol extends GBDeviceProtocol { buf.order(ByteOrder.LITTLE_ENDIAN); for (Pair pair : pairs) { + if (pair.first == null || pair.second == null) + continue; buf.putInt(pair.first); if (pair.second instanceof Integer) { buf.put(TYPE_INT); diff --git a/app/src/main/res/xml/changelog_master.xml b/app/src/main/res/xml/changelog_master.xml index fe8e9179..99edf2ac 100644 --- a/app/src/main/res/xml/changelog_master.xml +++ b/app/src/main/res/xml/changelog_master.xml @@ -1,5 +1,8 @@ + + Pebble: Fix temperature unit in Timestyle Pebble watchface + Pebble: Fix installation of some watchapps Pebble: Try to improve PebbleKit compatibility