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

@ -44,7 +44,18 @@ public interface IPlugin {
* @param webView The PhoneGap 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.
*/

View File

@ -13,7 +13,8 @@ import android.webkit.WebView;
public abstract class Plugin implements IPlugin {
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.
@ -53,7 +54,20 @@ public abstract class Plugin implements IPlugin {
public void setView(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.
*/
@ -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
*/
@ -93,4 +108,25 @@ public abstract class Plugin implements IPlugin {
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));
}
}