Skip to content

Commit

Permalink
Removed Whitespaces, Removed uneeded imports, fixed the webdownload link
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Widmer committed Feb 10, 2011
1 parent 93596b8 commit 11b1bfa
Showing 1 changed file with 80 additions and 83 deletions.
163 changes: 80 additions & 83 deletions Android/BarcodeScanner/BarcodeScanner.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,34 +11,31 @@
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;


import com.phonegap.DroidGap;
import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;

import android.content.ActivityNotFoundException;

import android.net.Uri; import android.net.Uri;


import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

/** /**
* This calls out to the ZXing barcode reader and returns the result. * This calls out to the ZXing barcode reader and returns the result.
*/ */
public class BarcodeScanner extends Plugin { public class BarcodeScanner extends Plugin {
public static final int REQUEST_CODE = 0x0ba7c0de; public static final int REQUEST_CODE = 0x0ba7c0de;



public static final String defaultInstallTitle = "Install Barcode Scanner?"; public static final String defaultInstallTitle = "Install Barcode Scanner?";
public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?"; public static final String defaultInstallMessage = "This requires the free Barcode Scanner app. Would you like to install it now?";
public static final String defaultYesString = "Yes"; public static final String defaultYesString = "Yes";
public static final String defaultNoString = "No"; public static final String defaultNoString = "No";

public String callback; public String callback;

/** /**
* Constructor. * Constructor.
*/ */
Expand All @@ -47,15 +44,15 @@ public BarcodeScanner() {


/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArray of arguments for the plugin. * @param args JSONArray of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public PluginResult execute(String action, JSONArray args, String callbackId) { public PluginResult execute(String action, JSONArray args, String callbackId) {
this.callback = callbackId; this.callback = callbackId;

try { try {
if (action.equals("encode")) { if (action.equals("encode")) {
String type = null; String type = null;
Expand Down Expand Up @@ -88,9 +85,9 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
noString = args.getString(5); noString = args.getString(5);
} }


// if data.TypeOf() == Bundle, then call // if data.TypeOf() == Bundle, then call
// encode(type, Bundle) // encode(type, Bundle)
// else // else
// encode(type, String) // encode(type, String)
this.encode(type, data, installTitle, installMessage, yesString, noString); this.encode(type, data, installTitle, installMessage, yesString, noString);
} }
Expand All @@ -99,7 +96,7 @@ else if (action.equals("scan")) {
if(args.length() > 0) { if(args.length() > 0) {
barcodeTypes = args.getString(0); barcodeTypes = args.getString(0);
} }

String installTitle = defaultInstallTitle; String installTitle = defaultInstallTitle;
if(args.length() > 1) { if(args.length() > 1) {
installTitle = args.getString(1); installTitle = args.getString(1);
Expand Down Expand Up @@ -131,11 +128,11 @@ else if (action.equals("scan")) {
e.printStackTrace(); e.printStackTrace();
return new PluginResult(PluginResult.Status.JSON_EXCEPTION); return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
} }
} }


/** /**
* Initiates a barcode scan. If the ZXing scanner isn't installed, the user * Initiates a barcode scan. If the ZXing scanner isn't installed, the user
* will be prompted to install it. * will be prompted to install it.
* @param types The barcode types to accept * @param types The barcode types to accept
* @param installTitle The title for the dialog box that prompts the user to install the scanner * @param installTitle The title for the dialog box that prompts the user to install the scanner
Expand All @@ -149,21 +146,21 @@ public void scan(String barcodeFormats, String installTitle, String installMessa


// A null format means we scan for any type // A null format means we scan for any type
if (barcodeFormats != null) { if (barcodeFormats != null) {
// Tell the scanner what types we're after // Tell the scanner what types we're after
intentScan.putExtra("SCAN_FORMATS", barcodeFormats); intentScan.putExtra("SCAN_FORMATS", barcodeFormats);
} }


try { try {
this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE); this.ctx.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
showDownloadDialog(installTitle, installMessage, yesString, noString); showDownloadDialog(installTitle, installMessage, yesString, noString);
} }
} }


/** /**
* Called when the barcode scanner exits * Called when the barcode scanner exits
* *
* @param requestCode The request code originally supplied to startActivityForResult(), * @param requestCode The request code originally supplied to startActivityForResult(),
* allowing you to identify who this result came from. * allowing you to identify who this result came from.
* @param resultCode The integer result code returned by the child activity through its setResult(). * @param resultCode The integer result code returned by the child activity through its setResult().
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
Expand All @@ -176,66 +173,66 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
} else { } else {
this.error(new PluginResult(PluginResult.Status.ERROR), this.callback); this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
} }
} }
} }

private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) { private void showDownloadDialog(final String title, final String message, final String yesString, final String noString) {
final PhonegapActivity context = this.ctx; final PhonegapActivity context = this.ctx;
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
public void run() { public void run() {


AlertDialog.Builder dialog = new AlertDialog.Builder(context); AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(title); dialog.setTitle(title);
dialog.setMessage(message); dialog.setMessage(message);
dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() { dialog.setPositiveButton(yesString, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int i) { public void onClick(DialogInterface dlg, int i) {
dlg.dismiss(); dlg.dismiss();
Intent intent = new Intent(Intent.ACTION_VIEW, Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:com.google.zxing.client.android") Uri.parse("market://search?q=pname:com.google.zxing.client.android")
); );
try { try {
context.startActivity(intent); context.startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
// We don't have the market app installed, so download it directly. // We don't have the market app installed, so download it directly.
Intent in = new Intent(Intent.ACTION_VIEW); Intent in = new Intent(Intent.ACTION_VIEW);
in.setData(Uri.parse("http://zxing.googlecode.com/files/BarcodeScanner3.5.apk")); in.setData(Uri.parse("http://zxing.googlecode.com/files/BarcodeScanner3.53.apk"));
context.startActivity(in); context.startActivity(in);

}


} }
});
dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int i) {
dlg.dismiss();
}
});
dialog.create();
dialog.show();
}
};
context.runOnUiThread(runnable);
}


/** }
* Initiates a barcode encode. If the ZXing scanner isn't installed, the user });
* will be prompted to install it. dialog.setNegativeButton(noString, new DialogInterface.OnClickListener() {
* @param type The barcode type to encode public void onClick(DialogInterface dlg, int i) {
* @param data The data to encode in the bar code dlg.dismiss();
* @param installTitle The title for the dialog box that prompts the user to install the scanner }
* @param installMessage The message prompting the user to install the barcode scanner });
* @param yesString The string "Yes" or localised equivalent dialog.create();
* @param noString The string "No" or localised version dialog.show();
*/ }
public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) { };
Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE"); context.runOnUiThread(runnable);
intentEncode.putExtra("ENCODE_TYPE", type); }
intentEncode.putExtra("ENCODE_DATA", data);

/**
try { * Initiates a barcode encode. If the ZXing scanner isn't installed, the user
this.ctx.startActivity(intentEncode); * will be prompted to install it.
} catch (ActivityNotFoundException e) { * @param type The barcode type to encode
showDownloadDialog(installTitle, installMessage, yesString, noString); * @param data The data to encode in the bar code
} * @param installTitle The title for the dialog box that prompts the user to install the scanner
* @param installMessage The message prompting the user to install the barcode scanner
* @param yesString The string "Yes" or localised equivalent
* @param noString The string "No" or localised version
*/
public void encode(String type, String data, String installTitle, String installMessage, String yesString, String noString) {
Intent intentEncode = new Intent("com.google.zxing.client.android.ENCODE");
intentEncode.putExtra("ENCODE_TYPE", type);
intentEncode.putExtra("ENCODE_DATA", data);

try {
this.ctx.startActivity(intentEncode);
} catch (ActivityNotFoundException e) {
showDownloadDialog(installTitle, installMessage, yesString, noString);
} }
}
} }

0 comments on commit 11b1bfa

Please sign in to comment.