diff --git a/mobile/cordova_plugins.js b/mobile/cordova_plugins.js index aab670afa..887842e5e 100644 --- a/mobile/cordova_plugins.js +++ b/mobile/cordova_plugins.js @@ -27,6 +27,17 @@ module.exports = [ "clobbers": [ "cordova.plugins.clipboard" ] + }, + { + "file": "plugins/nl.x-services.plugins.toast/www/Toast.js", + "id": "nl.x-services.plugins.toast.Toast", + "clobbers": [ + "window.plugins.toast" + ] + }, + { + "file": "plugins/nl.x-services.plugins.toast/test/tests.js", + "id": "nl.x-services.plugins.toast.tests" } ]; module.exports.metadata = @@ -35,7 +46,8 @@ module.exports.metadata = "de.appplant.cordova.plugin.email-composer": "0.8.2dev", "com.phonegap.plugins.barcodescanner": "1.0.1", "org.apache.cordova.splashscreen": "0.3.0", - "com.verso.cordova.clipboard": "0.1.0" + "com.verso.cordova.clipboard": "0.1.0", + "nl.x-services.plugins.toast": "2.0" } // BOTTOM OF METADATA }); \ No newline at end of file diff --git a/mobile/plugins/nl.x-services.plugins.toast/test/tests.js b/mobile/plugins/nl.x-services.plugins.toast/test/tests.js new file mode 100644 index 000000000..1d079d4d8 --- /dev/null +++ b/mobile/plugins/nl.x-services.plugins.toast/test/tests.js @@ -0,0 +1,59 @@ +cordova.define("nl.x-services.plugins.toast.tests", function(require, exports, module) { exports.defineAutoTests = function() { + + var fail = function (done) { + expect(true).toBe(false); + done(); + }, + succeed = function (done) { + expect(true).toBe(true); + done(); + }; + + describe('Plugin availability', function () { + it("window.plugins.toast should exist", function() { + expect(window.plugins.toast).toBeDefined(); + }); + }); + + describe('API functions', function () { + it("should define show", function() { + expect(window.plugins.toast.show).toBeDefined(); + }); + + it("should define showShortTop", function() { + expect(window.plugins.toast.showShortTop).toBeDefined(); + }); + + it("should define showShortCenter", function() { + expect(window.plugins.toast.showShortCenter).toBeDefined(); + }); + + it("should define showShortBottom", function() { + expect(window.plugins.toast.showShortBottom).toBeDefined(); + }); + + it("should define showLongTop", function() { + expect(window.plugins.toast.showLongTop).toBeDefined(); + }); + + it("should define showLongCenter", function() { + expect(window.plugins.toast.showLongCenter).toBeDefined(); + }); + + it("should define showLongBottom", function() { + expect(window.plugins.toast.showLongBottom).toBeDefined(); + }); + }); + + describe('Invalid usage', function () { + it("should fail due to an invalid position", function(done) { + window.plugins.toast.show('hi', 'short', 'nowhere', fail.bind(null, done), succeed.bind(null, done)); + }); + + it("should fail due to an invalid duration", function(done) { + window.plugins.toast.show('hi', 'medium', 'top', fail.bind(null, done), succeed.bind(null, done)); + }); + }); +}; + +}); diff --git a/mobile/plugins/nl.x-services.plugins.toast/www/Toast.js b/mobile/plugins/nl.x-services.plugins.toast/www/Toast.js new file mode 100644 index 000000000..cad6dab44 --- /dev/null +++ b/mobile/plugins/nl.x-services.plugins.toast/www/Toast.js @@ -0,0 +1,42 @@ +cordova.define("nl.x-services.plugins.toast.Toast", function(require, exports, module) { function Toast() { +} + +Toast.prototype.show = function (message, duration, position, successCallback, errorCallback) { + cordova.exec(successCallback, errorCallback, "Toast", "show", [message, duration, position]); +}; + +Toast.prototype.showShortTop = function (message, successCallback, errorCallback) { + this.show(message, "short", "top", successCallback, errorCallback); +}; + +Toast.prototype.showShortCenter = function (message, successCallback, errorCallback) { + this.show(message, "short", "center", successCallback, errorCallback); +}; + +Toast.prototype.showShortBottom = function (message, successCallback, errorCallback) { + this.show(message, "short", "bottom", successCallback, errorCallback); +}; + +Toast.prototype.showLongTop = function (message, successCallback, errorCallback) { + this.show(message, "long", "top", successCallback, errorCallback); +}; + +Toast.prototype.showLongCenter = function (message, successCallback, errorCallback) { + this.show(message, "long", "center", successCallback, errorCallback); +}; + +Toast.prototype.showLongBottom = function (message, successCallback, errorCallback) { + this.show(message, "long", "bottom", successCallback, errorCallback); +}; + +Toast.install = function () { + if (!window.plugins) { + window.plugins = {}; + } + + window.plugins.toast = new Toast(); + return window.plugins.toast; +}; + +cordova.addConstructor(Toast.install); +}); diff --git a/mobile/res/xml/config.xml b/mobile/res/xml/config.xml index 871fce104..dfd9181ad 100644 --- a/mobile/res/xml/config.xml +++ b/mobile/res/xml/config.xml @@ -31,4 +31,7 @@ + + + diff --git a/mobile/src/nl/xservices/plugins/Toast.java b/mobile/src/nl/xservices/plugins/Toast.java new file mode 100644 index 000000000..6d44a77e4 --- /dev/null +++ b/mobile/src/nl/xservices/plugins/Toast.java @@ -0,0 +1,56 @@ +package nl.xservices.plugins; + +import android.view.Gravity; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.json.JSONArray; +import org.json.JSONException; + +public class Toast extends CordovaPlugin { + + private static final String ACTION_SHOW_EVENT = "show"; + + @Override + public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { + if (ACTION_SHOW_EVENT.equals(action)) { + + final String message = args.getString(0); + final String duration = args.getString(1); + final String position = args.getString(2); + + cordova.getActivity().runOnUiThread(new Runnable() { + public void run() { + android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), message, 0); + + if ("top".equals(position)) { + toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 20); + } else if ("bottom".equals(position)) { + toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 20); + } else if ("center".equals(position)) { + toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0); + } else { + callbackContext.error("invalid position. valid options are 'top', 'center' and 'bottom'"); + return; + } + + if ("short".equals(duration)) { + toast.setDuration(android.widget.Toast.LENGTH_SHORT); + } else if ("long".equals(duration)) { + toast.setDuration(android.widget.Toast.LENGTH_LONG); + } else { + callbackContext.error("invalid duration. valid options are 'short' and 'long'"); + return; + } + + toast.show(); + callbackContext.success(); + } + }); + + return true; + } else { + callbackContext.error("toast." + action + " is not a supported function. Did you mean '" + ACTION_SHOW_EVENT + "'?"); + return false; + } + } +}