From 9961d9e54d09edfb25e38b0faa48b5d69af927b9 Mon Sep 17 00:00:00 2001 From: Braden Shepherdson Date: Wed, 12 Sep 2012 14:31:01 -0400 Subject: [PATCH] Add onReset to Plugin API, call on navigate. --- .../org/apache/cordova/CordovaWebViewClient.java | 11 +++++------ framework/src/org/apache/cordova/api/IPlugin.java | 7 +++++++ framework/src/org/apache/cordova/api/Plugin.java | 12 ++++++++++++ .../src/org/apache/cordova/api/PluginManager.java | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java index fe0e9e9f..5f907636 100755 --- a/framework/src/org/apache/cordova/CordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java @@ -23,20 +23,15 @@ import java.util.Hashtable; import org.apache.cordova.api.CordovaInterface; import org.apache.cordova.api.PluginResult; -import java.io.IOException; -import java.io.InputStream; - import org.apache.cordova.api.LOG; import org.json.JSONException; import org.json.JSONObject; import android.annotation.TargetApi; -import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; -import android.content.res.AssetManager; import android.graphics.Bitmap; import android.net.Uri; import android.net.http.SslError; @@ -44,7 +39,6 @@ import android.util.Log; import android.view.View; import android.webkit.HttpAuthHandler; import android.webkit.SslErrorHandler; -import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -269,6 +263,11 @@ public class CordovaWebViewClient extends WebViewClient { // Broadcast message that page has loaded 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(); + } } /** diff --git a/framework/src/org/apache/cordova/api/IPlugin.java b/framework/src/org/apache/cordova/api/IPlugin.java index 870bb9e1..a33a663f 100755 --- a/framework/src/org/apache/cordova/api/IPlugin.java +++ b/framework/src/org/apache/cordova/api/IPlugin.java @@ -116,4 +116,11 @@ public interface IPlugin { * @return Return true to prevent the URL from loading. Default is false. */ 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(); } diff --git a/framework/src/org/apache/cordova/api/Plugin.java b/framework/src/org/apache/cordova/api/Plugin.java index 84b67e00..c27b1e54 100755 --- a/framework/src/org/apache/cordova/api/Plugin.java +++ b/framework/src/org/apache/cordova/api/Plugin.java @@ -23,6 +23,8 @@ import org.json.JSONArray; import org.json.JSONObject; import android.content.Intent; +import android.util.Log; + /** * 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) { 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() { + } } diff --git a/framework/src/org/apache/cordova/api/PluginManager.java b/framework/src/org/apache/cordova/api/PluginManager.java index aef63023..aaf614d7 100755 --- a/framework/src/org/apache/cordova/api/PluginManager.java +++ b/framework/src/org/apache/cordova/api/PluginManager.java @@ -397,6 +397,20 @@ public class PluginManager { return false; } + /** + * Called when the app navigates or refreshes. + */ + public void onReset() { + Iterator it = this.entries.values().iterator(); + while (it.hasNext()) { + IPlugin plugin = it.next().plugin; + if (plugin != null) { + plugin.onReset(); + } + } + } + + private void pluginConfigurationMissing() { LOG.e(TAG, "====================================================================================="); LOG.e(TAG, "ERROR: plugin.xml is missing. Add res/xml/plugins.xml to your project.");