1111import org .json .JSONArray ;
1212import org .json .JSONException ;
1313
14- import com .phonegap .DroidGap ;
15- import com .phonegap .api .PhonegapActivity ;
16- import com .phonegap .api .Plugin ;
17- import com .phonegap .api .PluginResult ;
18-
1914import android .app .Activity ;
2015import android .app .AlertDialog ;
16+ import android .content .ActivityNotFoundException ;
2117import android .content .DialogInterface ;
2218import android .content .Intent ;
23-
24- import android .content .ActivityNotFoundException ;
25-
2619import android .net .Uri ;
2720
21+ import com .phonegap .api .PhonegapActivity ;
22+ import com .phonegap .api .Plugin ;
23+ import com .phonegap .api .PluginResult ;
24+
2825/**
2926 * This calls out to the ZXing barcode reader and returns the result.
3027 */
3128public class BarcodeScanner extends Plugin {
3229 public static final int REQUEST_CODE = 0x0ba7c0de ;
3330
34-
31+
3532 public static final String defaultInstallTitle = "Install Barcode Scanner?" ;
3633 public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?" ;
3734 public static final String defaultYesString = "Yes" ;
3835 public static final String defaultNoString = "No" ;
39-
36+
4037 public String callback ;
41-
38+
4239 /**
4340 * Constructor.
4441 */
@@ -47,15 +44,15 @@ public BarcodeScanner() {
4744
4845 /**
4946 * Executes the request and returns PluginResult.
50- *
47+ *
5148 * @param action The action to execute.
5249 * @param args JSONArray of arguments for the plugin.
5350 * @param callbackId The callback id used when calling back into JavaScript.
5451 * @return A PluginResult object with a status and message.
5552 */
5653 public PluginResult execute (String action , JSONArray args , String callbackId ) {
5754 this .callback = callbackId ;
58-
55+
5956 try {
6057 if (action .equals ("encode" )) {
6158 String type = null ;
@@ -88,9 +85,9 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
8885 noString = args .getString (5 );
8986 }
9087
91- // if data.TypeOf() == Bundle, then call
88+ // if data.TypeOf() == Bundle, then call
9289 // encode(type, Bundle)
93- // else
90+ // else
9491 // encode(type, String)
9592 this .encode (type , data , installTitle , installMessage , yesString , noString );
9693 }
@@ -99,7 +96,7 @@ else if (action.equals("scan")) {
9996 if (args .length () > 0 ) {
10097 barcodeTypes = args .getString (0 );
10198 }
102-
99+
103100 String installTitle = defaultInstallTitle ;
104101 if (args .length () > 1 ) {
105102 installTitle = args .getString (1 );
@@ -131,11 +128,11 @@ else if (action.equals("scan")) {
131128 e .printStackTrace ();
132129 return new PluginResult (PluginResult .Status .JSON_EXCEPTION );
133130 }
134- }
135-
136-
131+ }
132+
133+
137134 /**
138- * Initiates a barcode scan. If the ZXing scanner isn't installed, the user
135+ * Initiates a barcode scan. If the ZXing scanner isn't installed, the user
139136 * will be prompted to install it.
140137 * @param types The barcode types to accept
141138 * @param installTitle The title for the dialog box that prompts the user to install the scanner
@@ -149,21 +146,21 @@ public void scan(String barcodeFormats, String installTitle, String installMessa
149146
150147 // A null format means we scan for any type
151148 if (barcodeFormats != null ) {
152- // Tell the scanner what types we're after
153- intentScan .putExtra ("SCAN_FORMATS" , barcodeFormats );
149+ // Tell the scanner what types we're after
150+ intentScan .putExtra ("SCAN_FORMATS" , barcodeFormats );
154151 }
155152
156- try {
157- this .ctx .startActivityForResult ((Plugin ) this , intentScan , REQUEST_CODE );
158- } catch (ActivityNotFoundException e ) {
153+ try {
154+ this .ctx .startActivityForResult ((Plugin ) this , intentScan , REQUEST_CODE );
155+ } catch (ActivityNotFoundException e ) {
159156 showDownloadDialog (installTitle , installMessage , yesString , noString );
160157 }
161158 }
162159
163160 /**
164- * Called when the barcode scanner exits
165- *
166- * @param requestCode The request code originally supplied to startActivityForResult(),
161+ * Called when the barcode scanner exits
162+ *
163+ * @param requestCode The request code originally supplied to startActivityForResult(),
167164 * allowing you to identify who this result came from.
168165 * @param resultCode The integer result code returned by the child activity through its setResult().
169166 * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
@@ -176,66 +173,66 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
176173 } else {
177174 this .error (new PluginResult (PluginResult .Status .ERROR ), this .callback );
178175 }
179- }
176+ }
180177 }
181-
182- private void showDownloadDialog (final String title , final String message , final String yesString , final String noString ) {
178+
179+ private void showDownloadDialog (final String title , final String message , final String yesString , final String noString ) {
183180 final PhonegapActivity context = this .ctx ;
184181 Runnable runnable = new Runnable () {
185- public void run () {
186-
187- AlertDialog .Builder dialog = new AlertDialog .Builder (context );
188- dialog .setTitle (title );
189- dialog .setMessage (message );
190- dialog .setPositiveButton (yesString , new DialogInterface .OnClickListener () {
191- public void onClick (DialogInterface dlg , int i ) {
192- dlg .dismiss ();
193- Intent intent = new Intent (Intent .ACTION_VIEW ,
194- Uri .parse ("market://search?q=pname:com.google.zxing.client.android" )
195- );
196- try {
197- context .startActivity (intent );
198- } catch (ActivityNotFoundException e ) {
199- // We don't have the market app installed, so download it directly.
200- Intent in = new Intent (Intent .ACTION_VIEW );
201- in .setData (Uri .parse ("http://zxing.googlecode.com/files/BarcodeScanner3.5.apk" ));
202- context .startActivity (in );
203-
204- }
182+ public void run () {
183+
184+ AlertDialog .Builder dialog = new AlertDialog .Builder (context );
185+ dialog .setTitle (title );
186+ dialog .setMessage (message );
187+ dialog .setPositiveButton (yesString , new DialogInterface .OnClickListener () {
188+ public void onClick (DialogInterface dlg , int i ) {
189+ dlg .dismiss ();
190+ Intent intent = new Intent (Intent .ACTION_VIEW ,
191+ Uri .parse ("market://search?q=pname:com.google.zxing.client.android" )
192+ );
193+ try {
194+ context .startActivity (intent );
195+ } catch (ActivityNotFoundException e ) {
196+ // We don't have the market app installed, so download it directly.
197+ Intent in = new Intent (Intent .ACTION_VIEW );
198+ in .setData (Uri .parse ("http://zxing.googlecode.com/files/BarcodeScanner3.53.apk" ));
199+ context .startActivity (in );
205200
206201 }
207- });
208- dialog .setNegativeButton (noString , new DialogInterface .OnClickListener () {
209- public void onClick (DialogInterface dlg , int i ) {
210- dlg .dismiss ();
211- }
212- });
213- dialog .create ();
214- dialog .show ();
215- }
216- };
217- context .runOnUiThread (runnable );
218- }
219202
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- }
203+ }
204+ });
205+ dialog .setNegativeButton (noString , new DialogInterface .OnClickListener () {
206+ public void onClick (DialogInterface dlg , int i ) {
207+ dlg .dismiss ();
208+ }
209+ });
210+ dialog .create ();
211+ dialog .show ();
212+ }
213+ };
214+ context .runOnUiThread (runnable );
215+ }
216+
217+ /**
218+ * Initiates a barcode encode. If the ZXing scanner isn't installed, the user
219+ * will be prompted to install it.
220+ * @param type The barcode type to encode
221+ * @param data The data to encode in the bar code
222+ * @param installTitle The title for the dialog box that prompts the user to install the scanner
223+ * @param installMessage The message prompting the user to install the barcode scanner
224+ * @param yesString The string "Yes" or localised equivalent
225+ * @param noString The string "No" or localised version
226+ */
227+ public void encode (String type , String data , String installTitle , String installMessage , String yesString , String noString ) {
228+ Intent intentEncode = new Intent ("com.google.zxing.client.android.ENCODE" );
229+ intentEncode .putExtra ("ENCODE_TYPE" , type );
230+ intentEncode .putExtra ("ENCODE_DATA" , data );
231+
232+ try {
233+ this .ctx .startActivity (intentEncode );
234+ } catch (ActivityNotFoundException e ) {
235+ showDownloadDialog (installTitle , installMessage , yesString , noString );
240236 }
237+ }
241238}
0 commit comments