/** * This method is called when there is an event for this widget. * It passes on the event to all widget's listeners. * * If the event is #MAW_EVENT_WEB_VIEW_HOOK_INVOKED the data * parameter "urlData" gets deleted automatically after the * event is processed. * * @param widgetEventData The data for the widget event. */ void WebView::handleWidgetEvent(MAWidgetEventData* widgetEventData) { Widget::handleWidgetEvent(widgetEventData); if ( MAW_EVENT_WEB_VIEW_CONTENT_LOADING == widgetEventData->eventType) { for (int i = 0; i < mWebViewListeners.size(); i++) { mWebViewListeners[i]->webViewContentLoading( this, widgetEventData->status); } } else if (MAW_EVENT_WEB_VIEW_HOOK_INVOKED == widgetEventData->eventType) { int hookType = widgetEventData->hookType; MAHandle url = widgetEventData->urlData; for (int i = 0; i < mWebViewListeners.size(); i++) { mWebViewListeners[i]->webViewHookInvoked( this, hookType, url); } // Here the data object gets detroyed. maDestroyPlaceholder(url); } }
void WidgetSkin::flushCacheUntilNewImageFits(int numPixels) { int totalPixelsInCache = numPixels; HashMap<CacheKey, CacheElement>::Iterator iter = sCache.begin(); while(iter != sCache.end()) { totalPixelsInCache += iter->first.w*iter->first.h; iter++; } int currentTime = maGetMilliSecondCount(); while(totalPixelsInCache>maxCacheSize) { int oldest = currentTime; iter = sCache.begin(); HashMap<CacheKey, CacheElement>::Iterator best = sCache.end(); while(iter != sCache.end()) { if(iter->second.lastUsed<oldest) { oldest = iter->second.lastUsed; best = iter; } iter++; } if(best == sCache.end()) break; maDestroyPlaceholder(best->second.image); sCache.erase(best); totalPixelsInCache-=iter->first.w*iter->first.h; } }
/** * Inherited from HighLevelHttpConnection. * Called when the HTTP connection has finished * downloading data. * Calls onDownloadComplete. * @param data Handle to the data, will be * 0 on error, > 0 on success. * @param result Result code, RES_OK on success, * otherwise an HTTP error code. */ void HighLevelImageDownloader::dataDownloaded(MAHandle data, int result) { // The resulting image. MAHandle image = 0; // Do we have any data? if (data) { // Convert data to image. image = maCreatePlaceholder(); int res = maCreateImageFromData( image, data, 0, maGetDataSize(data)); // Do we have an error? if (RES_OUT_OF_MEMORY == res) { // The image could not be created, set data handle to zero. image = 0; } // Deallocate the data object, we are done with it. maDestroyPlaceholder(data); } // Notify download complete. onDownloadComplete(image); }
Controller::~Controller() { // remember the selected font scale and path char path[FILENAME_MAX + 1]; getcwd(path, FILENAME_MAX); int pathLength = strlen(path) + 1; int dataLength = (sizeof(int) * 3) + pathLength; MAHandle data = maCreatePlaceholder(); if (maCreateData(data, dataLength) == RES_OK) { int storeVersion = STORE_VERSION; int offset = 0; // write the version number maWriteData(data, &storeVersion, offset, sizeof(int)); offset += sizeof(int); // write the fontScale maWriteData(data, &_fontScale, offset, sizeof(int)); offset += sizeof(int); // write the current path maWriteData(data, &pathLength, offset, sizeof(int)); maWriteData(data, path, offset + sizeof(int), pathLength); MAHandle store = maOpenStore(PACKAGE, MAS_CREATE_IF_NECESSARY); maWriteStore(store, data); maCloseStore(store, 0); } maDestroyPlaceholder(data); delete _output; delete [] _programSrc; }
/** * Here we handle events from the web view. We turn on or off * location tracking depending of the message sent from JavaScript. */ void handleWidgetEvent(MAWidgetEventData* widgetEvent) { // Handle messages from the WebView widget. if (MAW_EVENT_WEB_VIEW_HOOK_INVOKED == widgetEvent->eventType) { // Get message content, this is the url set in JavaScript. MAUtil::String message = Util::createTextFromHandle( widgetEvent->urlData); // Note: It is very important to free the data handle // when we are done with using it, since a new data object // is allocated for each message. (In the high-level library // this is done automatically.) maDestroyPlaceholder(widgetEvent->urlData); // Check the message string and execute the corresponding syscall. if (0 == message.find("StartTrackingGeoLocation")) { maLocationStart(); } else if (0 == message.find("StopTrackingGeoLocation")) { maLocationStop(); } } }
/** * Handles HOOK_INVOKED events for WebViews in the app. * This code enables WebViews to send messages to each other. * * The only thing that work reliable from other WebViews than * the main one, are CallJS and calls that do not return anything, * like mosync.app.sendToBackground(). * * Apps are supposed to use the main WebView to for accessing the * fulll Wormhole JS API, and only use mosync.nativeui.callJS() * from other WebViews. This way, the main WebView becomes a * mediator, which is a good design because native access is * restricted to one point. */ void HybridMoblet::customEvent(const MAEvent& event) { if (EVENT_TYPE_WIDGET == event.type) { MAWidgetEventData* widgetEventData = (MAWidgetEventData*)event.data; MAWidgetHandle webViewHandle = widgetEventData->widgetHandle; // If target object is the main WebView, then we just return // because this is handled by the NativeUI library event processing, // which will invoke HybridMoblet::handleWebViewMessage(). if (getWebView()->getWidgetHandle() == webViewHandle) { return; } // Process HOOK_INVOKED messages. This makes CallJS messages work. if (MAW_EVENT_WEB_VIEW_HOOK_INVOKED == widgetEventData->eventType) { // We don't care about the hook type. // int hookType = widgetEventData->hookType; MAHandle data = widgetEventData->urlData; handleWebViewMessage(webViewHandle, data); // Free data. maDestroyPlaceholder(data); } } }
/** * 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; }
/** * Called when the download is complete * @param downloader The downloader who finished it's operation * @param data A handle to the data that was downloaded */ void BundleDownloader::finishedDownloading(Downloader* downloader, MAHandle data) { lprintfln("Completed download"); //extract the file System mBundleListener->bundleDownloaded(data); maDestroyPlaceholder(mResourceFile); //loadSavedApp(); }
MoSyncCamController::~MoSyncCamController() { if ( mDisplayedImageHandle > 0 ) { maDestroyPlaceholder(mDisplayedImageHandle); } if ( mLastSnapshotDataHandle > 0 ) { maDestroyPlaceholder(mLastSnapshotDataHandle); } mCameraScreen->unregisterCameraListener(this); delete mCameraScreen; delete mImageViewerScreen; }
void WidgetSkin::flushCache() { HashMap<CacheKey, CacheElement>::Iterator iter = sCache.begin(); while(iter != sCache.end()) { maDestroyPlaceholder(iter->second.image); iter++; } sCache.clear(); }
MAHandle FacebookResponse::getImageData() const { MAHandle data = maCreatePlaceholder(); maCreateData(data, getDataSize()); maWriteData(data, getData(), 0, getDataSize()); MAHandle image = maCreatePlaceholder(); maCreateImageFromData(image, data, 0, getDataSize()); maDestroyPlaceholder(data); return image; }
bool Controller::construct() { MAExtent screenSize = maGetScrSize(); _output = new AnsiWidget(this, EXTENT_X(screenSize), EXTENT_Y(screenSize)); _output->construct(); _initialFontSize = _output->getFontSize(); _runMode = init_state; opt_ide = IDE_NONE; opt_graphics = true; opt_pref_bpp = 0; opt_nosave = true; opt_interactive = true; opt_verbose = false; opt_quiet = true; opt_command[0] = 0; opt_usevmt = 0; os_graphics = 1; // restore the selected font scale and path MAHandle data = maCreatePlaceholder(); MAHandle store = maOpenStore(PACKAGE, 0); if (store != STERR_NONEXISTENT) { if (maReadStore(store, data) == RES_OK) { int offset = 0; int storeVersion; int pathLength; char path[FILENAME_MAX + 1]; maReadData(data, &storeVersion, offset, sizeof(int)); offset += sizeof(int); if (storeVersion == STORE_VERSION) { maReadData(data, &_fontScale, offset, sizeof(int)); offset += sizeof(int); if (_fontScale != 100) { int fontSize = (_initialFontSize * _fontScale / 100); _output->setFontSize(fontSize); } maReadData(data, &pathLength, offset, sizeof(int)); maReadData(data, &path, offset+ sizeof(int), pathLength); if (pathLength > 1) { chdir(path); } } } maCloseStore(store, 0); } maDestroyPlaceholder(data); return true; }
void MoSyncCamController::snapshotRequested() { mCameraScreen->showSnapshotInProgress(); if ( mLastSnapshotDataHandle > 0 ) { maDestroyPlaceholder(mLastSnapshotDataHandle); mLastSnapshotDataHandle = 0; } mLastSnapshotDataHandle = maCreatePlaceholder(); mCameraScreen->takeSnapshot(mLastSnapshotDataHandle); }
/** * The destructor deletes the main widget * and all of its children. */ NativeScreen::~NativeScreen() { // Remove the listener MAUtil::Environment::getEnvironment().removeCustomEventListener(this); // Delete the main widget, also deletes child widgets. maWidgetDestroy(mMainLayout); if (mLastDisplayedImageHandle != -1) { maDestroyPlaceholder(mLastDisplayedImageHandle); } }
/** * Destructor. */ Purchase::~Purchase() { PurchaseManager::getInstance()->unregisterPurchase(this); maPurchaseDestroy(mHandle); // If the purchase is restored do not destroy placeholder. if (!mIsRestored) { maDestroyPlaceholder(mHandle); } mPurchaseEventListeners.clear(); delete mReceipt; }
void MoSyncCamController::imageViewingDone() { if ( !isDisplayed(*mCameraScreen) ) { mCameraScreen->showWithTransition(mBackwardTransition, SCREEN_TRANSITION_DURATION); if ( mDisplayedImageHandle > 0 ) { maDestroyPlaceholder(mDisplayedImageHandle); mDisplayedImageHandle = 0; } setCurrentScreen(*mCameraScreen); } }
void MoSyncCamController::snapshotFinished( const NativeUI::CameraSnapshotData& imageData ) { bool snapshotIsAvailable = true; if ( imageData.resultCode != MA_CAMERA_RES_OK ) { maAlert("Camera", "Snapshot failed", "OK", NULL, NULL); snapshotIsAvailable = false; if ( mLastSnapshotDataHandle > 0 ) { maDestroyPlaceholder(mLastSnapshotDataHandle); mLastSnapshotDataHandle = 0; } } mCameraScreen->hideSnapshotInProgress(snapshotIsAvailable); }
int NativeScreen::handleImageData(MAHandle myImageData) { printf("handleImageData(%d)", myImageData); int resCode = -1; MAHandle hImage = maCreatePlaceholder(); int dataSize = maGetDataSize(myImageData); int createImageRes = maCreateImageFromData(hImage, myImageData, 0, dataSize); // Used for testing only. MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize) + " res = " + MAUtil::integerToString(createImageRes); printf("\n%s\n", info.c_str()); if (createImageRes != RES_OK) { // If the Android VM gets an out of memory exception, get the image handle instead. maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL); maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false"); maImagePickerOpen(); } else { char imgHandle[256]; sprintf(imgHandle, "%d", hImage); printf("imgHandle=%s---hImage=%d", imgHandle, hImage); // display the preview resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle); } // at this point the new selected image is either displayed or non-existent (out of memory) // the former displayed image (if exists) can be now safely deleted and reused if (mLastDisplayedImageHandle != -1) { maDestroyPlaceholder(mLastDisplayedImageHandle); } mLastDisplayedImageHandle = hImage; return resCode; }
/** * Loads an image from a file and returns the handle to it. * * @param imagePath relative path to the image file. */ MAHandle ResourceMessageHandler::loadImageResource(const char* imagePath) { if (!getFileUtil()) { return 0; } // Get the current apllication directory path. String appPath = getFileUtil()->getAppPath(); // Construct image path. char completePath[2048]; sprintf(completePath, "%s%s", appPath.c_str(), imagePath); // Load the image and create a data handle from it. MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ); if (imageFile < 0) { return 0; } int fileSize = maFileSize(imageFile); if (fileSize < 1) { return 0; } // Create buffer to hold file data. MAHandle fileData = maCreatePlaceholder(); int result = maCreateData(fileData, fileSize); if (RES_OK != result) { maDestroyPlaceholder(fileData); return 0; } // Read data from file. result = maFileReadToData(imageFile, fileData, 0, fileSize); maFileClose(imageFile); if (result < 0) { maDestroyPlaceholder(fileData); return 0; } // Create image. MAHandle imageHandle = maCreatePlaceholder(); result = maCreateImageFromData( imageHandle, fileData, 0, maGetDataSize(fileData)); maDestroyPlaceholder(fileData); if (RES_OK != result) { maDestroyPlaceholder(imageHandle); return 0; } // Return the handle to the loaded image. return imageHandle; }
/** * Handle widget events. * @param event A MoSync event data structure. */ void NativeScreen::customEvent(const MAEvent& event) { // Get the information sent by the widget. MAWidgetEventData* widgetEventData = (MAWidgetEventData*) event.data; if ( event.type == EVENT_TYPE_IMAGE_PICKER) { if ( event.imagePickerState == 1 ) { // ready, get handle MAHandle myImage = event.imagePickerItem; char checkboxBuffer[BUF_SIZE]; maWidgetGetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, checkboxBuffer, BUF_SIZE); MAUtil::String value = MAUtil::lowerString(checkboxBuffer); if ( strcmp(value.c_str(),"true") == 0 ) { MAHandle hImage = maCreatePlaceholder(); int dataSize = maGetDataSize(event.imagePickerItem); int createImageRes= maCreateImageFromData(hImage, event.imagePickerItem, 0, dataSize); // Used for testing only. MAUtil::String info = "Ready.Size = " + MAUtil::integerToString(dataSize) + "res = " + MAUtil::integerToString(createImageRes) + ", mime type = " + MAUtil::integerToString(event.imagePickerEncodingType); if ( createImageRes != RES_OK ) { maAlert("Memory Warning", " The image cannot be created. Try again", NULL, NULL, NULL); maWidgetSetProperty(mEventReturnTypeCheckbox, MAW_CHECK_BOX_CHECKED, "false"); // If the Android VM gets an out of memory exception, get the image handle instead. maImagePickerOpen(); } else { char imgHandle[256]; sprintf(imgHandle, "%d", hImage); int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, imgHandle); } maDestroyPlaceholder(hImage); } else { char buffer[256]; sprintf(buffer, "%d", myImage); int resCode = maWidgetSetProperty(mPreview, MAW_IMAGE_IMAGE, buffer); } setLabelText(mLabel, "Preview is available"); } else { setLabelText(mLabel, "The user canceled the image selection"); } } // Check that the event was a click (touch) event. if (widgetEventData->eventType == MAW_EVENT_CLICKED) { // Handle the event emitted by the widget widgetClicked(widgetEventData->widgetHandle); } }
/** * Copy a file. Overwrites the destination file. * @return 0 on success <0 on error. */ static int FileCopyFile( const String& sourcePath, const String& destinationPath) { // Open source file. MAHandle sourceFile = maFileOpen(sourcePath.c_str(), MA_ACCESS_READ_WRITE); if (sourceFile < 0) { return -1; } // Check that source file exists. int exists = maFileExists(sourceFile); if (1 != exists) { maFileClose(sourceFile); return -1; } // Get and check source size. int fileSize = maFileSize(sourceFile); if (fileSize < 0) { maFileClose(sourceFile); return -1; } // Create data object for source data to copy. MAHandle data = maCreatePlaceholder(); int createDataResult = maCreateData(data, fileSize); if (RES_OK != createDataResult) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } int readResult = maFileReadToData(sourceFile, data, 0, fileSize); if (readResult < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // This deletes the destination file if it already exists. FileDeleteFile(destinationPath); // Create destination file. bool createSuccess = FileCreatePath(destinationPath); if (!createSuccess) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Open destination file. MAHandle destinationFile = maFileOpen(destinationPath.c_str(), MA_ACCESS_READ_WRITE); if (destinationFile < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Write data to destination file. int writeResult = maFileWriteFromData(destinationFile, data, 0, fileSize); if (writeResult < 0) { maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); return -1; } // Close files and free data object. maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); // Success. return 0; }
static void DeallocateHandle(MAHandle handle) { maDestroyPlaceholder(handle); }
Font::~Font() { if(mFontImage) { maDestroyPlaceholder(mFontImage); } }