diff options
| -rw-r--r-- | daemon/appmsgmanager.cpp | 32 | ||||
| -rw-r--r-- | daemon/appmsgmanager.h | 7 | ||||
| -rw-r--r-- | daemon/jskitmanager.cpp | 22 | ||||
| -rw-r--r-- | daemon/jskitmanager.h | 2 | ||||
| -rw-r--r-- | daemon/jskitobjects.cpp | 3 | ||||
| -rw-r--r-- | daemon/manager.cpp | 7 | ||||
| -rw-r--r-- | daemon/manager.h | 1 |
7 files changed, 60 insertions, 14 deletions
diff --git a/daemon/appmsgmanager.cpp b/daemon/appmsgmanager.cpp index 312043a..f24b8d9 100644 --- a/daemon/appmsgmanager.cpp +++ b/daemon/appmsgmanager.cpp @@ -69,6 +69,16 @@ void AppMsgManager::send(const QUuid &uuid, const QVariantMap &data, const std:: } } +void AppMsgManager::setMessageHandler(const QUuid &uuid, MessageHandlerFunc func) +{ + _handlers.insert(uuid, func); +} + +void AppMsgManager::clearMessageHandler(const QUuid &uuid) +{ + _handlers.remove(uuid); +} + uint AppMsgManager::lastTransactionId() const { return _lastTransactionId; @@ -236,6 +246,8 @@ void AppMsgManager::handlePushMessage(const QByteArray &data) if (!unpackPushMessage(data, &transaction, &uuid, &dict)) { logger()->warn() << "Failed to parse APP_MSG PUSH"; + watch->sendMessage(WatchConnector::watchAPPLICATION_MESSAGE, + buildNackMessage(transaction)); return; } @@ -244,7 +256,25 @@ void AppMsgManager::handlePushMessage(const QByteArray &data) QVariantMap msg = mapAppKeys(uuid, dict); logger()->debug() << "Mapped dict" << msg; - emit messageReceived(uuid, msg); + bool result; + + MessageHandlerFunc handler = _handlers.value(uuid); + if (handler) { + result = handler(msg); + } else { + // No handler? Let's just send an ACK. + result = false; + } + + if (result) { + logger()->debug() << "ACKing transaction" << transaction; + watch->sendMessage(WatchConnector::watchAPPLICATION_MESSAGE, + buildAckMessage(transaction)); + } else { + logger()->info() << "NACKing transaction" << transaction; + watch->sendMessage(WatchConnector::watchAPPLICATION_MESSAGE, + buildNackMessage(transaction)); + } } void AppMsgManager::handleAckMessage(const QByteArray &data, bool ack) diff --git a/daemon/appmsgmanager.h b/daemon/appmsgmanager.h index 9aaabd4..e52c544 100644 --- a/daemon/appmsgmanager.h +++ b/daemon/appmsgmanager.h @@ -19,6 +19,11 @@ public: void send(const QUuid &uuid, const QVariantMap &data, const std::function<void()> &ackCallback, const std::function<void()> &nackCallback); + + typedef std::function<bool(const QVariantMap &)> MessageHandlerFunc; + void setMessageHandler(const QUuid &uuid, MessageHandlerFunc func); + void clearMessageHandler(const QUuid &uuid); + uint lastTransactionId() const; uint nextTransactionId() const; @@ -30,7 +35,6 @@ public slots: signals: void appStarted(const QUuid &uuid); void appStopped(const QUuid &uuid); - void messageReceived(const QUuid &uuid, const QVariantMap &data); private: WatchConnector::Dict mapAppKeys(const QUuid &uuid, const QVariantMap &data); @@ -56,6 +60,7 @@ private slots: private: AppManager *apps; WatchConnector *watch; + QHash<QUuid, MessageHandlerFunc> _handlers; quint8 _lastTransactionId; struct PendingTransaction { diff --git a/daemon/jskitmanager.cpp b/daemon/jskitmanager.cpp index 6b291df..c73d32e 100644 --- a/daemon/jskitmanager.cpp +++ b/daemon/jskitmanager.cpp @@ -9,7 +9,6 @@ JSKitManager::JSKitManager(AppManager *apps, AppMsgManager *appmsg, QObject *par { connect(_appmsg, &AppMsgManager::appStarted, this, &JSKitManager::handleAppStarted); connect(_appmsg, &AppMsgManager::appStopped, this, &JSKitManager::handleAppStopped); - connect(_appmsg, &AppMsgManager::messageReceived, this, &JSKitManager::handleAppMessage); } JSKitManager::~JSKitManager() @@ -83,7 +82,7 @@ void JSKitManager::handleAppStopped(const QUuid &uuid) } } -void JSKitManager::handleAppMessage(const QUuid &uuid, const QVariantMap &data) +void JSKitManager::handleAppMessage(const QUuid &uuid, const QVariantMap &msg) { if (_curApp.uuid() == uuid) { logger()->debug() << "received a message for the current JSKit app"; @@ -94,7 +93,7 @@ void JSKitManager::handleAppMessage(const QUuid &uuid, const QVariantMap &data) } QJSValue eventObj = _engine->newObject(); - eventObj.setProperty("payload", _engine->toScriptValue(data)); + eventObj.setProperty("payload", _engine->toScriptValue(msg)); _jspebble->invokeCallbacks("appmessage", QJSValueList({eventObj})); } @@ -164,6 +163,19 @@ void JSKitManager::startJsApp() // Now load the actual script loadJsFile(_curApp.path() + "/pebble-js-app.js"); + // Setup the message callback + QUuid uuid = _curApp.uuid(); + _appmsg->setMessageHandler(uuid, [this, uuid](const QVariantMap &msg) { + QMetaObject::invokeMethod(this, "handleAppMessage", Qt::QueuedConnection, + Q_ARG(QUuid, uuid), + Q_ARG(QVariantMap, msg)); + + // Invoke the slot as a queued connection to give time for the ACK message + // to go through first. + + return true; + }); + // We try to invoke the callbacks even if script parsing resulted in error... _jspebble->invokeCallbacks("ready"); } @@ -174,6 +186,10 @@ void JSKitManager::stopJsApp() logger()->debug() << "stopping JS app"; + if (!_curApp.uuid().isNull()) { + _appmsg->clearMessageHandler(_curApp.uuid()); + } + _engine->collectGarbage(); _engine->deleteLater(); diff --git a/daemon/jskitmanager.h b/daemon/jskitmanager.h index 4239f91..b42e338 100644 --- a/daemon/jskitmanager.h +++ b/daemon/jskitmanager.h @@ -35,7 +35,7 @@ public slots: private slots: void handleAppStarted(const QUuid &uuid); void handleAppStopped(const QUuid &uuid); - void handleAppMessage(const QUuid &uuid, const QVariantMap &data); + void handleAppMessage(const QUuid &uuid, const QVariantMap &msg); private: bool loadJsFile(const QString &filename); diff --git a/daemon/jskitobjects.cpp b/daemon/jskitobjects.cpp index e55f2d5..3386f16 100644 --- a/daemon/jskitobjects.cpp +++ b/daemon/jskitobjects.cpp @@ -519,7 +519,8 @@ void JSKitGeolocation::handleTimeout() for (auto it = _watches.begin(); it != _watches.end(); /*no adv*/) { if (it->timer.hasExpired(it->timeout)) { - logger()->info() << "positioning timeout for watch" << it->watchId; + logger()->info() << "positioning timeout for watch" << it->watchId + << ", watch is" << it->timer.elapsed() << "ms old, timeout is" << it->timeout; invokeCallback(it->errorCallback, obj); if (it->once) { diff --git a/daemon/manager.cpp b/daemon/manager.cpp index b488432..8a00373 100644 --- a/daemon/manager.cpp +++ b/daemon/manager.cpp @@ -55,7 +55,6 @@ Manager::Manager(Settings *settings, QObject *parent) : connect(notifications, SIGNAL(twitterNotify(const QString &,const QString &)), SLOT(onTwitterNotify(const QString &,const QString &))); connect(notifications, SIGNAL(facebookNotify(const QString &,const QString &)), SLOT(onFacebookNotify(const QString &,const QString &))); - connect(appmsg, &AppMsgManager::messageReceived, this, &Manager::onAppMessage); connect(appmsg, &AppMsgManager::appStarted, this, &Manager::onAppOpened); connect(appmsg, &AppMsgManager::appStopped, this, &Manager::onAppClosed); @@ -395,14 +394,10 @@ void Manager::transliterateMessage(const QString &text) void Manager::onAppNotification(const QUuid &uuid, const QString &title, const QString &body) { + Q_UNUSED(uuid); watch->sendSMSNotification(title, body); } -void Manager::onAppMessage(const QUuid &uuid, const QVariantMap &data) -{ - emit proxy->AppMessage(uuid.toString(), data); -} - void Manager::onAppOpened(const QUuid &uuid) { currentAppUuid = uuid; diff --git a/daemon/manager.h b/daemon/manager.h index 18bd7bf..bf83f75 100644 --- a/daemon/manager.h +++ b/daemon/manager.h @@ -103,7 +103,6 @@ private slots: void setMprisMetadata(QVariantMap metadata); void onAppNotification(const QUuid &uuid, const QString &title, const QString &body); - void onAppMessage(const QUuid &uuid, const QVariantMap &data); void onAppOpened(const QUuid &uuid); void onAppClosed(const QUuid &uuid); }; |
