Skip to content

Commit

Permalink
Merge pull request #101 from geneanet/add-permissions-camera
Browse files Browse the repository at this point in the history
Add permissions camera for android 6+
  • Loading branch information
ChristopheBoucaut authored Feb 1, 2017
2 parents fd89979 + 5022228 commit 35fe833
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 22 deletions.
1 change: 1 addition & 0 deletions doc/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ An `options` object containing the parameters of the camera.
+ **Note :** Contains the error code.
- **Code "2" :** Error while taking a picture.
- **Code "3" :** Camera closed before takin a picture.
- **Code "4" :** Permissions denied.
- **message :**
+ **Type :** `string`
+ **Note :** A error message.
Expand Down
1 change: 1 addition & 0 deletions doc/fr/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ La fonction `onFail` est appelée lorsque la prise de vue a échouée.
+ **Note :** Contient le code d'erreur.
- **Code "2" :** Erreur lors de l'exécution de l'appareil photo.
- **Code "3" :** L'utilisateur a fermé l'appareil photo sans prendre de photo.
- **Code "4" :** L'utilisateur a refusé une permission.
- **message :**
+ **Type :** `string`
+ **Note :** Contient un message détaillant l'erreur.
Expand Down
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<clobbers target="navigator.GeneanetCustomCamera" />
</js-module>

<dependency id="cordova-plugin-compat" version="^1.1.0" />

<hook type="after_plugin_install" src="hooks/after_plugin_install/npminstall.js" />

<platform name="android">
Expand Down
90 changes: 68 additions & 22 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@
import XXX_NAME_CURRENT_PACKAGE_XXX.CameraActivity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.Manifest;
import android.util.Base64;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PermissionHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CameraLauncher extends CordovaPlugin {

protected CallbackContext callbackContext;
protected Intent intent;

protected static final int RESULT_SUCCESS = 1;
protected static final int RESULT_ERROR = 2;
protected static final int RESULT_BACK = 3;
protected static final int PERMISSION_DENIED_ERROR = 4;

protected static final int REQUEST_CODE = 88224646;
protected static final int ALL_PERMISSIONS = 0;
protected final static String[] permissions = { Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE };

/**
* Execute the plugin.
*
*
* @param action Action executed.
* @param args Parameters for the plugin.
* @param callbackContext Context of callback.
Expand All @@ -35,8 +42,7 @@ public boolean execute(String action, JSONArray args,
if (action.equals("startCamera")) {
this.callbackContext = callbackContext;

Intent intent = new Intent(this.cordova.getActivity(),
CameraActivity.class);
this.intent = new Intent(this.cordova.getActivity(), CameraActivity.class);

if (args.getString(0) != "null") {
byte[] imgBackgroundBase64;
Expand Down Expand Up @@ -70,29 +76,36 @@ public boolean execute(String action, JSONArray args,

// If we don't have a background image, disable miniature and opacity options.
if (args.getString(0) == "null") {
intent.putExtra("miniature", false);
intent.putExtra("opacity", false);
this.intent.putExtra("miniature", false);
this.intent.putExtra("opacity", false);
} else {
intent.putExtra("miniature", args.getBoolean(2));
intent.putExtra("opacity", args.getBoolean(7));
this.intent.putExtra("miniature", args.getBoolean(2));
this.intent.putExtra("opacity", args.getBoolean(7));
}
intent.putExtra("saveInGallery", args.getBoolean(3));
intent.putExtra("cameraBackgroundColor", args.getString(4));
intent.putExtra("cameraBackgroundColorPressed", args.getString(5));

this.intent.putExtra("saveInGallery", args.getBoolean(3));
this.intent.putExtra("cameraBackgroundColor", args.getString(4));
this.intent.putExtra("cameraBackgroundColorPressed", args.getString(5));
if (args.getInt(6) >= 0 && args.getInt(6) <= 100) {
intent.putExtra("quality", args.getInt(6));
this.intent.putExtra("quality", args.getInt(6));
}
intent.putExtra("startOrientation", this.cordova.getActivity().getResources().getConfiguration().orientation);
this.intent.putExtra("startOrientation", this.cordova.getActivity().getResources().getConfiguration().orientation);

intent.putExtra("defaultFlash", args.getInt(8));
intent.putExtra("switchFlash", args.getBoolean(9));
this.intent.putExtra("defaultFlash", args.getInt(8));
this.intent.putExtra("switchFlash", args.getBoolean(9));

intent.putExtra("defaultCamera", args.getInt(10));
intent.putExtra("switchCamera", args.getBoolean(11));
this.intent.putExtra("defaultCamera", args.getInt(10));
this.intent.putExtra("switchCamera", args.getBoolean(11));

cordova.startActivityForResult((CordovaPlugin) this, intent,
CameraLauncher.REQUEST_CODE);
boolean takePicturePermission = PermissionHelper.hasPermission(this, Manifest.permission.CAMERA);
boolean saveAlbumPermission = PermissionHelper.hasPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (!takePicturePermission || !saveAlbumPermission) {
// Request permissions if we don't have already.
PermissionHelper.requestPermissions(this, ALL_PERMISSIONS, permissions);
} else {
// Else, start the capture.
this.startCapture();
}

return true;
}
Expand Down Expand Up @@ -140,11 +153,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
}

/**
* Generate error in the plugin.
*
* Generate error in the plugin.
*
* @param code Error code.
* @param message Error message.
*
*
* @return a JSON Object.
*/
protected JSONObject generateError(int code, String message) {
Expand All @@ -158,4 +171,37 @@ protected JSONObject generateError(int code, String message) {

return resultForPlugin;
}

/**
* Just start the capture.
*/
protected void startCapture() {
cordova.startActivityForResult((CordovaPlugin) this, this.intent, CameraLauncher.REQUEST_CODE);
}

/**
* Callback to request a permission.
*
* @param int requestCode
* @param String[] permissions
* @param int[] grantResults
*
* @throws JSONException
*/
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
// Permissions denied ? We stop the capture.
this.callbackContext.error(generateError(CameraLauncher.PERMISSION_DENIED_ERROR, intent.getStringExtra("Permissions refused")));
return;
}
}

switch (requestCode) {
case ALL_PERMISSIONS:
// All permissions granted, start the capture.
this.startCapture();
break;
}
}
}

0 comments on commit 35fe833

Please sign in to comment.