Search in sources :

Example 1 with GBDeviceEventAppInfo

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo in project Gadgetbridge by Freeyourgadget.

the class PebbleIoThread method evaluateGBDeviceEventPebble.

// FIXME: parts are supposed 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(PebbleUtils.getPbwCacheDir(), 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);
                if (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
                    if (mPebbleProtocol.hasAppMessageHandler(appMgmt.uuid)) {
                        WebViewSingleton.getInstance().stopJavascriptInterface();
                    } else {
                        WebViewSingleton.getInstance().runJavascriptInterface(gbDevice, appMgmt.uuid);
                    }
                }
                mPebbleActiveAppTracker.markAppOpened(appMgmt.uuid);
                break;
            case STOP:
                mPebbleActiveAppTracker.markAppClosed(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 (GBApplication.getGBPrefs().isBackgroundJsEnabled()) {
            sendAppMessageJS((GBDeviceEventAppMessage) deviceEvent);
        }
        if (mEnablePebblekit) {
            LOG.info("Got AppMessage event");
            if (mPebbleKitSupport != null && ((GBDeviceEventAppMessage) deviceEvent).type == GBDeviceEventAppMessage.TYPE_APPMESSAGE) {
                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 GBDeviceEventAppInfo

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo in project Gadgetbridge by Freeyourgadget.

the class FossilHRWatchAdapter method setInstalledApplications.

public void setInstalledApplications(List<ApplicationInformation> installedApplications) {
    this.installedApplications = installedApplications;
    GBDeviceEventAppInfo appInfoEvent = new GBDeviceEventAppInfo();
    appInfoEvent.apps = new GBDeviceApp[installedApplications.size()];
    for (int i = 0; i < installedApplications.size(); i++) {
        String appName = installedApplications.get(i).getAppName();
        String appVersion = installedApplications.get(i).getAppVersion();
        UUID appUUID = UUID.nameUUIDFromBytes(appName.getBytes(StandardCharsets.UTF_8));
        GBDeviceApp.Type appType;
        if (installedApplications.get(i).getAppName().endsWith("App")) {
            appType = GBDeviceApp.Type.APP_GENERIC;
        } else {
            appType = GBDeviceApp.Type.WATCHFACE;
        }
        appInfoEvent.apps[i] = new GBDeviceApp(appUUID, appName, "(unknown)", appVersion, appType);
    }
    getDeviceSupport().evaluateGBDeviceEvent(appInfoEvent);
}
Also used : GBDeviceEventAppInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo) UUID(java.util.UUID) Paint(android.graphics.Paint) GBDeviceApp(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp)

Example 3 with GBDeviceEventAppInfo

use of nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo in project Gadgetbridge by Freeyourgadget.

the class PebbleProtocol method decodeResponse.

@Override
public GBDeviceEvent[] decodeResponse(byte[] responseData) {
    ByteBuffer buf = ByteBuffer.wrap(responseData);
    buf.order(ByteOrder.BIG_ENDIAN);
    short length = buf.getShort();
    short endpoint = buf.getShort();
    GBDeviceEvent[] devEvts = null;
    byte pebbleCmd;
    switch(endpoint) {
        case ENDPOINT_MUSICCONTROL:
            pebbleCmd = buf.get();
            GBDeviceEventMusicControl musicCmd = new GBDeviceEventMusicControl();
            switch(pebbleCmd) {
                case MUSICCONTROL_NEXT:
                    musicCmd.event = GBDeviceEventMusicControl.Event.NEXT;
                    break;
                case MUSICCONTROL_PREVIOUS:
                    musicCmd.event = GBDeviceEventMusicControl.Event.PREVIOUS;
                    break;
                case MUSICCONTROL_PLAY:
                    musicCmd.event = GBDeviceEventMusicControl.Event.PLAY;
                    break;
                case MUSICCONTROL_PAUSE:
                    musicCmd.event = GBDeviceEventMusicControl.Event.PAUSE;
                    break;
                case MUSICCONTROL_PLAYPAUSE:
                    musicCmd.event = GBDeviceEventMusicControl.Event.PLAYPAUSE;
                    break;
                case MUSICCONTROL_VOLUMEUP:
                    musicCmd.event = GBDeviceEventMusicControl.Event.VOLUMEUP;
                    break;
                case MUSICCONTROL_VOLUMEDOWN:
                    musicCmd.event = GBDeviceEventMusicControl.Event.VOLUMEDOWN;
                    break;
                default:
                    break;
            }
            devEvts = new GBDeviceEvent[] { musicCmd };
            break;
        case ENDPOINT_PHONECONTROL:
            pebbleCmd = buf.get();
            GBDeviceEventCallControl callCmd = new GBDeviceEventCallControl();
            switch(pebbleCmd) {
                case PHONECONTROL_HANGUP:
                    callCmd.event = GBDeviceEventCallControl.Event.END;
                    break;
                default:
                    LOG.info("Unknown PHONECONTROL event" + pebbleCmd);
                    break;
            }
            devEvts = new GBDeviceEvent[] { callCmd };
            break;
        case ENDPOINT_FIRMWAREVERSION:
            pebbleCmd = buf.get();
            GBDeviceEventVersionInfo versionCmd = new GBDeviceEventVersionInfo();
            // skip
            buf.getInt();
            versionCmd.fwVersion = getFixedString(buf, 32);
            mFwMajor = versionCmd.fwVersion.charAt(1) - 48;
            LOG.info("Pebble firmware major detected as " + mFwMajor);
            byte[] tmp = new byte[9];
            buf.get(tmp, 0, 9);
            int hwRev = buf.get() + 8;
            if (hwRev >= 0 && hwRev < hwRevisions.length) {
                versionCmd.hwVersion = hwRevisions[hwRev];
            }
            devEvts = new GBDeviceEvent[] { versionCmd };
            break;
        case ENDPOINT_APPMANAGER:
            pebbleCmd = buf.get();
            switch(pebbleCmd) {
                case APPMANAGER_GETAPPBANKSTATUS:
                    GBDeviceEventAppInfo appInfoCmd = new GBDeviceEventAppInfo();
                    int slotCount = buf.getInt();
                    int slotsUsed = buf.getInt();
                    appInfoCmd.apps = new GBDeviceApp[slotsUsed];
                    boolean[] slotInUse = new boolean[slotCount];
                    for (int i = 0; i < slotsUsed; i++) {
                        int id = buf.getInt();
                        int index = buf.getInt();
                        slotInUse[index] = true;
                        String appName = getFixedString(buf, 32);
                        String appCreator = getFixedString(buf, 32);
                        int flags = buf.getInt();
                        GBDeviceApp.Type appType;
                        if ((flags & 16) == 16) {
                            // FIXME: verify this assumption
                            appType = GBDeviceApp.Type.APP_ACTIVITYTRACKER;
                        } else if ((flags & 1) == 1) {
                            // FIXME: verify this assumption
                            appType = GBDeviceApp.Type.WATCHFACE;
                        } else {
                            appType = GBDeviceApp.Type.APP_GENERIC;
                        }
                        short appVersion = buf.getShort();
                        appInfoCmd.apps[i] = new GBDeviceApp(tmpUUIDS.get(i), appName, appCreator, String.valueOf(appVersion), appType);
                    }
                    for (int i = 0; i < slotCount; i++) {
                        if (!slotInUse[i]) {
                            appInfoCmd.freeSlot = (byte) i;
                            LOG.info("found free slot " + i);
                            break;
                        }
                    }
                    devEvts = new GBDeviceEvent[] { appInfoCmd };
                    break;
                case APPMANAGER_GETUUIDS:
                    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
                    sendBytes.encodedBytes = encodeSimpleMessage(ENDPOINT_APPMANAGER, APPMANAGER_GETAPPBANKSTATUS);
                    devEvts = new GBDeviceEvent[] { sendBytes };
                    tmpUUIDS.clear();
                    slotsUsed = buf.getInt();
                    for (int i = 0; i < slotsUsed; i++) {
                        UUID uuid = getUUID(buf);
                        LOG.info("found uuid: " + uuid);
                        tmpUUIDS.add(uuid);
                    }
                    break;
                case APPMANAGER_REMOVEAPP:
                    GBDeviceEventAppManagement deleteRes = new GBDeviceEventAppManagement();
                    deleteRes.type = GBDeviceEventAppManagement.EventType.DELETE;
                    int result = buf.getInt();
                    switch(result) {
                        case APPMANAGER_RES_SUCCESS:
                            deleteRes.event = GBDeviceEventAppManagement.Event.SUCCESS;
                            break;
                        default:
                            deleteRes.event = GBDeviceEventAppManagement.Event.FAILURE;
                            break;
                    }
                    devEvts = new GBDeviceEvent[] { deleteRes };
                    break;
                default:
                    LOG.info("Unknown APPMANAGER event" + pebbleCmd);
                    break;
            }
            break;
        case ENDPOINT_PUTBYTES:
            pebbleCmd = buf.get();
            GBDeviceEventAppManagement installRes = new GBDeviceEventAppManagement();
            installRes.type = GBDeviceEventAppManagement.EventType.INSTALL;
            switch(pebbleCmd) {
                case PUTBYTES_INIT:
                    installRes.token = buf.getInt();
                    installRes.event = GBDeviceEventAppManagement.Event.SUCCESS;
                    break;
                default:
                    installRes.token = buf.getInt();
                    installRes.event = GBDeviceEventAppManagement.Event.FAILURE;
                    break;
            }
            devEvts = new GBDeviceEvent[] { installRes };
            break;
        case ENDPOINT_APPLICATIONMESSAGE:
        case ENDPOINT_LAUNCHER:
            pebbleCmd = buf.get();
            last_id = buf.get();
            UUID uuid = getUUID(buf);
            switch(pebbleCmd) {
                case APPLICATIONMESSAGE_PUSH:
                    LOG.info((endpoint == ENDPOINT_LAUNCHER ? "got LAUNCHER PUSH from UUID : " : "got APPLICATIONMESSAGE PUSH from UUID : ") + uuid);
                    AppMessageHandler handler = mAppMessageHandlers.get(uuid);
                    if (handler != null) {
                        currentRunningApp = uuid;
                        if (handler.isEnabled()) {
                            if (endpoint == ENDPOINT_APPLICATIONMESSAGE) {
                                ArrayList<Pair<Integer, Object>> dict = decodeDict(buf);
                                devEvts = handler.handleMessage(dict);
                            } else {
                                devEvts = handler.onAppStart();
                            }
                        } else {
                            devEvts = new GBDeviceEvent[] { null };
                        }
                    } else {
                        try {
                            devEvts = decodeDictToJSONAppMessage(uuid, buf);
                        } catch (JSONException e) {
                            LOG.error(e.getMessage());
                        }
                        if (!uuid.equals(currentRunningApp)) {
                            GBDeviceEventAppManagement gbDeviceEventAppManagement = new GBDeviceEventAppManagement();
                            gbDeviceEventAppManagement.uuid = uuid;
                            gbDeviceEventAppManagement.type = GBDeviceEventAppManagement.EventType.START;
                            gbDeviceEventAppManagement.event = GBDeviceEventAppManagement.Event.SUCCESS;
                            // prepend the
                            GBDeviceEvent[] concatEvents = new GBDeviceEvent[(devEvts != null ? devEvts.length : 0) + 1];
                            concatEvents[0] = gbDeviceEventAppManagement;
                            if (devEvts != null) {
                                System.arraycopy(devEvts, 0, concatEvents, 1, devEvts.length);
                            }
                            devEvts = concatEvents;
                        }
                    }
                    currentRunningApp = uuid;
                    break;
                case APPLICATIONMESSAGE_ACK:
                case APPLICATIONMESSAGE_NACK:
                    if (pebbleCmd == APPLICATIONMESSAGE_ACK) {
                        LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") ACK");
                    } else {
                        LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ") NACK");
                    }
                    GBDeviceEventAppMessage evtAppMessage = null;
                    if (endpoint == ENDPOINT_APPLICATIONMESSAGE && idLookup[last_id & 0xff] != null) {
                        evtAppMessage = new GBDeviceEventAppMessage();
                        if (pebbleCmd == APPLICATIONMESSAGE_ACK) {
                            evtAppMessage.type = GBDeviceEventAppMessage.TYPE_ACK;
                        } else {
                            evtAppMessage.type = GBDeviceEventAppMessage.TYPE_NACK;
                        }
                        evtAppMessage.id = idLookup[last_id & 0xff];
                        evtAppMessage.appUUID = currentRunningApp;
                    }
                    devEvts = new GBDeviceEvent[] { evtAppMessage };
                    break;
                case APPLICATIONMESSAGE_REQUEST:
                    LOG.info("got APPLICATIONMESSAGE/LAUNCHER (EP " + endpoint + ")  REQUEST");
                    devEvts = new GBDeviceEvent[] { null };
                    break;
                default:
                    break;
            }
            break;
        case ENDPOINT_PHONEVERSION:
            pebbleCmd = buf.get();
            switch(pebbleCmd) {
                case PHONEVERSION_REQUEST:
                    LOG.info("Pebble asked for Phone/App Version - repLYING!");
                    GBDeviceEventSendBytes sendBytes = new GBDeviceEventSendBytes();
                    sendBytes.encodedBytes = encodePhoneVersion(PHONEVERSION_REMOTE_OS_ANDROID);
                    devEvts = new GBDeviceEvent[] { sendBytes };
                    break;
                default:
                    break;
            }
            break;
        case ENDPOINT_DATALOG:
            devEvts = decodeDatalog(buf, length);
            break;
        case ENDPOINT_SCREENSHOT:
            devEvts = new GBDeviceEvent[] { decodeScreenshot(buf, length) };
            break;
        case ENDPOINT_EXTENSIBLENOTIFS:
        case ENDPOINT_NOTIFICATIONACTION:
            devEvts = decodeAction(buf);
            break;
        case ENDPOINT_PING:
            devEvts = new GBDeviceEvent[] { decodePing(buf) };
            break;
        case ENDPOINT_APPFETCH:
            devEvts = new GBDeviceEvent[] { decodeAppFetch(buf) };
            break;
        case ENDPOINT_SYSTEMMESSAGE:
            devEvts = new GBDeviceEvent[] { decodeSystemMessage(buf) };
            break;
        case ENDPOINT_APPRUNSTATE:
            devEvts = decodeAppRunState(buf);
            break;
        case ENDPOINT_BLOBDB:
            devEvts = new GBDeviceEvent[] { decodeBlobDb(buf) };
            break;
        case ENDPOINT_APPREORDER:
            devEvts = new GBDeviceEvent[] { decodeAppReorder(buf) };
            break;
        case ENDPOINT_APPLOGS:
            decodeAppLogs(buf);
            break;
        case ENDPOINT_VOICECONTROL:
            devEvts = new GBDeviceEvent[] { decodeVoiceControl(buf) };
            break;
        case ENDPOINT_AUDIOSTREAM:
            devEvts = new GBDeviceEvent[] { decodeAudioStream(buf) };
            // LOG.debug("AUDIOSTREAM DATA: " + GB.hexdump(responseData, 4, length));
            break;
        default:
            break;
    }
    return devEvts;
}
Also used : GBDeviceEvent(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent) JSONException(org.json.JSONException) GBDeviceEventCallControl(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl) GBDeviceEventVersionInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo) ByteBuffer(java.nio.ByteBuffer) GBDeviceEventAppManagement(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement) GBDeviceEventMusicControl(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl) GBDeviceEventAppMessage(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage) GBDeviceEventAppInfo(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo) GBDeviceEventSendBytes(nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes) UUID(java.util.UUID) GBDeviceApp(nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp) Pair(android.util.Pair)

Aggregations

GBDeviceEventAppInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppInfo)3 UUID (java.util.UUID)2 GBDeviceEventAppManagement (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppManagement)2 GBDeviceEventAppMessage (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventAppMessage)2 GBDeviceEventVersionInfo (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo)2 GBDeviceApp (nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceApp)2 Paint (android.graphics.Paint)1 Pair (android.util.Pair)1 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 GBDeviceEvent (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent)1 GBDeviceEventCallControl (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCallControl)1 GBDeviceEventMusicControl (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl)1 GBDeviceEventSendBytes (nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes)1 GBDeviceEventDataLogging (nodomain.freeyourgadget.gadgetbridge.deviceevents.pebble.GBDeviceEventDataLogging)1 JSONException (org.json.JSONException)1