From 91dd693623ae17c84d5d8e3e27139247b2b59690 Mon Sep 17 00:00:00 2001 From: ochakov Date: Tue, 1 Dec 2015 16:10:43 -0500 Subject: [PATCH 1/3] Fix missing CAMERA permission for Android M Fixing a bug where for Android M new permissions model, only external storage permission was asked, while camera permission was not, causing camera to fail on Android 6.0 --- .idea/workspace.xml | 215 ++++++++++++++++++++++++++++++++ src/android/CameraLauncher.java | 13 +- 2 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 000000000..f3117e838 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,215 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1455901347287 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index 15e908fbe..cc90b3999 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -105,7 +105,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 +114,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. * @@ -172,8 +177,8 @@ else if ((this.srcType == PHOTOLIBRARY) || (this.srcType == SAVEDPHOTOALBUM)) { // preserve the original exif data and filename in the modified file that is // created if(this.mediaType == PICTURE && (this.destType == FILE_URI || this.destType == NATIVE_URI) - && fileWillBeModified() && !PermissionHelper.hasPermission(this, permissions[0])) { - PermissionHelper.requestPermission(this, SAVE_TO_ALBUM_SEC, Manifest.permission.READ_EXTERNAL_STORAGE); + && fileWillBeModified() && !cordova.hasPermission(permissions[SAVE_TO_ALBUM_SEC])) { + getReadPermission(SAVE_TO_ALBUM_SEC); } else { this.getImage(this.srcType, destType, encodingType); } @@ -232,7 +237,7 @@ private String getTempDirectoryPath() { * @param returnType Set the type of image to return. */ public void callTakePicture(int returnType, int encodingType) { - if (PermissionHelper.hasPermission(this, permissions[0])) { + if (cordova.hasPermission(this, permissions[TAKE_PIC_SEC])) { takePicture(returnType, encodingType); } else { PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE); From 70fcf30423946ec9d3299be1495ddd44cacff3c1 Mon Sep 17 00:00:00 2001 From: ochakov Date: Tue, 1 Dec 2015 16:34:08 -0500 Subject: [PATCH 2/3] Fix requiring both Camera and Storage permissions To take a picture on Android M, both Camera and Storage access permissions are required. Now asking for both permissions simultaneously. --- .idea/workspace.xml | 8 ++++---- src/android/CameraLauncher.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f3117e838..5beeaf684 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -27,8 +27,8 @@ - - + + @@ -205,8 +205,8 @@ - - + + diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index cc90b3999..cb80a482a 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -237,10 +237,10 @@ private String getTempDirectoryPath() { * @param returnType Set the type of image to return. */ public void callTakePicture(int returnType, int encodingType) { - if (cordova.hasPermission(this, permissions[TAKE_PIC_SEC])) { + if (cordova.hasPermission(permissions[TAKE_PIC_SEC]) && cordova.hasPermission(permissions[SAVE_TO_ALBUM_SEC])) { takePicture(returnType, encodingType); } else { - PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE); + cordova.requestPermissions(this, TAKE_PIC_SEC, permissions); } } From c4618a894eec92349371221fe18b5572151bff6f Mon Sep 17 00:00:00 2001 From: ochakov Date: Wed, 2 Dec 2015 18:51:22 -0500 Subject: [PATCH 3/3] Check android.permission.CAMERA in the package According to the PR conversation, when android.permission.CAMERA is not set in the package, there is no need to ask for the camera permission. Also, checking now camera and storage permissions separately, so if only one of them is missing, the other one will be requested and not both. --- .idea/workspace.xml | 215 -------------------------------- src/android/CameraLauncher.java | 36 +++++- 2 files changed, 33 insertions(+), 218 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 5beeaf684..000000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,215 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1455901347287 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java index cb80a482a..d4ef3b69d 100644 --- a/src/android/CameraLauncher.java +++ b/src/android/CameraLauncher.java @@ -58,6 +58,9 @@ Licensed to the Apache Software Foundation (ASF) under one 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 @@ -237,14 +240,41 @@ private String getTempDirectoryPath() { * @param returnType Set the type of image to return. */ public void callTakePicture(int returnType, int encodingType) { - if (cordova.hasPermission(permissions[TAKE_PIC_SEC]) && cordova.hasPermission(permissions[SAVE_TO_ALBUM_SEC])) { + 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 { - cordova.requestPermissions(this, TAKE_PIC_SEC, permissions); + 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