Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * PhoneGap is available under *either* the terms of the modified BSD license *or* the
  3.  * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
  4.  *
  5.  * Copyright (c) Matt Kane 2010
  6.  * Copyright (c) 2011, IBM Corporation
  7.  */
  8.  
  9. package com.phonegap.plugins.barcodescanner;
  10.  
  11. import java.util.List;
  12.  
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16.  
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.content.pm.PackageManager;
  21. import android.content.pm.ResolveInfo;
  22. //import android.util.Log;
  23.  
  24. import org.apache.cordova.api.Plugin;
  25. import org.apache.cordova.api.PluginResult;
  26.  
  27. /**
  28.  * This calls out to the ZXing barcode reader and returns the result.
  29.  */
  30. public class BarcodeScanner extends Plugin {
  31.     private static final String SCAN        = "scan";
  32.     private static final String ENCODE      = "encode";
  33.     private static final String CANCELLED   = "cancelled";
  34.     private static final String FORMAT      = "format";
  35.     private static final String TEXT        = "text";
  36.     private static final String DATA        = "data";
  37.     private static final String TYPE        = "type";
  38.     private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN"; //"com.phonegap.plugins.barcodescanner.SCAN";
  39.     private static final String SCAN_PACK   = "com.google.zxing.client.android";
  40.     private static final String ENCODE_DATA = "ENCODE_DATA";
  41.     private static final String ENCODE_TYPE = "ENCODE_TYPE";
  42.     private static final String ENCODE_INTENT = "com.google.zxing.client.android.ENCODE";//"com.phonegap.plugins.barcodescanner.ENCODE";
  43.     private static final String TEXT_TYPE   = "TEXT_TYPE";
  44.    
  45.     //private static final String EMAIL_TYPE    = "EMAIL_TYPE";
  46.     //private static final String PHONE_TYPE    = "PHONE_TYPE";
  47.     //private static final String SMS_TYPE  = "SMS_TYPE";
  48.  
  49.     public static final int REQUEST_CODE = 0x0ba7c0de;
  50.  
  51.     public String callback;
  52.  
  53.     /**
  54.      * Constructor.
  55.      */
  56.     public BarcodeScanner() {
  57.     }
  58.  
  59.     /**
  60.      * Executes the request and returns PluginResult.
  61.      *
  62.      * @param action        The action to execute.
  63.      * @param args          JSONArray of arguments for the plugin.
  64.      * @param callbackId    The callback id used when calling back into JavaScript.
  65.      * @return              A PluginResult object with a status and message.
  66.      */
  67.     public PluginResult execute(String action, JSONArray args, String callbackId) {
  68.         this.callback = callbackId;
  69.  
  70.         if (action.equals(ENCODE)) {
  71.             JSONObject obj = args.optJSONObject(0);
  72.             if (obj != null) {
  73.                 String type = obj.optString(TYPE);
  74.                 String data = obj.optString(DATA);
  75.  
  76.                 // If the type is null then force the type to text
  77.                 if (type == null) {
  78.                     type = TEXT_TYPE;
  79.                 }
  80.  
  81.                 if (data == null) {
  82.                     return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
  83.                 }
  84.                 //verify pack
  85.                 if( this.isIntentAvailable(this.cordova.getActivity().getApplicationContext(), ENCODE_INTENT) == false ){
  86.                     return new PluginResult(PluginResult.Status.ERROR, "BarcodeScanner App not available!, Please Install the app and try new.");
  87.                 } else {
  88.                     encode(type, data);
  89.                 }
  90.             } else {
  91.                 return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode");
  92.             }
  93.         }
  94.         else if (action.equals(SCAN)) {
  95.             if( this.isIntentAvailable(this.cordova.getActivity().getApplicationContext(), SCAN_INTENT) == false ){
  96.                 return new PluginResult(PluginResult.Status.ERROR, "1-BarcodeScanner App not available!, Please Install the app and try new.");
  97.             } else {
  98.                 scan();
  99.             }
  100.         } else {
  101.             return new PluginResult(PluginResult.Status.INVALID_ACTION);
  102.         }
  103.         PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
  104.         r.setKeepCallback(true);
  105.         return r;
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Starts an intent to scan and decode a barcode.
  111.      */
  112.     public void scan() {
  113.         Intent intentScan = new Intent(SCAN_INTENT);
  114.         intentScan.setPackage(SCAN_PACK);
  115.         //change mode for you requeriments
  116.         intentScan.putExtra("SCAN_MODE", "PRODUCT_MODE");
  117.  
  118.         intentScan.addCategory(Intent.CATEGORY_DEFAULT);
  119.  
  120.         this.cordova.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE);
  121.     }
  122.  
  123.     /**
  124.      * Called when the barcode scanner intent completes
  125.      *
  126.      * @param requestCode       The request code originally supplied to startActivityForResult(),
  127.      *                          allowing you to identify who this result came from.
  128.      * @param resultCode        The integer result code returned by the child activity through its setResult().
  129.      * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
  130.      */
  131.     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
  132.         if (requestCode == REQUEST_CODE) {
  133.             if (resultCode == Activity.RESULT_OK) {
  134.                 JSONObject obj = new JSONObject();
  135.                 try {
  136.                     obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
  137.                     obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
  138.                     obj.put(CANCELLED, false);
  139.                 } catch(JSONException e) {
  140.                     //Log.d(LOG_TAG, "This should never happen");
  141.                 }
  142.                 this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
  143.             } if (resultCode == Activity.RESULT_CANCELED) {
  144.                 JSONObject obj = new JSONObject();
  145.                 try {
  146.                     obj.put(TEXT, "");
  147.                     obj.put(FORMAT, "");
  148.                     obj.put(CANCELLED, true);
  149.                 } catch(JSONException e) {
  150.                     //Log.d(LOG_TAG, "This should never happen");
  151.                 }
  152.                 this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
  153.             } else {
  154.                 this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
  155.             }
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Initiates a barcode encode.
  161.      * @param data  The data to encode in the bar code
  162.      * @param data2
  163.      */
  164.     public void encode(String type, String data) {
  165.         Intent intentEncode = new Intent(ENCODE_INTENT);
  166.         intentEncode.putExtra(ENCODE_TYPE, type);
  167.         intentEncode.putExtra(ENCODE_DATA, data);
  168.  
  169.         this.cordova.getActivity().startActivity(intentEncode);
  170.        
  171.     }
  172.     //http://android-developers.blogspot.mx/2009/01/can-i-use-this-intent.html
  173.     /**
  174.      * Indicates whether the specified action can be used as an intent. This
  175.      * method queries the package manager for installed packages that can
  176.      * respond to an intent with the specified action. If no suitable package is
  177.      * found, this method returns false.
  178.      *
  179.      * @param context The application's environment.
  180.      * @param action The Intent action to check for availability.
  181.      *
  182.      * @return True if an Intent with the specified action can be sent and
  183.      *         responded to, false otherwise.
  184.      */
  185.     private boolean isIntentAvailable(Context context, String action) {
  186.         final PackageManager packageManager = context.getPackageManager();
  187.         final Intent intent = new Intent(action);
  188.         List<ResolveInfo> list =
  189.                 packageManager.queryIntentActivities(intent,
  190.                         PackageManager.MATCH_DEFAULT_ONLY);
  191.         return list.size() > 0;
  192.     }
  193. }