From 9adb85a64bc51d8d37281f9243a45151070cba22 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 5 Oct 2010 20:35:51 -0500 Subject: [PATCH] Add callbackId to Plugin.execute() so result can be sent back when overlapping calls to same plugin occur. --- framework/src/com/phonegap/AccelListener.java | 11 +++--- framework/src/com/phonegap/AudioHandler.java | 11 +++--- .../src/com/phonegap/CameraLauncher.java | 11 +++--- .../src/com/phonegap/CompassListener.java | 11 +++--- .../src/com/phonegap/ContactManager.java | 11 +++--- framework/src/com/phonegap/CryptoHandler.java | 11 +++--- framework/src/com/phonegap/Device.java | 11 +++--- framework/src/com/phonegap/FileUtils.java | 11 +++--- framework/src/com/phonegap/GeoBroker.java | 11 +++--- .../src/com/phonegap/NetworkManager.java | 11 +++--- framework/src/com/phonegap/Notification.java | 11 +++--- framework/src/com/phonegap/Storage.java | 11 +++--- framework/src/com/phonegap/TempListener.java | 11 +++--- framework/src/com/phonegap/api/IPlugin.java | 20 +++------- framework/src/com/phonegap/api/Plugin.java | 37 +++++++------------ .../src/com/phonegap/api/PluginManager.java | 36 ++++++++---------- 16 files changed, 111 insertions(+), 125 deletions(-) mode change 100644 => 100755 framework/src/com/phonegap/Storage.java diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index 2b3eb0ba..fb4005a7 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -59,13 +59,14 @@ public class AccelListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/AudioHandler.java b/framework/src/com/phonegap/AudioHandler.java index 1d0717cc..9beb7a65 100755 --- a/framework/src/com/phonegap/AudioHandler.java +++ b/framework/src/com/phonegap/AudioHandler.java @@ -35,13 +35,14 @@ public class AudioHandler extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/CameraLauncher.java b/framework/src/com/phonegap/CameraLauncher.java index 8f885f10..dab270c3 100755 --- a/framework/src/com/phonegap/CameraLauncher.java +++ b/framework/src/com/phonegap/CameraLauncher.java @@ -46,13 +46,14 @@ public class CameraLauncher extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/CompassListener.java b/framework/src/com/phonegap/CompassListener.java index 90f5ca3d..88e96b89 100755 --- a/framework/src/com/phonegap/CompassListener.java +++ b/framework/src/com/phonegap/CompassListener.java @@ -54,13 +54,14 @@ public class CompassListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/ContactManager.java b/framework/src/com/phonegap/ContactManager.java index 921c3bc2..00ad3e12 100755 --- a/framework/src/com/phonegap/ContactManager.java +++ b/framework/src/com/phonegap/ContactManager.java @@ -19,13 +19,14 @@ public class ContactManager extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { if (contactAccessor == null) { contactAccessor = ContactAccessor.getInstance(webView, ctx); } diff --git a/framework/src/com/phonegap/CryptoHandler.java b/framework/src/com/phonegap/CryptoHandler.java index bb8061ec..746433c8 100755 --- a/framework/src/com/phonegap/CryptoHandler.java +++ b/framework/src/com/phonegap/CryptoHandler.java @@ -15,13 +15,14 @@ public class CryptoHandler extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/Device.java b/framework/src/com/phonegap/Device.java index 0c10139a..a4cebbf4 100755 --- a/framework/src/com/phonegap/Device.java +++ b/framework/src/com/phonegap/Device.java @@ -56,13 +56,14 @@ public class Device extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/FileUtils.java b/framework/src/com/phonegap/FileUtils.java index 71a43376..094fa08a 100755 --- a/framework/src/com/phonegap/FileUtils.java +++ b/framework/src/com/phonegap/FileUtils.java @@ -33,13 +33,14 @@ public class FileUtils extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; //System.out.println("FileUtils.execute("+action+")"); diff --git a/framework/src/com/phonegap/GeoBroker.java b/framework/src/com/phonegap/GeoBroker.java index 3cd52f1d..6dafc401 100755 --- a/framework/src/com/phonegap/GeoBroker.java +++ b/framework/src/com/phonegap/GeoBroker.java @@ -29,13 +29,14 @@ public class GeoBroker extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index fa650e7b..256d34e3 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -37,13 +37,14 @@ public class NetworkManager extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { diff --git a/framework/src/com/phonegap/Notification.java b/framework/src/com/phonegap/Notification.java index c15e9125..e999c3b9 100755 --- a/framework/src/com/phonegap/Notification.java +++ b/framework/src/com/phonegap/Notification.java @@ -22,13 +22,14 @@ public class Notification extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/Storage.java b/framework/src/com/phonegap/Storage.java old mode 100644 new mode 100755 index fcf16ff0..a3339306 --- a/framework/src/com/phonegap/Storage.java +++ b/framework/src/com/phonegap/Storage.java @@ -25,13 +25,14 @@ public class Storage extends Plugin { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/TempListener.java b/framework/src/com/phonegap/TempListener.java index 62f634e2..5370421f 100644 --- a/framework/src/com/phonegap/TempListener.java +++ b/framework/src/com/phonegap/TempListener.java @@ -36,13 +36,14 @@ public class TempListener extends Plugin implements SensorEventListener { } /** - * Executes the request and returns CommandResult. + * Executes the request and returns PluginResult. * - * @param action The command to execute. - * @param args JSONArry of arguments for the command. - * @return A CommandResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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) { + public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; diff --git a/framework/src/com/phonegap/api/IPlugin.java b/framework/src/com/phonegap/api/IPlugin.java index 90e55110..2bb4e4db 100755 --- a/framework/src/com/phonegap/api/IPlugin.java +++ b/framework/src/com/phonegap/api/IPlugin.java @@ -15,11 +15,12 @@ public interface IPlugin { /** * Executes the request and returns PluginResult. * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @return A PluginResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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. */ - PluginResult execute(String action, JSONArray args); + PluginResult execute(String action, JSONArray args, String callbackId); /** * Identifies if action to be executed returns a value and should be run synchronously. @@ -45,17 +46,6 @@ public interface IPlugin { */ void setView(WebView webView); - /** - * Sets the callback ID that is required to call a success or error - * JavaScript callback. - * - * The JavaScript callback call looks like this: - * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); - * - * @param callbackId - */ - void setCallbackId(String callbackId); - /** * Called when the system is about to start resuming a previous activity. */ diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index 2676a2c7..5eb19ddb 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -14,16 +14,16 @@ public abstract class Plugin implements IPlugin { public WebView webView; // WebView object public DroidGap ctx; // DroidGap object - public String callbackId; // key for the JavaScript callback /** * Executes the request and returns PluginResult. * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @return A PluginResult object with a status and message. + * @param action The action to execute. + * @param args JSONArry 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 abstract PluginResult execute(String action, JSONArray args); + public abstract PluginResult execute(String action, JSONArray args, String callbackId); /** * Identifies if action to be executed returns a value and should be run synchronously. @@ -55,19 +55,6 @@ public abstract class Plugin implements IPlugin { this.webView = webView; } - /** - * Sets the callback ID that is required to call a success or error - * JavaScript callback. - * - * The JavaScript callback call looks like this: - * PhoneGap.callbackSuccess(callbackId, { message: 'foo' }); - * - * @param callbackId - */ - public void setCallbackId(String callbackId) { - this.callbackId = callbackId; - } - /** * Called when the system is about to start resuming a previous activity. */ @@ -115,18 +102,20 @@ public abstract class Plugin implements IPlugin { * that execute should return null and the callback from the async operation can * call success(...) or error(...) * - * @param pluginResult + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. */ - public void success(PluginResult pluginResult) { - this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId)); + public void success(PluginResult pluginResult, String callbackId) { + this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(callbackId)); } /** * Call the JavaScript error callback for this plugin. * - * @param pluginResult + * @param pluginResult The result to return. + * @param callbackId The callback id used when calling back into JavaScript. */ - public void error(PluginResult pluginResult) { - this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId)); + public void error(PluginResult pluginResult, String callbackId) { + this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(callbackId)); } } diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 637e92dd..2addf3af 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -70,19 +70,24 @@ public final class PluginManager { c = getClassByName(clazz); } if (isPhoneGapPlugin(c)) { - final Plugin plugin = this.addPlugin(clazz, c, callbackId); + final Plugin plugin = this.addPlugin(clazz, c); final DroidGap ctx = this.ctx; runAsync = async && !plugin.isSynch(action); if (runAsync) { // Run this on a different thread so that this one can return back to JS Thread thread = new Thread(new Runnable() { public void run() { - // Call execute on the plugin so that it can do it's thing - PluginResult cr = plugin.execute(action, args); - // Check the status for 0 (success) or otherwise - if (cr.getStatus() == 0) { - ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); - } else { + try { + // Call execute on the plugin so that it can do it's thing + PluginResult cr = plugin.execute(action, args, callbackId); + // Check the status for 0 (success) or otherwise + if (cr.getStatus() == 0) { + ctx.sendJavascript(cr.toSuccessCallbackString(callbackId)); + } else { + ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); + } + } catch (Exception e) { + PluginResult cr = new PluginResult(PluginResult.Status.ERROR); ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); } } @@ -91,7 +96,7 @@ public final class PluginManager { return ""; } else { // Call execute on the plugin so that it can do it's thing - cr = plugin.execute(action, args); + cr = plugin.execute(action, args, callbackId); } } } catch (ClassNotFoundException e) { @@ -157,7 +162,7 @@ public final class PluginManager { */ public Plugin addPlugin(String className) { try { - return this.addPlugin(className, this.getClassByName(className), ""); + return this.addPlugin(className, this.getClassByName(className)); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("Error adding plugin "+className+"."); @@ -175,7 +180,7 @@ public final class PluginManager { * @return The plugin */ @SuppressWarnings("unchecked") - private Plugin addPlugin(String className, Class clazz, String callbackId) { + private Plugin addPlugin(String className, Class clazz) { if (this.plugins.containsKey(className)) { return this.getPlugin(className); } @@ -185,7 +190,6 @@ public final class PluginManager { this.plugins.put(className, plugin); plugin.setContext((DroidGap)this.ctx); plugin.setView(this.app); - plugin.setCallbackId(callbackId); return plugin; } catch (Exception e) { @@ -217,16 +221,6 @@ public final class PluginManager { this.services.put(serviceType, className); } - /** - * Get the class that implements a service. - * - * @param serviceType - * @return - */ - //private String getClassForService(String serviceType) { - // return this.services.get(serviceType); - //} - /** * Called when the system is about to start resuming a previous activity. */