Skip to content

Commit

Permalink
Real availability check on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Sep 30, 2016
1 parent 8bdf79a commit 4848652
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ You'll notice that the Promise will no longer receive the result as there may be
```

### function: available
Note that the Android implementation will always return `true` at the moment.
Note that the iOS implementation will always return `true` at the moment,
on Android we actually check for a camera to be available.

```js
var barcodescanner = require("nativescript-barcodescanner");

Expand Down
6 changes: 0 additions & 6 deletions barcodescanner-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ var barcodescanner = {};

// a few default implementations because not all platforms provide them

barcodescanner.available = function () {
return new Promise(function (resolve) {
resolve(true);
});
};

barcodescanner.hasCameraPermission = function () {
return new Promise(function (resolve) {
resolve(true);
Expand Down
16 changes: 14 additions & 2 deletions barcodescanner.android.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var barcodescanner = require("./barcodescanner-common");
var appModule = require("application");
var camModule = require("camera");

var SCANNER_REQUEST_CODE = 444;
var CAMERA_PERMISSION_REQUEST_CODE = 555;
var broadcastManager;

barcodescanner.rememberedContext = null;


barcodescanner._cameraPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
Expand All @@ -17,6 +17,18 @@ barcodescanner._cameraPermissionGranted = function () {
return hasPermission;
};

barcodescanner.available = function () {
return new Promise(function (resolve, reject) {
try {
resolve(camModule.isAvailable());
} catch (ex) {
console.log("Error in barcodescanner.available: " + ex);
// let's just assume it's ok
resolve(true);
}
});
};

barcodescanner.hasCameraPermission = function () {
return new Promise(function (resolve) {
resolve(barcodescanner._cameraPermissionGranted());
Expand Down Expand Up @@ -135,7 +147,7 @@ barcodescanner.scan = function(arg) {
}
}
};

// we need to cache and restore the context, otherwise the dialogs module will be broken (and possibly other things as well)
barcodescanner.rememberedContext = appModule.android.currentContext;
appModule.android.currentContext.startActivityForResult(intent, SCANNER_REQUEST_CODE);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-barcodescanner",
"version": "1.4.0",
"version": "1.4.1",
"description": "Scan QR/barcodes with a {N} app.",
"main": "barcodescanner.js",
"nativescript": {
Expand Down

0 comments on commit 4848652

Please sign in to comment.