Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Check Android M permissions before open the CardIoActivity #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<engine name="cordova" version="&gt;=3.0.0" />
</engines>

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

<js-module src="www/cdv-plugin-card-io.js" name="CardIO">
<clobbers target="CardIO" />
Expand Down
57 changes: 47 additions & 10 deletions src/android/CardIOCordovaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@

package io.card.cordova.sdk;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

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

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.content.pm.PackageManager;

import io.card.payment.CardIOActivity;
import io.card.payment.CreditCard;

public class CardIOCordovaPlugin extends CordovaPlugin {

private String TAG = CardIOCordovaPlugin.class.getSimpleName();
private CallbackContext callbackContext;
private Activity activity = null;
private static final int REQUEST_CARD_SCAN = 10;

private String[] requiredPermissions = {Manifest.permission.CAMERA};

@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
Expand Down Expand Up @@ -78,12 +79,19 @@ private void scan(JSONArray args) throws JSONException {
}

private void canScan(JSONArray args) throws JSONException {
if (CardIOActivity.canReadCardWithCamera()) {
// This is where we return if scanning is enabled.
this.callbackContext.success("Card Scanning is enabled");

if (!hasPermisssion(requiredPermissions)) {
PermissionHelper.requestPermissions(this, 0, requiredPermissions);
this.callbackContext.error("Camera usage is not allowed");
} else {
this.callbackContext.error("Card Scanning is not enabled");
if (CardIOActivity.canReadCardWithCamera()) {
// This is where we return if scanning is enabled.
this.callbackContext.success("Card Scanning is enabled");
} else {
this.callbackContext.error("Card Scanning is not enabled");
}
}

}

// onActivityResult
Expand Down Expand Up @@ -136,4 +144,33 @@ private <T> T getConfiguration(JSONObject configurations, String name, T default
return defaultValue;
}
}

public boolean hasPermisssion(String[] requestedPermissions) {
for(String p : requestedPermissions) {
if(!PermissionHelper.hasPermission(this, p)){
return false;
}
}
return true;
}

public void onRequestPermissionResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {
PluginResult result;

if (callbackContext != null) {
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
callbackContext.sendPluginResult(result);
return;
}

}
result = new PluginResult(PluginResult.Status.OK);
callbackContext.sendPluginResult(result);
}
}


}