package com.AB.ABZxing;
import android.app.Activity;
import android.app.AlertDialog;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Events;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import anywheresoftware.b4a.IOnActivityResult;
@ShortName("ABBarcode")
@Events(values={"BarcodeFound (barCode as String, formatName as string)", "Canceled()"})
@Version(1.0f)
@Author("Alain Bailleul")
public class ABZxing implements IOnActivityResult {
private BA _ba=null;
private String _eventName="";
private static IOnActivityResult ion;
private static final String DEFAULT_TITLE = "Install Barcode Scanner?";
private static final String DEFAULT_MESSAGE = "This application requires Barcode Scanner. Would you like to install it?";
private static final String DEFAULT_YES = "Yes";
private static final String DEFAULT_NO = "No";
private static final int REQUEST_CODE = 0x0ba7c0de;
public void ABGetBarcode(final BA ba, String eventName, String mode) {
_ba=ba;
_eventName=eventName;
Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
if (mode != null) {
intentScan.putExtra("SCAN_FORMATS", mode);
}
[B][COLOR="Red"] ion = new IOnActivityResult();[/COLOR][/B]
try {
[COLOR="SeaGreen"][B]// needs to be: ba.startActivityForResult(ion, intentScan, REQUEST_CODE);
// to start the scanner and requesting an answer to ResultArrived[/B][/COLOR]
ba.startActivityForResult(ion, intentScan);
} catch (ActivityNotFoundException e) {
showDownloadDialog(ba.activity, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
}
}
@SuppressWarnings("unchecked")
@Override
public void ResultArrived(int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_CANCELED) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
final String s = _eventName.toLowerCase(BA.cul) + "_barcodefound";
_ba.raiseEvent(this, s, contents, formatName);
} else {
final String s = _eventName.toLowerCase(BA.cul) + "_barcodefound";
_ba.raiseEvent(this, s, "", "");
}
}
else {
final String s = _eventName.toLowerCase(BA.cul) + "_canceled";
_ba.raiseEvent(this, s);
}
}
private static AlertDialog showDownloadDialog(final Activity activity,
CharSequence stringTitle,
CharSequence stringMessage,
CharSequence stringButtonYes,
CharSequence stringButtonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
downloadDialog.setTitle(stringTitle);
downloadDialog.setMessage(stringMessage);
downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);
}
});
downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {}
});
return downloadDialog.show();
}
}