diff --git a/src/android/FullScreenImage.java b/src/android/FullScreenImage.java index d807c97..f07de92 100644 --- a/src/android/FullScreenImage.java +++ b/src/android/FullScreenImage.java @@ -13,14 +13,17 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.Locale; +import java.util.concurrent.ExecutorService; import android.annotation.SuppressLint; +import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; +import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Environment; @@ -33,13 +36,14 @@ import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaInterface; @SuppressLint("DefaultLocale") -public class FullScreenImage extends CordovaPlugin { +public class FullScreenImage extends CordovaPlugin implements CordovaInterface { private CallbackContext command; - private static final String LOG_TAG = "FullScreenImagePlugin"; + private static final String LOG_TAG = "FullScreenImagePlugin"; /** * Executes the request. @@ -62,20 +66,25 @@ public boolean execute (String action, JSONArray args, CallbackContext callback) throws JSONException { this.command = callback; + PluginResult resultStart = new PluginResult(PluginResult.Status.NO_RESULT); + resultStart.setKeepCallback(true); if ("showImageURL".equals(action)) { showImageURL(args); + callback.sendPluginResult(resultStart); return true; } if ("showImageBase64".equals(action)) { showImageBase64(args); + callback.sendPluginResult(resultStart); return true; } // Returning false results in a "MethodNotFound" error. + callback.sendPluginResult(new PluginResult(PluginResult.Status.ERROR)); return false; } @@ -120,16 +129,19 @@ public void showImageURL (JSONArray args) throws JSONException { inputStream.close(); path = Uri.fromFile(f); - } + } Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185). intent.setDataAndType(path, MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase(Locale.getDefault()))); - this.cordova.getActivity().startActivity(intent); + this.cordova.setActivityResultCallback (this); + this.cordova.startActivityForResult(this, intent, 0); - } catch (IOException e) { + } catch (Exception e) { + PluginResult result = new PluginResult(PluginResult.Status.ERROR); + result.setKeepCallback(false); + this.command.sendPluginResult(result); Log.d(LOG_TAG, "Could not create file: " + e.toString()); - } } @@ -163,13 +175,15 @@ public void showImageBase64 (JSONArray args) throws JSONException{ Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(path, "image/*"); - this.cordova.getActivity().startActivity(intent); + this.cordova.setActivityResultCallback (this); + this.cordova.startActivityForResult(this, intent, 0); - } catch (IOException e) { + } catch (Exception e) { + PluginResult result = new PluginResult(PluginResult.Status.ERROR); + result.setKeepCallback(false); + this.command.sendPluginResult(result); Log.d(LOG_TAG, "Could not create file: " + e.toString()); } - - } @@ -194,4 +208,45 @@ private File getTempDirectoryPath() { cache.mkdirs(); return cache; } + + @Override + public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) { + + } + + @Override + public void setActivityResultCallback(CordovaPlugin plugin) { + + } + + @Override + public Activity getActivity() { + return null; + } + + @Override + public ExecutorService getThreadPool() { + return null; + } + + @Override + public void requestPermission(CordovaPlugin plugin, int requestCode, String permission) { + + } + + @Override + public void requestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) { + + } + + @Override + public boolean hasPermission(String permission) { + return false; + } + + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + PluginResult result = new PluginResult(PluginResult.Status.OK); + result.setKeepCallback(false); + this.command.sendPluginResult(result); + } } diff --git a/src/ios/FullScreenImage.m b/src/ios/FullScreenImage.m index ffdc757..9dc9bec 100644 --- a/src/ios/FullScreenImage.m +++ b/src/ios/FullScreenImage.m @@ -33,39 +33,47 @@ - (void) showImageURL:(CDVInvokedUrlCommand*)command{ NSString *fullPath = [[command.arguments objectAtIndex:0] valueForKey:@"url"]; NSURL *URL = [NSURL URLWithString:fullPath]; + __block BOOL result; + __block CDVPluginResult* resultPlugin = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; BOOL fileExists = URL.isFileURL && [[NSFileManager defaultManager] fileExistsAtPath:URL.path]; - + if (!fileExists) { NSString *soundFilePath = [NSString stringWithFormat:@"%@/www/%@",[[NSBundle mainBundle] resourcePath],fullPath]; URL = [NSURL fileURLWithPath:soundFilePath]; } - + if (URL) { [self.documentURLs addObject:URL]; [self setupDocumentControllerWithURL:URL]; double delayInSeconds = 0.1; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ - [self.docInteractionController presentPreviewAnimated:YES]; + result = [self.docInteractionController presentPreviewAnimated:YES]; + + if(result){ + resultPlugin = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } + [self.commandDelegate sendPluginResult:resultPlugin callbackId:command.callbackId]; + }); } }]; } - (void) showImageBase64:(CDVInvokedUrlCommand*)command{ - + [self.commandDelegate runInBackground:^{ NSString *fullPath = [[command.arguments objectAtIndex:0] valueForKey:@"base64"]; - + NSString *imageName = [[command.arguments objectAtIndex:0] valueForKey:@"name"]; - + NSString *imageType = [[command.arguments objectAtIndex:0] valueForKey:@"type"]; - + if([imageName isKindOfClass:[NSNull class]] || [imageName isEqualToString:@""]){ imageName = @"default"; } - + if([imageType isKindOfClass:[NSNull class]] || [imageType isEqualToString:@""]){ NSData *imageDatatest = [NSData dataFromBase64String:fullPath]; @@ -76,31 +84,39 @@ - (void) showImageBase64:(CDVInvokedUrlCommand*)command{ // https://github.com/keensoft/FullScreenImage-Cordova-Plugin/issues/22 NSData *imageData = [NSData dataFromBase64String:fullPath]; UIImage *ret = [UIImage imageWithData:imageData]; - - + + NSData *imageDataSaved=UIImagePNGRepresentation(ret); - - + + NSString *docsDir; NSArray *dirPaths; - - + __block BOOL result; + __block CDVPluginResult* resultPlugin = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; + + dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *completeImageName = [NSString stringWithFormat:@"%@.%@",imageName,imageType]; NSString *databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:completeImageName]]; [imageDataSaved writeToFile:databasePath atomically:YES]; - + NSURL *imageURL=[NSURL fileURLWithPath:databasePath]; - + if (imageURL) { [self.documentURLs addObject:imageURL]; [self setupDocumentControllerWithURL:imageURL]; double delayInSeconds = 0.1; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ - [self.docInteractionController presentPreviewAnimated:YES]; + result = [self.docInteractionController presentPreviewAnimated:YES]; + + if(result){ + resultPlugin = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } + [self.commandDelegate sendPluginResult:resultPlugin callbackId:command.callbackId]; }); + } }]; } diff --git a/www/fullscreenimage.js b/www/fullscreenimage.js index f6a60dc..0b19a18 100644 --- a/www/fullscreenimage.js +++ b/www/fullscreenimage.js @@ -20,10 +20,9 @@ var FullScreenImage = function () { */ FullScreenImage.prototype.showImageURL = function (url) { - - exec(null, null, "FullScreenImage", "showImageURL", [{"url":url}]); - - + return new Promise(function(resolve, reject) { + exec(resolve, reject, "FullScreenImage", "showImageURL", [{"url":url}]); + }); }; /* @@ -37,10 +36,9 @@ FullScreenImage.prototype.showImageURL = function (url) { */ FullScreenImage.prototype.showImageBase64 = function (base64String, name, type) { - - exec(null, null, "FullScreenImage", "showImageBase64", [{"base64":base64String, "name":name, "type":type}]); - - + return new Promise(function(resolve, reject) { + exec(resolve, reject, "FullScreenImage", "showImageBase64", [{"base64":base64String, "name":name, "type":type}]); + }); }; module.exports = new FullScreenImage();