void MessageHandler::handleMoSyncMessage( Wormhole::MessageStream& message, MAWidgetHandle webViewHandle, Wormhole::HybridMoblet* moblet) { const char* p = message.getNext(); const char* logMessage; if (0 == strcmp(p, "ExitApplication")) { // Exit the application. moblet->exit(); } else if (0 == strcmp(p, "SendToBackground")) { maSendToBackground(); } else if (0 == strcmp(p, "SysLog")) { logMessage = message.getNext(); maWriteLog(logMessage, strlen(logMessage)); } else if (0 == strcmp(p, "OpenExternalURL")) { maPlatformRequest(message.getNext()); } else if (0 == strcmp(p, "OpenWormhole")) { moblet->openWormhole(webViewHandle); } }
/** * Implementation of standard API exposed to JavaScript * This function is used to detect different messages from JavaScript * and call the respective function in MoSync. * * @return true if stream was handled, false if not. */ bool ResourceMessageHandler::handleMessage(Wormhole::MessageStream& stream) { char buffer[512]; const char * action = stream.getNext(); if (0 == strcmp("loadImage", action)) { const char* imagePath = stream.getNext(); const char* imageID = stream.getNext(); // Load the Image resource. MAHandle imageHandle = loadImageResource(imagePath); if (imageHandle > 0) { sprintf(buffer, "mosync.resource.imageLoaded(\"%s\", %d)", imageID, imageHandle); mWebView->callJS(buffer); } else { // TODO: Better way to inform about the error? // Call JS function with error code? // mosync.resource.imageLoaded(<imageID>, -1) ?? char errorMessage[1024]; sprintf(errorMessage, "@@@ MoSync: ResourceMessageHandler could not load image: %s", imagePath); maWriteLog(errorMessage, strlen(errorMessage)); } } else if (0 == strcmp("loadRemoteImage", action)) { const char* imageURL = stream.getNext(); const char* imageID = stream.getNext(); MAHandle imageHandle = maCreatePlaceholder(); mImageDownloader->beginDownloading(imageURL,imageHandle); sprintf(buffer, "mosync.resource.imageDownloadStarted(\"%s\", %d)", imageID, imageHandle); mWebView->callJS(buffer); } else if (0 == strcmp("DestroyPlaceholder", action)) { MAHandle handle = stringToInteger(stream.getNext()); maDestroyPlaceholder(handle); } else if (0 == strcmp("sendRemoteLogMessage", action)) { const char* url = stream.getNext(); const char* message = stream.getNext(); sendRemoteLogMessage(message, url); } return true; }
/** * Function that opens the requested URL. */ void handlePlatformRequest(Wormhole::MessageStream& message) { // Obtain URL parameter sent from JavaScript. const char* url = message.getNext(); // Make the request to open the URL. maPlatformRequest(url); }
/** * Calls a JavaScript callback function using the "callbackId" * parameter. The callbackId is supplied automatically when * calling mosync.bridge.send wuth a callback function. * @param message The message stream from which to get the * callback id. * @param result A string that contains the data to be returned * to the JavaScript callback function. * @param success Success value passed back to JavaScript, used * to indicate success or error of a call. */ void callCallbackWithResult( Wormhole::MessageStream& message, const String& result, bool success) { // Get the callbackID parameter. const char* callbackId = message.getNext(); // Call JavaScript reply handler. String successValue = success ? "true" : "false"; String script = "mosync.bridge.reply("; script += callbackId; script += ",'" + result + "'"; script += "," + successValue + ")"; lprintfln("@@@ JS: %s", script.c_str()); message.getWebView()->callJS(script); }
/** * Invoked from JavaScript to evaluate a JS script in a WebView. */ void MessageHandler::handleCallJSMessage( Wormhole::MessageStream& message, Wormhole::HybridMoblet* moblet) { // TODO: Add error handling for missing parameters. // Get the native MoSync widget handle for the WebView // this call should be forwarded to. int webViewHandle = MAUtil::stringToInteger(message.getNext()); // When the handle is zero, we use the main WebView // (which is usually hidden in a NativeUI app). if (0 == webViewHandle) { webViewHandle = moblet->getWebView()->getWidgetHandle(); } // Evaluate the JavaScript code in the target WebView. const char* script = message.getNext(); moblet->callJS(webViewHandle, script); }
/** * Set get the score of a named player. * @param message */ void serviceSetScore(Wormhole::MessageStream& message) { lprintfln("@@@ serviceSetScore"); // Get the name of the player. const char* name = message.getNext(); // Get the new score value. const char* score = message.getNext(); // Update the score for the given name. char query[128]; sprintf(query, "UPDATE player SET score=%s WHERE name='%s'", score, name); MAHandle result = maDBExecSQL(mDB, query); if (MA_DB_OK == result) { callCallbackWithResult(message, "Updated score", true); } else { callCallbackWithResult(message, "Failed to update score", false); } }
/** * Get get the score of a named player and return the result * to a JavaScript callback function. * @param message */ void serviceGetScore(Wormhole::MessageStream& message) { lprintfln("@@@ serviceGetScore"); // Get the name of the player. const char* name = message.getNext(); // Query the score for the given name. char query[128]; sprintf(query, "SELECT score FROM player WHERE name='%s'", name); MAHandle cursor = maDBExecSQL(mDB, query); if ((cursor > 0) && (MA_DB_OK == maDBCursorNext(cursor))) { // Read the score into a String object. String score; int result = DBUtil::getColumnString( cursor, // Cursor handle. 0, // Index of column (we have only one column in the // result, zero is the index of the first column). score // String set to the score value. ); if (MA_DB_OK == result) { lprintfln("@@@ serviceGetScore result: %s", score.c_str()); // Return result to JavaScript and exit this function. callCallbackWithResult(message, score.c_str(), true); maDBCursorDestroy(cursor); return; } maDBCursorDestroy(cursor); } // Return error to JavaScript. callCallbackWithResult(message, "Could not read score", false); }
void vibrate(Wormhole::MessageStream& message) { int duration = MAUtil::stringToInteger(message.getNext()); maVibrate(duration); }
void orent(Wormhole::MessageStream& message) { MAUtil::String (url) = MAUtil::String(message.getNext()); showPage(url); }
/** * Implementation of standard API exposed to JavaScript * This function is used to detect different messages from JavaScript * and call the respective function in MoSync. * * @return true if stream was handled, false if not. */ bool NativeUIMessageHandler::handleMessage(Wormhole::MessageStream& stream) { char buffer[1024]; printf("Getting the next action \n"); char * action = (char*)stream.getNext(); printf("action: %s\n", action); // Widget Handling Calls if(0 == strcmp("maWidgetCreate", action)) { char* widgetType = (char*)stream.getNext(); char* widgetID = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); printf("maWidgetCreate: %s, %s, %s\n", widgetType, widgetID, callbackID); int numParams = stringToInteger(stream.getNext()); MAWidgetHandle widget = maWidgetCreate(widgetType); if(widget <= 0) { sprintf(buffer,"'%s', %d", callbackID, widget); sendNativeUIError(buffer); } else { if(numParams > 0) { for(int i = 0; i < numParams/2; i++) { char* property = (char*)stream.getNext(); char* value = (char*)stream.getNext(); printf("maWidgetSetProperty %s, %s\n", property, value); int res = maWidgetSetProperty(widget, property, value); if(res < 0) { printf("could not set property\n"); } else { printf("set property done\n"); } } } //We use a special callback for widget creation printf("calling CallBack \n"); sprintf( buffer, "mosync.nativeui.createCallback('%s', '%s', %d)", callbackID, widgetID, widget); printf("Done creatign the script %s\n", buffer); mWebView->callJS(buffer); printf("done Calling Callback"); } } else if(0 == strcmp("maWidgetDestroy", action)) { MAWidgetHandle widget = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetDestroy(widget); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetAddChild", action) ) { MAWidgetHandle parent = stringToInteger(stream.getNext()); MAWidgetHandle child = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetAddChild(parent, child); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetInsertChild", action)) { MAWidgetHandle parent = stringToInteger((char*)stream.getNext()); MAWidgetHandle child = stringToInteger((char*)stream.getNext()); int index = stringToInteger((char*)stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetInsertChild(parent, child, index); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetRemoveChild", action)) { MAWidgetHandle child = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetRemoveChild(child); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetModalDialogShow", action)) { MAWidgetHandle dialogHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetModalDialogShow(dialogHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetModalDialogHide", action)) { MAWidgetHandle dialogHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetModalDialogHide(dialogHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetScreenShow", action)) { MAWidgetHandle screenHandle = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetScreenShow(screenHandle); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetStackScreenPush", action)) { MAWidgetHandle stackScreen = stringToInteger(stream.getNext()); MAWidgetHandle newScreen = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetStackScreenPush(stackScreen, newScreen); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetStackScreenPop", action)) { MAWidgetHandle stackScreen = stringToInteger(stream.getNext()); char* callbackID = (char*)stream.getNext(); int res = maWidgetStackScreenPop(stackScreen); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetSetProperty", action)) { MAWidgetHandle widget = stringToInteger(stream.getNext()); char *property = (char*)stream.getNext(); char *value = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); int res = maWidgetSetProperty(widget, property, value); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUISuccess(buffer); } } else if(0 == strcmp("maWidgetGetProperty", action)) { char value[1024]; MAWidgetHandle widget = stringToInteger(stream.getNext()); char* property = (char*)stream.getNext(); char* callbackID = (char*)stream.getNext(); int res = maWidgetGetProperty(widget, property, value, 1024); if(res < 0) { sprintf(buffer,"'%s', %d", callbackID, res); sendNativeUIError(buffer); } else { sprintf(buffer,"'%s', '%s', '%s'", callbackID, property, value); sendNativeUISuccess(buffer); } } // Tell the WebView that we have processed the stream, so that // it can send the next one. char replyScript[1024]; char * mosyncCallBackId = (char*)stream.getNext(); if(mosyncCallBackId != NULL) { sprintf( replyScript, "mosync.bridge.reply(%s)", mosyncCallBackId); printf("calling general callback %s\n", replyScript); mWebView->callJS(replyScript); } return true; }