From 3f166162a05c79782cbcf162154f16b94efa2609 Mon Sep 17 00:00:00 2001 From: Jayson Raymond Date: Mon, 7 Jan 2013 15:49:52 -0800 Subject: [PATCH 1/2] Update for Cordova/PhoneGap 2.0 plugins (http://simonmacdonald.blogspot.ca/2012/08/so-you-wanna-write-phonegap-200-android.html) --- README.md | 33 +++++++----- www/barcodescanner.js | 120 +++++++++++++++++++++++------------------- 2 files changed, 86 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 545a6c60..cd338a00 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,17 @@ Cross-platform BarcodeScanner for Cordova / PhoneGap. Follows the [Cordova Plugin spec](https://github.com/alunny/cordova-plugin-spec), so that it works with [Pluginstall](https://github.com/alunny/pluginstall). +This plugin leverages Cordova/PhoneGap's [require/define functionality used for plugins](http://simonmacdonald.blogspot.ca/2012/08/so-you-wanna-write-phonegap-200-android.html). + Note: the Android source for this project includes an Android Library Project. pluginstall currently doesn't support Library Project refs, so its been prebuilt as a jar library. Any updates to the Library Project should be committed with an updated jar. ## Using the plugin ## -The plugin creates the object `window.plugins.barcodeScanner` with the method `scan(success, fail)`. -The following barcode types are currently supported: +The plugin creates the object `cordova/plugin/BarcodeScanner` with the method `scan(success, fail)`. +The following barcode types are currently supported: ### Android * QR_CODE @@ -46,15 +48,20 @@ The following barcode types are currently supported: A full example could be: - window.plugins.barcodeScanner.scan( function(result) { - alert("We got a barcode\n" + - "Result: " + result.text + "\n" + - "Format: " + result.format + "\n" + - "Cancelled: " + result.cancelled); - }, function(error) { - alert("Scanning failed: " + error); - } - ); + var scanner = $wnd.PhoneGap.require("cordova/plugin/BarcodeScanner"); + + scanner.scan( + function (result) { + alert("We got a barcode\n" + + "Result: " + result.text + "\n" + + "Format: " + result.format + "\n" + + "Cancelled: " + result.cancelled); + }, + function (error) { + alert("Scanning failed: " + error); + } + ); + ## Encoding a Barcode ## The plugin creates the object `window.plugins.barcodeScanner` with the method `encode(type, data, success, fail)`. @@ -67,7 +74,9 @@ Supported encoding types: A full example could be: - window.plugins.barcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) { + var scanner = $wnd.PhoneGap.require("cordova/plugin/BarcodeScanner"); + + scanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) { alert("encode success: " + success); }, function(fail) { alert("encoding failed: " + fail); diff --git a/www/barcodescanner.js b/www/barcodescanner.js index 52615a36..4cc56a46 100644 --- a/www/barcodescanner.js +++ b/www/barcodescanner.js @@ -6,58 +6,68 @@ * Copyright (c) 2011, IBM Corporation */ -var BarcodeScanner = function() { -}; - -//------------------------------------------------------------------- -BarcodeScanner.Encode = { - TEXT_TYPE: "TEXT_TYPE", - EMAIL_TYPE: "EMAIL_TYPE", - PHONE_TYPE: "PHONE_TYPE", - SMS_TYPE: "SMS_TYPE", - // CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascriopt to Java - // LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascriopt to Java -}; - -//------------------------------------------------------------------- -BarcodeScanner.prototype.scan = function(successCallback, errorCallback) { - if (errorCallback == null) { errorCallback = function() {}} - - if (typeof errorCallback != "function") { - console.log("BarcodeScanner.scan failure: failure parameter not a function"); - return - } - - if (typeof successCallback != "function") { - console.log("BarcodeScanner.scan failure: success callback parameter must be a function"); - return - } - - cordova.exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []); -}; - -//------------------------------------------------------------------- -BarcodeScanner.prototype.encode = function(type, data, successCallback, errorCallback, options) { - if (errorCallback == null) { errorCallback = function() {}} - - if (typeof errorCallback != "function") { - console.log("BarcodeScanner.scan failure: failure parameter not a function"); - return - } - - if (typeof successCallback != "function") { - console.log("BarcodeScanner.scan failure: success callback parameter must be a function"); - return - } - - cordova.exec(successCallback, errorCallback, 'BarcodeScanner', 'encode', [{"type": type, "data": data, "options": options}]); -}; - -//------------------------------------------------------------------- - -if(!window.plugins) { - window.plugins = {}; -} -if (!window.plugins.barcodeScanner) { - window.plugins.barcodeScanner = new BarcodeScanner(); -} + +PhoneGap.define("cordova/plugin/BarcodeScanner", + + function (require, exports, module) { + + var exec = require("cordova/exec"); + + var BarcodeScanner = function () { + }; + + //------------------------------------------------------------------- + BarcodeScanner.Encode = { + TEXT_TYPE: "TEXT_TYPE", + EMAIL_TYPE: "EMAIL_TYPE", + PHONE_TYPE: "PHONE_TYPE", + SMS_TYPE: "SMS_TYPE", + // CONTACT_TYPE: "CONTACT_TYPE", // TODO: not implemented, requires passing a Bundle class from Javascript to Java + // LOCATION_TYPE: "LOCATION_TYPE" // TODO: not implemented, requires passing a Bundle class from Javascript to Java + }; + + //------------------------------------------------------------------- + BarcodeScanner.prototype.scan = function (successCallback, errorCallback) { + if (errorCallback == null) { + errorCallback = function () { + } + } + + if (typeof errorCallback != "function") { + console.log("BarcodeScanner.scan failure: failure parameter not a function"); + return + } + + if (typeof successCallback != "function") { + console.log("BarcodeScanner.scan failure: success callback parameter must be a function"); + return + } + + exec(successCallback, errorCallback, 'BarcodeScanner', 'scan', []); + }; + + //------------------------------------------------------------------- + BarcodeScanner.prototype.encode = function (type, data, successCallback, errorCallback, options) { + if (errorCallback == null) { + errorCallback = function () { + } + } + + if (typeof errorCallback != "function") { + console.log("BarcodeScanner.scan failure: failure parameter not a function"); + return + } + + if (typeof successCallback != "function") { + console.log("BarcodeScanner.scan failure: success callback parameter must be a function"); + return + } + + exec(successCallback, errorCallback, 'BarcodeScanner', 'encode', [ + {"type": type, "data": data, "options": options} + ]); + }; + + var barcodeScanner = new BarcodeScanner(); + module.exports = barcodeScanner; + }); \ No newline at end of file From c7158af58ee9aeb0e325ae733c4bbfd1db51e563 Mon Sep 17 00:00:00 2001 From: Jayson Raymond Date: Thu, 10 Jan 2013 15:54:44 -0800 Subject: [PATCH 2/2] readme fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd338a00..d9f80b2c 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ The following barcode types are currently supported: A full example could be: - var scanner = $wnd.PhoneGap.require("cordova/plugin/BarcodeScanner"); + var scanner = window.PhoneGap.require("cordova/plugin/BarcodeScanner"); scanner.scan( function (result) { @@ -74,7 +74,7 @@ Supported encoding types: A full example could be: - var scanner = $wnd.PhoneGap.require("cordova/plugin/BarcodeScanner"); + var scanner = window.PhoneGap.require("cordova/plugin/BarcodeScanner"); scanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com", function(success) { alert("encode success: " + success);