Skip to content

Commit 99c996f

Browse files
author
Matt Kane
committed
Merge branch 'master' of https://github.com/roblabs/phonegap-plugins into roblabs-master
2 parents efadb79 + 862f5a9 commit 99c996f

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

Android/BarcodeScanner/BarcodeScanner.java

100644100755
Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,44 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
5757
this.callback = callbackId;
5858

5959
try {
60-
if (action.equals("scan")) {
60+
if (action.equals("encode")) {
61+
String type = null;
62+
if(args.length() > 0) {
63+
type = args.getString(0);
64+
}
65+
66+
String data = null;
67+
if(args.length() > 1) {
68+
data = args.getString(1);
69+
}
70+
71+
String installTitle = defaultInstallTitle;
72+
if(args.length() > 2) {
73+
installTitle = args.getString(2);
74+
}
75+
76+
String installMessage = defaultInstallMessage;
77+
if(args.length() > 3) {
78+
installMessage = args.getString(3);
79+
}
80+
81+
String yesString = defaultYesString;
82+
if(args.length() > 4) {
83+
yesString = args.getString(4);
84+
}
85+
86+
String noString = defaultNoString;
87+
if(args.length() > 5) {
88+
noString = args.getString(5);
89+
}
90+
91+
// if data.TypeOf() == Bundle, then call
92+
// encode(type, Bundle)
93+
// else
94+
// encode(type, String)
95+
this.encode(type, data, installTitle, installMessage, yesString, noString);
96+
}
97+
else if (action.equals("scan")) {
6198
String barcodeTypes = null;
6299
if(args.length() > 0) {
63100
barcodeTypes = args.getString(0);
@@ -179,4 +216,26 @@ public void onClick(DialogInterface dlg, int i) {
179216
};
180217
context.runOnUiThread(runnable);
181218
}
219+
220+
/**
221+
* Initiates a barcode encode. If the ZXing scanner isn't installed, the user
222+
* will be prompted to install it.
223+
* @param type The barcode type to encode
224+
* @param data The data to encode in the bar code
225+
* @param installTitle The title for the dialog box that prompts the user to install the scanner
226+
* @param installMessage The message prompting the user to install the barcode scanner
227+
* @param yesString The string "Yes" or localised equivalent
228+
* @param noString The string "No" or localised version
229+
*/
230+
public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) {
231+
Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE");
232+
intentEncode.putExtra("ENCODE_TYPE", type);
233+
intentEncode.putExtra("ENCODE_DATA", data);
234+
235+
try {
236+
this.ctx.startActivity(intentEncode);
237+
} catch (ActivityNotFoundException e) {
238+
showDownloadDialog(installTitle, installMessage, yesString, noString);
239+
}
240+
}
182241
}

Android/BarcodeScanner/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ If the user doesn't have the app install they will be promped to install it the
77

88
1. To install the plugin, move barcodescanner.js to your project's www folder and include a reference to it
99
in your html files.
10-
2. Create a folder called "beetight" within your project's src/com/ folder and move the java file into it.
10+
2. Create a folder called 'src/com/beetight/barcodescanner' within your project's src/com/ folder.
11+
3. And copy the java file into that new folder.
12+
13+
`mkdir <your_project>/src/com/beetight/barcodescanner`
14+
15+
`cp ./BarcodeScanner.java <your_project>/src/com/beetight/barcodescanner`
1116

1217
## Using the plugin ##
1318
The plugin creates the object `window.plugins.barcodeScanner` with one method `scan(types, success, fail, options)`
@@ -50,6 +55,25 @@ A full example could be:
5055
alert("Scanning failed: " + error);
5156
}, {yesString: "Install"}
5257
);
58+
59+
## Encoding a Barcode ##
60+
Supported encoding types:
61+
62+
* TEXT_TYPE
63+
* EMAIL_TYPE
64+
* PHONE_TYPE
65+
* SMS_TYPE
66+
67+
68+
A full example could be:
69+
70+
window.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) {
71+
alert("encode success: " + success);
72+
}, function(fail) {
73+
alert("encoding failed: " + fail);
74+
}, {yesString: "Install"}
75+
);
76+
5377

5478
## BUGS AND CONTRIBUTIONS ##
5579
The latest bleeding-edge version is available [on GitHub](http://github.com/ascorbic/phonegap-plugins/tree/master/Android/)

Android/BarcodeScanner/barcodescanner.js

100644100755
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ BarcodeScanner.Type = {
2828
ALL_CODE_TYPES: null
2929
}
3030

31+
BarcodeScanner.Encode = {
32+
TEXT_TYPE: "TEXT_TYPE",
33+
EMAIL_TYPE: "EMAIL_TYPE",
34+
PHONE_TYPE: "PHONE_TYPE",
35+
SMS_TYPE: "SMS_TYPE",
36+
// CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascriopt to Java
37+
// LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascriopt to Java
38+
}
39+
3140
BarcodeScanner.prototype.scan = function(types, success, fail, options) {
3241

3342
/* These are the strings used in the dialog that appears if ZXing isn't installed */
@@ -61,6 +70,38 @@ BarcodeScanner.prototype.scan = function(types, success, fail, options) {
6170
}, 'BarcodeScanner', 'scan', [types, installTitle, installMessage, yesString, noString]);
6271
};
6372

73+
BarcodeScanner.prototype.encode = function(type, data, success, fail, options) {
74+
75+
/* These are the strings used in the dialog that appears if ZXing isn't installed */
76+
var installTitle = "Install Barcode Scanner?";
77+
var installMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
78+
var yesString = "Yes";
79+
var noString = "No";
80+
if (typeof options != 'undefined') {
81+
if(typeof options.installTitle != 'undefined') {
82+
installTitle = options.installTitle;
83+
}
84+
85+
if(typeof options.installMessage != 'undefined') {
86+
installMessage = options.installMessage;
87+
}
88+
89+
if(typeof options.yesString != 'undefined') {
90+
yesString = options.yesString;
91+
}
92+
93+
if(typeof options.noString != 'undefined') {
94+
noString = options.noString;
95+
}
96+
}
97+
98+
return PhoneGap.exec(function(args) {
99+
success(args);
100+
}, function(args) {
101+
fail(args);
102+
}, 'BarcodeScanner', 'encode', [type, data, installTitle, installMessage, yesString, noString]);
103+
};
104+
64105
PhoneGap.addConstructor(function() {
65106
PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());
66107
PluginManager.addService("BarcodeScanner","com.beetight.barcodescanner.BarcodeScanner");

0 commit comments

Comments
 (0)