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

Commit

Permalink
#26 App crashes if the CordovaActivity is stopped during scanning
Browse files Browse the repository at this point in the history
- Moved `scan` to a background thread so it won't block the UI for about 50ms while initiating the scanner
  • Loading branch information
EddyVerbruggen committed Apr 22, 2016
1 parent 8dea274 commit d0bf407
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion 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="2.1.0">
version="2.1.1">

<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
46 changes: 26 additions & 20 deletions src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void requestCameraPermission() {
this.callbackContext.success();
}

public void scan(JSONObject obj) {
public void scan(final 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).
Expand All @@ -154,26 +154,32 @@ public void scan(JSONObject obj) {
return;
}

Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// avoid calling other apps using the same intent filter action
intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());
final CordovaPlugin that = this;

cordova.getThreadPool().execute(new Runnable() {
public void run() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
// avoid calling other apps using the same intent filter action
intentScan.setPackage(that.cordova.getActivity().getApplicationContext().getPackageName());

if (obj != null) {
intentScan.putExtra(Intents.Scan.CAMERA_ID, obj.optBoolean(PREFER_FRONTCAMERA, false) ? 1 : 0);
intentScan.putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, obj.optBoolean(SHOW_FLIP_CAMERA_BUTTON, false));
if (obj.has(FORMATS)) {
intentScan.putExtra(Intents.Scan.FORMATS, obj.optString(FORMATS));
}
if (obj.has(PROMPT)) {
intentScan.putExtra(Intents.Scan.PROMPT_MESSAGE, obj.optString(PROMPT));
}
if (obj.has(ORIENTATION)) {
intentScan.putExtra(Intents.Scan.ORIENTATION_LOCK, obj.optString(ORIENTATION));
}
}

if (obj != null) {
intentScan.putExtra(Intents.Scan.CAMERA_ID, obj.optBoolean(PREFER_FRONTCAMERA, false) ? 1 : 0);
intentScan.putExtra(Intents.Scan.SHOW_FLIP_CAMERA_BUTTON, obj.optBoolean(SHOW_FLIP_CAMERA_BUTTON, false));
if (obj.has(FORMATS)) {
intentScan.putExtra(Intents.Scan.FORMATS, obj.optString(FORMATS));
}
if (obj.has(PROMPT)) {
intentScan.putExtra(Intents.Scan.PROMPT_MESSAGE, obj.optString(PROMPT));
}
if (obj.has(ORIENTATION)) {
intentScan.putExtra(Intents.Scan.ORIENTATION_LOCK, obj.optString(ORIENTATION));
that.cordova.startActivityForResult(that, intentScan, REQUEST_CODE);
}
}

this.cordova.startActivityForResult(this, intentScan, REQUEST_CODE);
});
}

/**
Expand All @@ -186,7 +192,7 @@ public void scan(JSONObject obj) {
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (requestCode == REQUEST_CODE && this.callbackContext != null) {
if (resultCode == Activity.RESULT_OK) {
JSONObject obj = new JSONObject();
try {
Expand Down

0 comments on commit d0bf407

Please sign in to comment.