Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
#15 Android M (6) compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Oct 27, 2015
1 parent 64845f4 commit f028ec4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 18 deletions.
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ And on Android you can also pass in these:


A full example could be:
```
```js
cordova.plugins.barcodeScanner.scan(
function (result) {
alert("We got a barcode\n" +
Expand All @@ -82,6 +82,36 @@ A full example could be:
);
```

## Checking permission
On Android 6 you need to request permission to use the camera at runtime when targeting API level 23+.
Even if the `uses-permission` tag for the Camera is present in `AndroidManifest.xml`.

Note that `hasCameraPermission` will return true when:
* You're running this on iOS, or
* You're targeting an API level lower than 23, or
* You're using Android < 6, or
* You've already granted permission.

```js
function hasCameraPermission() {
cordova.plugins.barcodeScanner.hasCameraPermission(
function(result) {
// if this is 'false' you probably want to call 'requestCameraPermission' now
alert(result);
}
)
}

function requestCameraPermission() {
// no callbacks required as this opens a popup which returns async
cordova.plugins.barcodeScanner.requestCameraPermission();
}
```

Note that backward compatibility was added by checking for permission in the `scan` function.
If permission is needed the `scan` method will now show the permission request popup.
The user will then need to allow camera access and launch the scanner again.

## Encoding a Barcode ##
The plugin creates the object `cordova.plugins.barcodeScanner` with the method `encode(type, data, success, fail)`.
Supported encoding types:
Expand All @@ -91,9 +121,8 @@ Supported encoding types:
* PHONE_TYPE
* SMS_TYPE

```
A full example could be:
```JS
cordova.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
alert("encode success: " + success);
}, function(fail) {
Expand Down
13 changes: 3 additions & 10 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.phonegap.plugins.barcodescanner"
version="1.2.12">
version="1.3.0">

<name>BarcodeScanner</name>
<description>You can use the BarcodeScanner plugin to scan different types of barcodes (using the device's camera) and get the metadata encoded in them for processing within your application.</description>
Expand Down Expand Up @@ -61,15 +61,6 @@
<platform name="android">

<source-file src="src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java" target-dir="src/com/phonegap/plugins/barcodescanner" />
<!--
<source-file src="R.java" target-dir="src/com/google/zxing/client/android" />
-->

<!--
<config-file target="res/xml/plugins.xml" parent="/plugins">
<plugin name="BarcodeScanner" value="com.phonegap.plugins.barcodescanner.BarcodeScanner"/>
</config-file>
-->

<config-file target="res/xml/config.xml" parent="/*">
<feature name="BarcodeScanner">
Expand Down Expand Up @@ -302,6 +293,8 @@
<string name="wifi_type_label">Type</string>
</config-file>

<framework src="com.android.support:support-v4:+" />

<framework src="src/android/ignorelinterrors.gradle" custom="true" type="gradleReference"/>

</platform>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
*/
package com.phonegap.plugins.barcodescanner;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.android.Intents;

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.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

/**
* This calls out to the ZXing barcode reader and returns the result.
Expand All @@ -30,6 +36,8 @@
public class BarcodeScanner extends CordovaPlugin {
public static final int REQUEST_CODE = 0x0ba7c0de;

private static final String REQUEST_CAMERA_PERMISSION = "requestCameraPermission";
private static final String HAS_CAMERA_PERMISSION = "hasCameraPermission";
private static final String SCAN = "scan";
private static final String ENCODE = "encode";
private static final String CANCELLED = "cancelled";
Expand Down Expand Up @@ -103,16 +111,49 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
}
} else if (action.equals(SCAN)) {
scan(args.optJSONObject(0));
} else if (action.equals(HAS_CAMERA_PERMISSION)) {
hasCameraPermission();
} else if (action.equals(REQUEST_CAMERA_PERMISSION)) {
requestCameraPermission();
} else {
return false;
}
return true;
}

/**
* Starts an intent to scan and decode a barcode.
*/
public void hasCameraPermission() {
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, cameraPermissionGranted()));
}

private boolean cameraPermissionGranted() {
boolean hasPermission = Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
hasPermission = PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this.cordova.getActivity(), Manifest.permission.CAMERA);
}
return hasPermission;
}

public void requestCameraPermission() {
if (!cameraPermissionGranted()) {
ActivityCompat.requestPermissions(
this.cordova.getActivity(),
new String[]{Manifest.permission.CAMERA},
444);
}
// this method executes async and we seem to have no known way to receive the result, so simply returning ok now
this.callbackContext.success();
}

public void scan(JSONObject obj) {

// note that if the dev didn't call requestCameraPermission before scan and cameraPermissionGranted returns false,
// then the app will ask permission and the scan method needs to be invoked again (done for backward compat).
if (!cameraPermissionGranted()) {
requestCameraPermission();
this.callbackContext.error("Please allow camera access and try again.");
return;
}

Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// avoid calling other apps using the same intent filter action
Expand Down
8 changes: 8 additions & 0 deletions www/barcodescanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
// CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascript to Java
// LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascript to Java
};
}

BarcodeScanner.prototype.hasCameraPermission = function (callback) {
exec(callback, null, 'BarcodeScanner', 'hasCameraPermission', []);
};

BarcodeScanner.prototype.requestCameraPermission = function (callback) {
exec(callback, null, 'BarcodeScanner', 'requestCameraPermission', []);
};

/**
Expand Down

0 comments on commit f028ec4

Please sign in to comment.