Add callbackId and JS callback sugar to plugin class and interface

This commit is contained in:
Dave Johnson 2010-10-05 10:32:00 +01:00 committed by Bryce Curtis
parent 7d6ffc676d
commit f1421bc724
2 changed files with 51 additions and 4 deletions

View File

@ -45,6 +45,17 @@ public interface IPlugin {
*/ */
void setView(WebView webView); 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. * Called when the system is about to start resuming a previous activity.
*/ */

View File

@ -13,7 +13,8 @@ import android.webkit.WebView;
public abstract class Plugin implements IPlugin { public abstract class Plugin implements IPlugin {
public WebView webView; // WebView object public WebView webView; // WebView object
public DroidGap ctx; // DroidGap object public DroidGap ctx; // DroidGap object
public String callbackId; // key for the JavaScript callback
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
@ -54,6 +55,19 @@ public abstract class Plugin implements IPlugin {
this.webView = webView; 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. * Called when the system is about to start resuming a previous activity.
*/ */
@ -85,7 +99,8 @@ public abstract class Plugin implements IPlugin {
} }
/** /**
* Send JavaScript statement back to JavaScript. * Send generic JavaScript statement back to JavaScript.
* success(...) and error(...) should be used instead where possible.
* *
* @param statement * @param statement
*/ */
@ -93,4 +108,25 @@ public abstract class Plugin implements IPlugin {
this.ctx.callbackServer.sendJavascript(statement); this.ctx.callbackServer.sendJavascript(statement);
} }
/**
* Call the JavaScript success callback for this plugin.
*
* This can be used if the execute code for the plugin is asynchronous meaning
* that execute should return null and the callback from the async operation can
* call success(...) or error(...)
*
* @param pluginResult
*/
public void success(PluginResult pluginResult) {
this.ctx.callbackServer.sendJavascript(pluginResult.toSuccessCallbackString(this.callbackId));
}
/**
* Call the JavaScript error callback for this plugin.
*
* @param pluginResult
*/
public void error(PluginResult pluginResult) {
this.ctx.callbackServer.sendJavascript(pluginResult.toErrorCallbackString(this.callbackId));
}
} }