From 8f7a5decb6d7783bbb42f6a4ffa296927a9e3812 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Wed, 25 May 2011 17:28:11 -0700 Subject: [PATCH 1/2] Add DS_Store to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5c53ca30..734d8757 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store default.properties gen assets/www/phonegap.js From 0b1e760fc151b4f3aee6847f0822a9d039dddf51 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Thu, 30 Jun 2011 13:42:40 -0700 Subject: [PATCH 2/2] Add better support for Activity result callbacks from plugins. Add some sugar for calling success / error callbacks from plugins --- framework/src/com/phonegap/DroidGap.java | 17 ++++---- .../com/phonegap/api/PhonegapActivity.java | 9 +++- framework/src/com/phonegap/api/Plugin.java | 42 +++++++++++++++++++ .../src/com/phonegap/api/PluginManager.java | 21 +--------- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 3830722e..17ebd615 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -9,6 +9,7 @@ package com.phonegap; import java.util.HashMap; import java.util.Map.Entry; + import org.json.JSONArray; import org.json.JSONException; @@ -617,7 +618,7 @@ public class DroidGap extends PhonegapActivity { public void setDoubleProperty(String name, double value) { this.getIntent().putExtra(name, value); } - + @Override /** * Called when the system is about to start resuming a previous activity. @@ -1311,12 +1312,7 @@ public class DroidGap extends PhonegapActivity { @Override public void startActivityForResult(Intent intent, int requestCode) throws RuntimeException { System.out.println("startActivityForResult(intent,"+requestCode+")"); - if (requestCode == -1) { - super.startActivityForResult(intent, requestCode); - } - else { - throw new RuntimeException("PhoneGap Exception: Call startActivityForResult(Command, Intent) instead."); - } + super.startActivityForResult(intent, requestCode); } /** @@ -1357,7 +1353,12 @@ public class DroidGap extends PhonegapActivity { callback.onActivityResult(requestCode, resultCode, intent); } } - + + @Override + public void setActivityResultCallback(Plugin plugin) { + this.activityResultCallback = plugin; + } + /** * Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable). * The errorCode parameter corresponds to one of the ERROR_* constants. diff --git a/framework/src/com/phonegap/api/PhonegapActivity.java b/framework/src/com/phonegap/api/PhonegapActivity.java index dcadfa2b..135cc3a5 100755 --- a/framework/src/com/phonegap/api/PhonegapActivity.java +++ b/framework/src/com/phonegap/api/PhonegapActivity.java @@ -15,7 +15,7 @@ import android.content.Intent; * It is used to isolate plugin development, and remove dependency on entire Phonegap library. */ public abstract class PhonegapActivity extends Activity { - + /** * Add a class that implements a service. * @@ -40,4 +40,11 @@ public abstract class PhonegapActivity extends Activity { * @param requestCode The request code that is passed to callback to identify the activity */ abstract public void startActivityForResult(Plugin command, Intent intent, int requestCode); + + /** + * Set the plugin to be called when a sub-activity exits. + * + * @param plugin The plugin on which onActivityResult is to be called + */ + abstract public void setActivityResultCallback(Plugin plugin); } diff --git a/framework/src/com/phonegap/api/Plugin.java b/framework/src/com/phonegap/api/Plugin.java index 746bd408..282f60f4 100755 --- a/framework/src/com/phonegap/api/Plugin.java +++ b/framework/src/com/phonegap/api/Plugin.java @@ -8,6 +8,7 @@ package com.phonegap.api; import org.json.JSONArray; +import org.json.JSONObject; import android.content.Intent; import android.webkit.WebView; @@ -19,6 +20,7 @@ import android.webkit.WebView; */ public abstract class Plugin implements IPlugin { + public String id; public WebView webView; // WebView object public PhonegapActivity ctx; // PhonegapActivity object @@ -126,6 +128,26 @@ public abstract class Plugin implements IPlugin { this.ctx.sendJavascript(pluginResult.toSuccessCallbackString(callbackId)); } + /** + * Helper for success callbacks that just returns the Status.OK by default + * + * @param message The message to add to the success result. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void success(JSONObject message, String callbackId) { + this.ctx.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId)); + } + + /** + * Helper for success callbacks that just returns the Status.OK by default + * + * @param message The message to add to the success result. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void success(String message, String callbackId) { + this.ctx.sendJavascript(new PluginResult(PluginResult.Status.OK, message).toSuccessCallbackString(callbackId)); + } + /** * Call the JavaScript error callback for this plugin. * @@ -135,4 +157,24 @@ public abstract class Plugin implements IPlugin { public void error(PluginResult pluginResult, String callbackId) { this.ctx.sendJavascript(pluginResult.toErrorCallbackString(callbackId)); } + + /** + * Helper for error callbacks that just returns the Status.ERROR by default + * + * @param message The message to add to the error result. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void error(JSONObject message, String callbackId) { + this.ctx.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId)); + } + + /** + * Helper for error callbacks that just returns the Status.ERROR by default + * + * @param message The message to add to the error result. + * @param callbackId The callback id used when calling back into JavaScript. + */ + public void error(String message, String callbackId) { + this.ctx.sendJavascript(new PluginResult(PluginResult.Status.ERROR, message).toErrorCallbackString(callbackId)); + } } diff --git a/framework/src/com/phonegap/api/PluginManager.java b/framework/src/com/phonegap/api/PluginManager.java index 393c6a29..12059e38 100755 --- a/framework/src/com/phonegap/api/PluginManager.java +++ b/framework/src/com/phonegap/api/PluginManager.java @@ -101,7 +101,7 @@ public final class PluginManager { ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); } } catch (Exception e) { - PluginResult cr = new PluginResult(PluginResult.Status.ERROR); + PluginResult cr = new PluginResult(PluginResult.Status.ERROR, e.getMessage()); ctx.sendJavascript(cr.toErrorCallbackString(callbackId)); } } @@ -160,24 +160,7 @@ public final class PluginManager { } return false; } - - /** - * Add plugin to be loaded and cached. This creates an instance of the plugin. - * If plugin is already created, then just return it. - * - * @param className The class to load - * @return The plugin - */ - public Plugin addPlugin(String className) { - try { - return this.addPlugin(className, this.getClassByName(className)); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - System.out.println("Error adding plugin "+className+"."); - } - return null; - } - + /** * Add plugin to be loaded and cached. This creates an instance of the plugin. * If plugin is already created, then just return it.