Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * PhoneGap is available under *either* the terms of the modified BSD license *or* the
- * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
- *
- * Copyright (c) Matt Kane 2010
- * Copyright (c) 2011, IBM Corporation
- */
- package com.phonegap.plugins.barcodescanner;
- import java.util.List;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- //import android.util.Log;
- import org.apache.cordova.api.Plugin;
- import org.apache.cordova.api.PluginResult;
- /**
- * This calls out to the ZXing barcode reader and returns the result.
- */
- public class BarcodeScanner extends Plugin {
- private static final String SCAN = "scan";
- private static final String ENCODE = "encode";
- private static final String CANCELLED = "cancelled";
- private static final String FORMAT = "format";
- 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.google.zxing.client.android.SCAN"; //"com.phonegap.plugins.barcodescanner.SCAN";
- private static final String SCAN_PACK = "com.google.zxing.client.android";
- private static final String ENCODE_DATA = "ENCODE_DATA";
- private static final String ENCODE_TYPE = "ENCODE_TYPE";
- private static final String ENCODE_INTENT = "com.google.zxing.client.android.ENCODE";//"com.phonegap.plugins.barcodescanner.ENCODE";
- private static final String TEXT_TYPE = "TEXT_TYPE";
- //private static final String EMAIL_TYPE = "EMAIL_TYPE";
- //private static final String PHONE_TYPE = "PHONE_TYPE";
- //private static final String SMS_TYPE = "SMS_TYPE";
- public static final int REQUEST_CODE = 0x0ba7c0de;
- public String callback;
- /**
- * Constructor.
- */
- public BarcodeScanner() {
- }
- /**
- * Executes the request and returns PluginResult.
- *
- * @param action The action to execute.
- * @param args JSONArray of arguments for the plugin.
- * @param callbackId The callback id used when calling back into JavaScript.
- * @return A PluginResult object with a status and message.
- */
- public PluginResult execute(String action, JSONArray args, String callbackId) {
- this.callback = callbackId;
- if (action.equals(ENCODE)) {
- JSONObject obj = args.optJSONObject(0);
- if (obj != null) {
- String type = obj.optString(TYPE);
- String data = obj.optString(DATA);
- // If the type is null then force the type to text
- if (type == null) {
- type = TEXT_TYPE;
- }
- if (data == null) {
- return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
- }
- //verify pack
- if( this.isIntentAvailable(this.cordova.getActivity().getApplicationContext(), ENCODE_INTENT) == false ){
- return new PluginResult(PluginResult.Status.ERROR, "BarcodeScanner App not available!, Please Install the app and try new.");
- } else {
- encode(type, data);
- }
- } else {
- return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
- }
- }
- else if (action.equals(SCAN)) {
- if( this.isIntentAvailable(this.cordova.getActivity().getApplicationContext(), SCAN_INTENT) == false ){
- return new PluginResult(PluginResult.Status.ERROR, "1-BarcodeScanner App not available!, Please Install the app and try new.");
- } else {
- scan();
- }
- } else {
- return new PluginResult(PluginResult.Status.INVALID_ACTION);
- }
- PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
- r.setKeepCallback(true);
- return r;
- }
- /**
- * Starts an intent to scan and decode a barcode.
- */
- public void scan() {
- Intent intentScan = new Intent(SCAN_INTENT);
- intentScan.setPackage(SCAN_PACK);
- //change mode for you requeriments
- intentScan.putExtra("SCAN_MODE", "PRODUCT_MODE");
- intentScan.addCategory(Intent.CATEGORY_DEFAULT);
- this.cordova.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
- }
- /**
- * Called when the barcode scanner intent completes
- *
- * @param requestCode The request code originally supplied to startActivityForResult(),
- * allowing you to identify who this result came from.
- * @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").
- */
- public void onActivityResult(int requestCode, int resultCode, Intent intent) {
- if (requestCode == REQUEST_CODE) {
- if (resultCode == Activity.RESULT_OK) {
- JSONObject obj = new JSONObject();
- try {
- obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
- obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
- obj.put(CANCELLED, false);
- } catch(JSONException e) {
- //Log.d(LOG_TAG, "This should never happen");
- }
- this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
- } if (resultCode == Activity.RESULT_CANCELED) {
- JSONObject obj = new JSONObject();
- try {
- obj.put(TEXT, "");
- obj.put(FORMAT, "");
- obj.put(CANCELLED, true);
- } catch(JSONException e) {
- //Log.d(LOG_TAG, "This should never happen");
- }
- this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
- } else {
- this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
- }
- }
- }
- /**
- * Initiates a barcode encode.
- * @param data The data to encode in the bar code
- * @param data2
- */
- public void encode(String type, String data) {
- Intent intentEncode = new Intent(ENCODE_INTENT);
- intentEncode.putExtra(ENCODE_TYPE, type);
- intentEncode.putExtra(ENCODE_DATA, data);
- this.cordova.getActivity().startActivity(intentEncode);
- }
- //http://android-developers.blogspot.mx/2009/01/can-i-use-this-intent.html
- /**
- * Indicates whether the specified action can be used as an intent. This
- * method queries the package manager for installed packages that can
- * respond to an intent with the specified action. If no suitable package is
- * found, this method returns false.
- *
- * @param context The application's environment.
- * @param action The Intent action to check for availability.
- *
- * @return True if an Intent with the specified action can be sent and
- * responded to, false otherwise.
- */
- private boolean isIntentAvailable(Context context, String action) {
- final PackageManager packageManager = context.getPackageManager();
- final Intent intent = new Intent(action);
- List<ResolveInfo> list =
- packageManager.queryIntentActivities(intent,
- PackageManager.MATCH_DEFAULT_ONLY);
- return list.size() > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement