Pebble: Better support for 8 and 16 bit integers in AppMessages

This commit is contained in:
Andreas Shimokawa 2015-09-18 00:03:34 +02:00
parent b02c286818
commit e6a8a1a36c
1 changed files with 39 additions and 9 deletions

View File

@ -1218,6 +1218,12 @@ public class PebbleProtocol extends GBDeviceProtocol {
length += 7; // key + type + length length += 7; // key + type + length
if (pair.second instanceof Integer) { if (pair.second instanceof Integer) {
length += 4; length += 4;
}
if (pair.second instanceof Short) {
length += 2;
}
if (pair.second instanceof Byte) {
length += 1;
} else if (pair.second instanceof String) { } else if (pair.second instanceof String) {
length += ((String) pair.second).length() + 1; length += ((String) pair.second).length() + 1;
} else if (pair.second instanceof byte[]) { } else if (pair.second instanceof byte[]) {
@ -1235,13 +1241,21 @@ public class PebbleProtocol extends GBDeviceProtocol {
buf.putLong(uuid.getLeastSignificantBits()); buf.putLong(uuid.getLeastSignificantBits());
buf.put((byte) pairs.size()); buf.put((byte) pairs.size());
buf.order(ByteOrder.LITTLE_ENDIAN); // Um, yes, really buf.order(ByteOrder.LITTLE_ENDIAN);
for (Pair<Integer, Object> pair : pairs) { for (Pair<Integer, Object> pair : pairs) {
buf.putInt(pair.first); buf.putInt(pair.first);
if (pair.second instanceof Integer) { if (pair.second instanceof Integer) {
buf.put(TYPE_INT); buf.put(TYPE_INT);
buf.putShort((short) 4); // length of int buf.putShort((short) 4); // length
buf.putInt((int) pair.second); buf.putInt((int) pair.second);
} else if (pair.second instanceof Short) {
buf.put(TYPE_INT);
buf.putShort((short) 2); // length
buf.putShort((short) pair.second);
} else if (pair.second instanceof Byte) {
buf.put(TYPE_INT);
buf.putShort((short) 1); // length
buf.put((byte) pair.second);
} else if (pair.second instanceof String) { } else if (pair.second instanceof String) {
String str = (String) pair.second; String str = (String) pair.second;
buf.put(TYPE_CSTRING); buf.put(TYPE_CSTRING);
@ -1266,13 +1280,29 @@ public class PebbleProtocol extends GBDeviceProtocol {
JSONObject jsonObject = (JSONObject) jsonArray.get(i); JSONObject jsonObject = (JSONObject) jsonArray.get(i);
String type = (String) jsonObject.get("type"); String type = (String) jsonObject.get("type");
int key = (int) jsonObject.get("key"); int key = (int) jsonObject.get("key");
if (type.equals("uint") || type.equals("int")) { int length = (int) jsonObject.get("length");
pairs.add(new Pair<>(key, (Object) jsonObject.getInt("value"))); switch (type) {
} else if (type.equals("string")) { case "uint":
pairs.add(new Pair<>(key, (Object) jsonObject.getString("value"))); case "int":
} else if (type.equals("bytes")) { if (length == 1) {
byte[] bytes = Base64.decode(jsonObject.getString("value"), Base64.NO_WRAP); pairs.add(new Pair<>(key, (Object) (byte) jsonObject.getInt("value")));
pairs.add(new Pair<>(key, (Object) bytes)); } else if (length == 2) {
pairs.add(new Pair<>(key, (Object) (short) jsonObject.getInt("value")));
} else {
if (type.equals("uint")) {
pairs.add(new Pair<>(key, (Object) (int) (jsonObject.getInt("value") & 0xffffffffL)));
} else {
pairs.add(new Pair<>(key, (Object) jsonObject.getInt("value")));
}
}
break;
case "string":
pairs.add(new Pair<>(key, (Object) jsonObject.getString("value")));
break;
case "bytes":
byte[] bytes = Base64.decode(jsonObject.getString("value"), Base64.NO_WRAP);
pairs.add(new Pair<>(key, (Object) bytes));
break;
} }
} catch (JSONException e) { } catch (JSONException e) {
return null; return null;