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
master
Daniele Gobbetti 2017-01-20 19:17:00 +01:00
parent 31ccaf361b
commit a7a37fd9c8
5 changed files with 63 additions and 14 deletions

View File

@ -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

View File

@ -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<String, Integer> 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();
}
}

View File

@ -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<String> 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<Pair<Integer, Object>> 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);
}

View File

@ -1874,6 +1874,8 @@ public class PebbleProtocol extends GBDeviceProtocol {
buf.order(ByteOrder.LITTLE_ENDIAN);
for (Pair<Integer, Object> pair : pairs) {
if (pair.first == null || pair.second == null)
continue;
buf.putInt(pair.first);
if (pair.second instanceof Integer) {
buf.put(TYPE_INT);

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<changelog>
<release version="next">
<change>Pebble: Fix temperature unit in Timestyle Pebble watchface</change>
</release>
<release version="0.17.1" versioncode="82">
<change>Pebble: Fix installation of some watchapps</change>
<change>Pebble: Try to improve PebbleKit compatibility</change>