From c84bf0e66d0ac14b44283261a2a3c9813e4750db Mon Sep 17 00:00:00 2001 From: Eccenux Date: Wed, 24 Jul 2013 18:41:40 +0200 Subject: [PATCH] Changes for crodova 3.0 Updates based mostly on: http://simonmacdonald.blogspot.fr/2013/06/why-dont-my-plugins-work-in-phonegap.html --- .../barcodescanner/BarcodeScanner.java | 93 +++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java b/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java index b48b5a6..6b1bcca 100644 --- a/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java +++ b/src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java @@ -4,8 +4,8 @@ * * Copyright (c) Matt Kane 2010 * Copyright (c) 2011, IBM Corporation + * Copyright (c) 2013, Maciej Nux Jaros */ - package com.phonegap.plugins.barcodescanner; import org.json.JSONArray; @@ -16,13 +16,18 @@ import android.app.Activity; import android.content.Intent; import android.util.Log; -import org.apache.cordova.api.Plugin; -import org.apache.cordova.api.PluginResult; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.PluginResult; /** * This calls out to the ZXing barcode reader and returns the result. + * + * @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java */ -public class BarcodeScanner extends Plugin { +public class BarcodeScanner extends CordovaPlugin { + public static final int REQUEST_CODE = 0x0ba7c0de; + private static final String SCAN = "scan"; private static final String ENCODE = "encode"; private static final String CANCELLED = "cancelled"; @@ -39,9 +44,9 @@ public class BarcodeScanner extends Plugin { private static final String PHONE_TYPE = "PHONE_TYPE"; private static final String SMS_TYPE = "SMS_TYPE"; - public static final int REQUEST_CODE = 0x0ba7c0de; + private static final String LOG_TAG = "BarcodeScanner"; - public String callback; + private CallbackContext callbackContext; /** * Constructor. @@ -50,15 +55,24 @@ public class BarcodeScanner extends Plugin { } /** - * Executes the request and returns PluginResult. + * Executes the request. * - * @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. + * This method is called from the WebView thread. To do a non-trivial amount of work, use: + * cordova.getThreadPool().execute(runnable); + * + * To run on the UI thread, use: + * cordova.getActivity().runOnUiThread(runnable); + * + * @param action The action to execute. + * @param args The exec() arguments. + * @param callbackContext The callback context used when calling back into JavaScript. + * @return Whether the action was valid. + * + * @sa https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaPlugin.java */ - public PluginResult execute(String action, JSONArray args, String callbackId) { - this.callback = callbackId; + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { + this.callbackContext = callbackContext; if (action.equals(ENCODE)) { JSONObject obj = args.optJSONObject(0); @@ -72,25 +86,23 @@ public class BarcodeScanner extends Plugin { } if (data == null) { - return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode"); + callbackContext.error("User did not specify data to encode"); + return true; } encode(type, data); } else { - return new PluginResult(PluginResult.Status.ERROR, "User did not specify data to encode"); + callbackContext.error("User did not specify data to encode"); + return true; } - } - else if (action.equals(SCAN)) { + } else if (action.equals(SCAN)) { scan(); } else { - return new PluginResult(PluginResult.Status.INVALID_ACTION); + return false; } - PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); - r.setKeepCallback(true); - return r; + return true; } - /** * Starts an intent to scan and decode a barcode. */ @@ -98,17 +110,18 @@ public class BarcodeScanner extends Plugin { Intent intentScan = new Intent(SCAN_INTENT); intentScan.addCategory(Intent.CATEGORY_DEFAULT); - this.cordova.startActivityForResult((Plugin) this, intentScan, REQUEST_CODE); + this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE); } /** - * Called when the barcode scanner intent completes + * 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"). + * @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"). */ + @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { @@ -117,30 +130,34 @@ public class BarcodeScanner extends Plugin { 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"); + } catch (JSONException e) { + Log.d(LOG_TAG, "This should never happen"); } - this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); + //this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); + this.callbackContext.success(obj); } else 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"); + } catch (JSONException e) { + Log.d(LOG_TAG, "This should never happen"); } - this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); + //this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback); + this.callbackContext.success(obj); } else { - this.error(new PluginResult(PluginResult.Status.ERROR), this.callback); + //this.error(new PluginResult(PluginResult.Status.ERROR), this.callback); + this.callbackContext.error("Unexpected error"); } } } /** * Initiates a barcode encode. - * @param data The data to encode in the bar code - * @param data2 + * + * @param type Endoiding type. + * @param data The data to encode in the bar code. */ public void encode(String type, String data) { Intent intentEncode = new Intent(ENCODE_INTENT); @@ -149,4 +166,4 @@ public class BarcodeScanner extends Plugin { this.cordova.getActivity().startActivity(intentEncode); } -} \ No newline at end of file +}