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

Commit

Permalink
added support for adding config parameter which is passed to barcode …
Browse files Browse the repository at this point in the history
…scanner activity
  • Loading branch information
Sean Nicholls authored and Ryan Willoughby committed Mar 31, 2015
1 parent 7b33022 commit 7773e4d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
android:windowSoftInputMode="stateAlwaysHidden"
android:exported="false">
<intent-filter>
<action android:name="com.phonegap.plugins.barcodescanner.SCAN"/>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

import android.util.Log;

/**
* This calls out to the ZXing barcode reader and returns the result.
*
Expand All @@ -35,7 +37,7 @@ public class BarcodeScanner extends CordovaPlugin {
private static final String TEXT = "text";
private static final String DATA = "data";
private static final String TYPE = "type";
private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN";
private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
private static final String ENCODE_DATA = "ENCODE_DATA";
private static final String ENCODE_TYPE = "ENCODE_TYPE";
private static final String ENCODE_INTENT = "com.phonegap.plugins.barcodescanner.ENCODE";
Expand Down Expand Up @@ -96,7 +98,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return true;
}
} else if (action.equals(SCAN)) {
scan();
scan(args);
} else {
return false;
}
Expand All @@ -106,9 +108,48 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan() {
public void scan(JSONArray args) {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);

// add config as intent extras
if(args.length() > 0) {

JSONObject obj;
JSONArray names;
String key;
Object value;

for(int i=0; i<args.length(); i++) {

try {
obj = args.getJSONObject(i);
} catch(JSONException e) {
Log.i("CordovaLog", e.getLocalizedMessage());
continue;
}

names = obj.names();
for(int j=0; j<names.length(); j++) {
try {
key = names.getString(j);
value = obj.get(key);

if(value instanceof Integer) {
intentScan.putExtra(key, (Integer)value);
} else if(value instanceof String) {
intentScan.putExtra(key, (String)value);
}

} catch(JSONException e) {
Log.i("CordovaLog", e.getLocalizedMessage());
continue;
}
}
}

}

// avoid calling other phonegap apps
intentScan.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());

Expand Down
15 changes: 13 additions & 2 deletions www/barcodescanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@
* }
* @param {Function} errorCallback
*/
BarcodeScanner.prototype.scan = function (successCallback, errorCallback) {
BarcodeScanner.prototype.scan = function (successCallback, errorCallback, config) {

if(config instanceof Array) {
// do nothing
} else {
if(typeof(config) === 'object') {
config = [ config ];
} else {
config = [];
}
}

if (errorCallback == null) {
errorCallback = function () {
};
Expand All @@ -86,7 +97,7 @@
return;
}

exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []);
exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', config);
};

//-------------------------------------------------------------------
Expand Down

0 comments on commit 7773e4d

Please sign in to comment.