Add onReset to Plugin API, call on navigate.

This commit is contained in:
Braden Shepherdson 2012-09-12 14:31:01 -04:00
parent 3d62744601
commit 9961d9e54d
4 changed files with 38 additions and 6 deletions

View File

@ -23,20 +23,15 @@ import java.util.Hashtable;
import org.apache.cordova.api.CordovaInterface; import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.PluginResult; import org.apache.cordova.api.PluginResult;
import java.io.IOException;
import java.io.InputStream;
import org.apache.cordova.api.LOG; import org.apache.cordova.api.LOG;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.net.http.SslError; import android.net.http.SslError;
@ -44,7 +39,6 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.webkit.HttpAuthHandler; import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler; import android.webkit.SslErrorHandler;
import android.webkit.WebResourceResponse;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
@ -269,6 +263,11 @@ public class CordovaWebViewClient extends WebViewClient {
// Broadcast message that page has loaded // Broadcast message that page has loaded
this.appView.postMessage("onPageStarted", url); this.appView.postMessage("onPageStarted", url);
// Notify all plugins of the navigation, so they can clean up if necessary.
if (this.appView.pluginManager != null) {
this.appView.pluginManager.onReset();
}
} }
/** /**

View File

@ -116,4 +116,11 @@ public interface IPlugin {
* @return Return true to prevent the URL from loading. Default is false. * @return Return true to prevent the URL from loading. Default is false.
*/ */
boolean onOverrideUrlLoading(String url); boolean onOverrideUrlLoading(String url);
/**
* Called when the WebView does a top-level navigation or refreshes.
*
* Plugins should stop any long-running processes and clean up internal state.
*/
void onReset();
} }

View File

@ -23,6 +23,8 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import android.content.Intent; import android.content.Intent;
import android.util.Log;
/** /**
* Plugin interface must be implemented by any plugin classes. * Plugin interface must be implemented by any plugin classes.
* *
@ -215,4 +217,14 @@ public abstract class Plugin implements IPlugin {
public void error(String message, String callbackId) { public void error(String message, String callbackId) {
this.webView.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message), callbackId); this.webView.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, message), callbackId);
} }
/**
* Called when the WebView does a top-level navigation or refreshes.
*
* Plugins should stop any long-running processes and clean up internal state.
*
* Does nothing by default.
*/
public void onReset() {
}
} }

View File

@ -397,6 +397,20 @@ public class PluginManager {
return false; return false;
} }
/**
* Called when the app navigates or refreshes.
*/
public void onReset() {
Iterator<PluginEntry> it = this.entries.values().iterator();
while (it.hasNext()) {
IPlugin plugin = it.next().plugin;
if (plugin != null) {
plugin.onReset();
}
}
}
private void pluginConfigurationMissing() { private void pluginConfigurationMissing() {
LOG.e(TAG, "====================================================================================="); LOG.e(TAG, "=====================================================================================");
LOG.e(TAG, "ERROR: plugin.xml is missing. Add res/xml/plugins.xml to your project."); LOG.e(TAG, "ERROR: plugin.xml is missing. Add res/xml/plugins.xml to your project.");