mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Add callbackId and JS callback sugar to plugin class and interface
This commit is contained in:
parent
7d6ffc676d
commit
f1421bc724
@ -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.
|
||||||
*/
|
*/
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user