Skip to content

Commit

Permalink
Dialogs module stop working after calling scan function #13
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Apr 1, 2016
1 parent c0b5f24 commit 00bd36c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions barcodescanner.android.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
var barcodescanner = require("./barcodescanner-common");
var appModule = require("application");
var context = appModule.android.context;

var SCANNER_REQUEST_CODE = 444;
var CAMERA_PERMISSION_REQUEST_CODE = 555;

barcodescanner.rememberedContext = null;

barcodescanner._cameraPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
android.support.v4.content.ContextCompat.checkSelfPermission(appModule.android.foregroundActivity, android.Manifest.permission.CAMERA);
android.support.v4.content.ContextCompat.checkSelfPermission(appModule.android.currentContext, android.Manifest.permission.CAMERA);
}
return hasPermission;
};
Expand All @@ -25,7 +26,7 @@ barcodescanner.requestCameraPermission = function () {
if (!barcodescanner._cameraPermissionGranted()) {
// in a future version we could hook up the callback and change this flow a bit
android.support.v4.app.ActivityCompat.requestPermissions(
appModule.android.foregroundActivity,
appModule.android.currentContext,
[android.Manifest.permission.CAMERA],
CAMERA_PERMISSION_REQUEST_CODE);
// this is not the nicest solution as the user needs to initiate scanning again after granting permission,
Expand All @@ -48,7 +49,7 @@ barcodescanner.scan = function(arg) {
var intent = new android.content.Intent("com.google.zxing.client.android.SCAN");

// limit searching for a valid Intent to this package only
intent.setPackage(context.getPackageName());
intent.setPackage(appModule.android.context.getPackageName());

if (arg !== null) {
// shown at the bottom of the scan UI, default is: "Place a barcode inside the viewfinder rectangle to scan it."
Expand All @@ -66,6 +67,10 @@ barcodescanner.scan = function(arg) {
if (intent.resolveActivity(appModule.android.context.getPackageManager()) !== null) {
var previousResult = appModule.android.onActivityResult;
appModule.android.onActivityResult = function (requestCode, resultCode, data) {
if (barcodescanner.rememberedContext !== null) {
appModule.android.currentContext = barcodescanner.rememberedContext;
barcodescanner.rememberedContext = null;
}
appModule.android.onActivityResult = previousResult;
if (requestCode === SCANNER_REQUEST_CODE) {
if (resultCode === android.app.Activity.RESULT_OK) {
Expand All @@ -78,8 +83,10 @@ barcodescanner.scan = function(arg) {
}
}
};

appModule.android.foregroundActivity.startActivityForResult(intent, SCANNER_REQUEST_CODE);

// 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);
} else {
// this is next to impossible
reject("Configuration error");
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.2.1",
"version": "1.2.2",
"description": "Scan QR/barcodes with a {N} app.",
"main": "barcodescanner.js",
"nativescript": {
Expand Down

0 comments on commit 00bd36c

Please sign in to comment.