CB-10120 android: Fix missing CAMERA permission for Android M

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.

Rebased by MatthewBooth and riknoll

This closes #142, closes #174
This commit is contained in:
ochakov 2015-12-01 16:10:43 -05:00 committed by Richard Knoll
parent 826aca3524
commit c12206ebc8

View File

@ -58,6 +58,9 @@ import android.provider.MediaStore;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.content.pm.PackageManager; 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, * 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 * 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 orientationCorrected; // Has the picture's orientation been corrected
private boolean allowEdit; // Should we allow the user to crop the image. 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; public CallbackContext callbackContext;
private int numPics; 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 scanMe; // Uri of image to be added to content store
private Uri croppedUri; private Uri croppedUri;
protected void getReadPermission(int requestCode)
{
cordova.requestPermission(this, requestCode, permissions[requestCode]);
}
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
@ -229,13 +237,40 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
* @param returnType Set the type of image to return. * @param returnType Set the type of image to return.
*/ */
public void callTakePicture(int returnType, int encodingType) { public void callTakePicture(int returnType, int encodingType) {
if (PermissionHelper.hasPermission(this, permissions[0])) { boolean takePicturePermission = cordova.hasPermission(permissions[TAKE_PIC_SEC]);
takePicture(returnType, encodingType);
} else { if (!takePicturePermission) {
PermissionHelper.requestPermission(this, TAKE_PIC_SEC, Manifest.permission.READ_EXTERNAL_STORAGE); 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 {
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) public void takePicture(int returnType, int encodingType)
{ {