diff --git a/README.md b/README.md index 2601553..51db739 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,7 @@ To add these entries into the `info.plist`, you can use the `edit-config` tag in - [camera](#module_camera) - [.getPicture(successCallback, errorCallback, options)](#module_camera.getPicture) + - [.stop()](#module_camera.stop) - [.cleanup()](#module_camera.cleanup) - [.onError](#module_camera.onError) : function - [.onSuccess](#module_camera.onSuccess) : function @@ -273,6 +274,31 @@ More examples [here](#camera-getPicture-examples). Quirks [here](#camera-getPict ```js navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions); ``` + + + +### camera.stop() +Closes the active camera or photo library picker UI. + +__Supported Platforms__ + +- Android +- iOS + +**Kind**: static method of [camera](#module_camera) +**Example** +```js +navigator.camera.stop(onSuccess, onFail); + +function onSuccess() { + console.log('Camera stopped successfully.'); +} + +function onFail(message) { + alert('Failed because: ' + message); +} +``` + ### camera.cleanup() diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index af03a9c..ce6ca2e 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -102,6 +102,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect private static final String IMAGE_URI_KEY = "imageUri"; private static final String TAKE_PICTURE_ACTION = "takePicture"; + private static final String STOP_ACTION = "stop"; public static final int PERMISSION_DENIED_ERROR = 20; public static final int TAKE_PIC_SEC = 0; @@ -146,12 +147,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect * @return A PluginResult object with a status and message. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - this.callbackContext = callbackContext; - this.applicationId = cordova.getContext().getPackageName(); this.applicationId = preferences.getString("applicationId", this.applicationId); if (action.equals(TAKE_PICTURE_ACTION)) { + this.callbackContext = callbackContext; this.srcType = CAMERA; this.destType = FILE_URI; this.saveToPhotoAlbum = false; @@ -216,6 +216,10 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect r.setKeepCallback(true); callbackContext.sendPluginResult(r); + return true; + } else if (action.equals(STOP_ACTION)) { + this.stopCamera(); + callbackContext.success(); return true; } return false; @@ -307,6 +311,24 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect } } + /** + * Closes any active camera or media picker activity started by this plugin. + */ + public void stopCamera() { + if (this.cordova == null || this.cordova.getActivity() == null) { + return; + } + + int[] sourceTypes = { CAMERA, PHOTOLIBRARY, SAVEDPHOTOALBUM }; + int[] returnTypes = { DATA_URL, FILE_URI }; + + for (int sourceType : sourceTypes) { + for (int returnType : returnTypes) { + this.cordova.getActivity().finishActivity((sourceType + 1) * 16 + returnType + 1); + } + } + } + public void takePicture(int returnType, int encodingType) { // Let's use the intent and see what happens diff --git a/src/ios/CDVCamera.h b/src/ios/CDVCamera.h index 63c9b86..48bd907 100644 --- a/src/ios/CDVCamera.h +++ b/src/ios/CDVCamera.h @@ -121,6 +121,7 @@ typedef NSUInteger CDVMediaType; - (void)takePicture:(CDVInvokedUrlCommand*)command; - (void)cleanup:(CDVInvokedUrlCommand*)command; +- (void)stop:(CDVInvokedUrlCommand*)command; // UIImagePickerControllerDelegate methods - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info; diff --git a/src/ios/CDVCamera.m b/src/ios/CDVCamera.m index 72bbd03..c94b5ae 100644 --- a/src/ios/CDVCamera.m +++ b/src/ios/CDVCamera.m @@ -233,6 +233,26 @@ static NSString* MIME_JPEG = @"image/jpeg"; }); } +- (void)stop:(CDVInvokedUrlCommand*)command +{ + dispatch_async(dispatch_get_main_queue(), ^{ + UIViewController* presentedViewController = self.viewController.presentedViewController; + if (presentedViewController == nil) { + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + return; + } + + [presentedViewController dismissViewControllerAnimated:YES completion:^{ + self.hasPendingOperation = NO; + self.cdvUIImagePickerController = nil; + + CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; + }]; + }); +} + - (void)sendNoPermissionResult:(NSString*)callbackId { CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"has no access to camera"]; // error callback expects string ATM diff --git a/types/index.d.ts b/types/index.d.ts index 4fe7788..7f87241 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -17,6 +17,14 @@ interface Navigator { * This plugin provides an API for taking pictures and for choosing images from the system's image library. */ interface Camera { + /** + * Closes the active camera or photo library picker UI. + * @param onSuccess Success callback, that called when stop succeeds. + * @param onError Error callback, that get an error message. + */ + stop( + onSuccess: () => void, + onError: (message: string) => void): void; /** * Removes intermediate photos taken by the camera from temporary storage. * @param onSuccess Success callback, that called when cleanup succeeds. diff --git a/www/Camera.js b/www/Camera.js index d6ced51..3d340b0 100644 --- a/www/Camera.js +++ b/www/Camera.js @@ -147,6 +147,18 @@ cameraExport.getPicture = function (successCallback, errorCallback, options) { exec(successCallback, errorCallback, 'Camera', 'takePicture', args); }; +/** + * Closes the active camera or media picker UI. + * + * __Supported Platforms__ + * + * - Android + * - iOS + */ +cameraExport.stop = function (successCallback, errorCallback) { + exec(successCallback, errorCallback, 'Camera', 'stop', []); +}; + /** * Removes intermediate image files that are kept in temporary storage * after calling [`camera.getPicture`]{@link module:camera.getPicture}. Applies only when the value of