diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 1bb6dac..f7ada36 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -58,6 +58,9 @@ import android.provider.MediaStore; import android.util.Base64; import android.util.Log; import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionInfo; + /** * This class launches the camera view, allows the user to take a picture, closes the camera view, * and returns the captured image. When the camera view is closed, the screen displayed before @@ -105,7 +108,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect private boolean orientationCorrected; // Has the picture's orientation been corrected private boolean allowEdit; // Should we allow the user to crop the image. - protected final static String[] permissions = { Manifest.permission.READ_EXTERNAL_STORAGE }; + protected final static String[] permissions = { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE }; public CallbackContext callbackContext; private int numPics; @@ -114,6 +117,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect private Uri scanMe; // Uri of image to be added to content store private Uri croppedUri; + protected void getReadPermission(int requestCode) + { + cordova.requestPermission(this, requestCode, permissions[requestCode]); + } + /** * Executes the request and returns PluginResult. * @@ -229,14 +237,41 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect * @param returnType Set the type of image to return. */ public void callTakePicture(int returnType, int encodingType) { - if (PermissionHelper.hasPermission(this, permissions[0])) { + boolean takePicturePermission = cordova.hasPermission(permissions[TAKE_PIC_SEC]); + + if (!takePicturePermission) { + takePicturePermission = true; // This permission is not required, unless we find android.permission.CAMERA in the package + try { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + String[] permissionsInPackage = packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions; + if (permissionsInPackage != null) { + for (String permission : permissionsInPackage) { + if (permission.equals(Manifest.permission.CAMERA)) { + takePicturePermission = false; + break; + } + } + } + } catch (NameNotFoundException e) { } + } + + boolean saveAlbumPermission = cordova.hasPermission(permissions[SAVE_TO_ALBUM_SEC]); + if (takePicturePermission && saveAlbumPermission) { takePicture(returnType, encodingType); } else { - PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE); + if (saveAlbumPermission && !takePicturePermission) { + cordova.requestPermission(this, TAKE_PIC_SEC, permissions[TAKE_PIC_SEC]); + } + else if (!saveAlbumPermission && takePicturePermission) { + cordova.requestPermission(this, TAKE_PIC_SEC, permissions[SAVE_TO_ALBUM_SEC]); + } + else + { + cordova.requestPermissions(this, TAKE_PIC_SEC, permissions); + } } } - public void takePicture(int returnType, int encodingType) { // Save the number of images currently on disk for later