Search in sources :

Example 1 with GBDeviceEventAppMessage

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;
}
Also used : GBDeviceEventAppManagement(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement) GBDeviceEventAppMessage(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage) GBDeviceEventVersionInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo) GBDeviceEventAppInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo) IOException(java.io.IOException) GBDeviceEventDataLogging(nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging) File(java.io.File)

Example 2 with GBDeviceEventAppMessage

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 };
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GBDeviceEventAppMessage(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)

Aggregations

GBDeviceEventAppMessage (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage)2 File (java.io.File)1 IOException (java.io.IOException)1 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)1 GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)1 GBDeviceEventAppManagement (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement)1 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)1 GBDeviceEventVersionInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo)1 GBDeviceEventDataLogging (nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1