use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage in project Gadgetbridge by Freeyourgadget.
the class PebbleIoThread method evaluateGBDeviceEventPebble.
// FIXME: parts are supporsed to be generic code
private boolean evaluateGBDeviceEventPebble(GBDeviceEvent deviceEvent) {
if (deviceEvent instanceof GBDeviceEventVersionInfo) {
if (prefs.getBoolean("datetime_synconconnect", true)) {
LOG.info("syncing time");
write(mPebbleProtocol.encodeSetTime());
}
write(mPebbleProtocol.encodeEnableAppLogs(prefs.getBoolean("pebble_enable_applogs", false)));
write(mPebbleProtocol.encodeReportDataLogSessions());
gbDevice.setState(GBDevice.State.INITIALIZED);
return false;
} else if (deviceEvent instanceof GBDeviceEventAppManagement) {
GBDeviceEventAppManagement appMgmt = (GBDeviceEventAppManagement) deviceEvent;
switch(appMgmt.type) {
case DELETE:
// right now on the Pebble we also receive this on a failed/successful installation ;/
switch(appMgmt.event) {
case FAILURE:
if (mIsInstalling) {
if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
// get the free slot
writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
} else {
finishInstall(true);
}
} else {
LOG.info("failure removing app");
}
break;
case SUCCESS:
if (mIsInstalling) {
if (mInstallState == PebbleAppInstallState.WAIT_SLOT) {
// get the free slot
writeInstallApp(mPebbleProtocol.encodeAppInfoReq());
} else {
finishInstall(false);
// refresh app list
write(mPebbleProtocol.encodeAppInfoReq());
}
} else {
LOG.info("successfully removed app");
write(mPebbleProtocol.encodeAppInfoReq());
}
break;
default:
break;
}
break;
case INSTALL:
switch(appMgmt.event) {
case FAILURE:
// TODO: report to Installer
LOG.info("failure installing app");
finishInstall(true);
break;
case SUCCESS:
setToken(appMgmt.token);
break;
case REQUEST:
LOG.info("APPFETCH request: " + appMgmt.uuid + " / " + appMgmt.token);
try {
installApp(Uri.fromFile(new File(FileUtils.getExternalFilesDir() + "/pbw-cache/" + appMgmt.uuid.toString() + ".pbw")), appMgmt.token);
} catch (IOException e) {
LOG.error("Error installing app: " + e.getMessage(), e);
}
break;
default:
break;
}
break;
case START:
LOG.info("got GBDeviceEventAppManagement START event for uuid: " + appMgmt.uuid);
break;
default:
break;
}
return true;
} else if (deviceEvent instanceof GBDeviceEventAppInfo) {
LOG.info("Got event for APP_INFO");
GBDeviceEventAppInfo appInfoEvent = (GBDeviceEventAppInfo) deviceEvent;
setInstallSlot(appInfoEvent.freeSlot);
return false;
} else if (deviceEvent instanceof GBDeviceEventAppMessage) {
if (mEnablePebblekit) {
LOG.info("Got AppMessage event");
if (mPebbleKitSupport != null) {
mPebbleKitSupport.sendAppMessageIntent((GBDeviceEventAppMessage) deviceEvent);
}
}
} else if (deviceEvent instanceof GBDeviceEventDataLogging) {
if (mEnablePebblekit) {
LOG.info("Got Datalogging event");
if (mPebbleKitSupport != null) {
mPebbleKitSupport.sendDataLoggingIntent((GBDeviceEventDataLogging) deviceEvent);
}
}
}
return false;
}
use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage in project Gadgetbridge by Freeyourgadget.
the class PebbleProtocol method decodeDictToJSONAppMessage.
private GBDeviceEvent[] decodeDictToJSONAppMessage(UUID uuid, ByteBuffer buf) throws JSONException {
buf.order(ByteOrder.LITTLE_ENDIAN);
byte dictSize = buf.get();
if (dictSize == 0) {
LOG.info("dict size is 0, ignoring");
return null;
}
JSONArray jsonArray = new JSONArray();
while (dictSize-- > 0) {
JSONObject jsonObject = new JSONObject();
Integer key = buf.getInt();
byte type = buf.get();
short length = buf.getShort();
jsonObject.put("key", key);
if (type == TYPE_CSTRING) {
length--;
}
jsonObject.put("length", length);
switch(type) {
case TYPE_UINT:
jsonObject.put("type", "uint");
if (length == 1) {
jsonObject.put("value", buf.get() & 0xff);
} else if (length == 2) {
jsonObject.put("value", buf.getShort() & 0xffff);
} else {
jsonObject.put("value", buf.getInt() & 0xffffffffL);
}
break;
case TYPE_INT:
jsonObject.put("type", "int");
if (length == 1) {
jsonObject.put("value", buf.get());
} else if (length == 2) {
jsonObject.put("value", buf.getShort());
} else {
jsonObject.put("value", buf.getInt());
}
break;
case TYPE_BYTEARRAY:
case TYPE_CSTRING:
byte[] bytes = new byte[length];
buf.get(bytes);
if (type == TYPE_BYTEARRAY) {
jsonObject.put("type", "bytes");
jsonObject.put("value", new String(Base64.encode(bytes, Base64.NO_WRAP)));
} else {
jsonObject.put("type", "string");
jsonObject.put("value", new String(bytes));
// skip null-termination;
buf.get();
}
break;
default:
LOG.info("unknown type in appmessage, ignoring");
return null;
}
jsonArray.put(jsonObject);
}
GBDeviceEventSendBytes sendBytesAck = null;
if (mAlwaysACKPebbleKit) {
// this is a hack we send an ack to the Pebble immediately because somebody said it helps some PebbleKit apps :P
sendBytesAck = new GBDeviceEventSendBytes();
sendBytesAck.encodedBytes = encodeApplicationMessageAck(uuid, last_id);
}
GBDeviceEventAppMessage appMessage = new GBDeviceEventAppMessage();
appMessage.appUUID = uuid;
appMessage.id = last_id & 0xff;
appMessage.message = jsonArray.toString();
return new GBDeviceEvent[] { appMessage, sendBytesAck };
}
Aggregations