From 17c919edd99eb9016ac478e9cc777d9a25b14fd7 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 24 Apr 2012 15:50:06 -0700 Subject: [PATCH] Tweaks to move history over into the WebView --- framework/src/org/apache/cordova/App.java | 6 +- .../org/apache/cordova/CordovaWebView.java | 63 ++++++++++++++++ .../src/org/apache/cordova/DroidGap.java | 73 +++---------------- 3 files changed, 75 insertions(+), 67 deletions(-) diff --git a/framework/src/org/apache/cordova/App.java b/framework/src/org/apache/cordova/App.java index 664fe353..21f66c0b 100755 --- a/framework/src/org/apache/cordova/App.java +++ b/framework/src/org/apache/cordova/App.java @@ -87,7 +87,7 @@ public class App extends Plugin { * Clear the resource cache. */ public void clearCache() { - ((DroidGap)this.ctx).clearCache(); + webView.clearCache(true); } /** @@ -147,11 +147,11 @@ public class App extends Plugin { e.printStackTrace(); } } - ((DroidGap)this.ctx).showWebPage(url, openExternal, clearHistory, params); + webView.showWebPage(url, openExternal, clearHistory, params); } /** - * Cancel loadUrl before it has been loaded. + * Cancel loadUrl before it has been loaded (Only works on a CordovaInterface class) */ public void cancelLoadUrl() { ((DroidGap)this.ctx).cancelLoadUrl(); diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index 1b139f38..59785e8d 100644 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -14,7 +14,9 @@ import org.apache.cordova.api.PluginManager; import org.xmlpull.v1.XmlPullParserException; import android.content.Context; +import android.content.Intent; import android.content.res.XmlResourceParser; +import android.net.Uri; import android.util.AttributeSet; import android.webkit.WebSettings; import android.webkit.WebView; @@ -388,4 +390,65 @@ public class CordovaWebView extends WebView { return false; } + + + /** + * Load the specified URL in the Cordova webview or a new browser instance. + * + * NOTE: If openExternal is false, only URLs listed in whitelist can be loaded. + * + * @param url The url to load. + * @param openExternal Load url in browser instead of Cordova webview. + * @param clearHistory Clear the history stack, so new page becomes top of history + * @param params DroidGap parameters for new app + */ + public void showWebPage(String url, boolean openExternal, boolean clearHistory, HashMap params) { //throws android.content.ActivityNotFoundException { + LOG.d(TAG, "showWebPage(%s, %b, %b, HashMap", url, openExternal, clearHistory); + + // If clearing history + if (clearHistory) { + this.clearHistory(); + } + + // If loading into our webview + if (!openExternal) { + + // Make sure url is in whitelist + if (url.startsWith("file://") || url.indexOf(this.baseUrl) == 0 || isUrlWhiteListed(url)) { + // TODO: What about params? + + // Clear out current url from history, since it will be replacing it + if (clearHistory) { + this.urls.clear(); + } + + // Load new URL + this.loadUrl(url); + } + // Load in default viewer if not + else { + LOG.w(TAG, "showWebPage: Cannot load URL into webview since it is not in white list. Loading into browser instead. (URL="+url+")"); + try { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + mCtx.startActivity(intent); + } catch (android.content.ActivityNotFoundException e) { + LOG.e(TAG, "Error loading url "+url, e); + } + } + } + + // Load in default view intent + else { + try { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + mCtx.startActivity(intent); + } catch (android.content.ActivityNotFoundException e) { + LOG.e(TAG, "Error loading url "+url, e); + } + } + } + + } diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index e523b00a..0203dba3 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -238,8 +238,8 @@ public class DroidGap extends Activity implements CordovaInterface { root = new LinearLayoutSoftKeyboardDetect(this, width, height); root.setOrientation(LinearLayout.VERTICAL); root.setBackgroundColor(this.backgroundColor); - root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, - ViewGroup.LayoutParams.FILL_PARENT, 0.0F)); + root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, 0.0F)); // If url was passed in to intent, then init webview, which will load the url Bundle bundle = this.getIntent().getExtras(); @@ -280,8 +280,8 @@ public class DroidGap extends Activity implements CordovaInterface { this.loadConfiguration(); this.appView.setLayoutParams(new LinearLayout.LayoutParams( - ViewGroup.LayoutParams.FILL_PARENT, - ViewGroup.LayoutParams.FILL_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, 1.0F)); // Add web view but make it invisible while loading URL @@ -366,7 +366,6 @@ public class DroidGap extends Activity implements CordovaInterface { // If loadingDialog property, then show the App loading dialog for first page of app // (This doesn't seem to actually do anything here) - /* String loading = null; if (me.urls.size() == 1) { loading = me.getStringProperty("loadingDialog", null); @@ -392,7 +391,6 @@ public class DroidGap extends Activity implements CordovaInterface { } me.spinnerStart(title, message); } - */ // Create a timeout timer for loadUrl final int currentLoadUrlTimeout = me.loadUrlTimeout; @@ -799,64 +797,6 @@ public class DroidGap extends Activity implements CordovaInterface { appView.callbackServer.sendJavascript(statement); } - /** - * Load the specified URL in the Cordova webview or a new browser instance. - * - * NOTE: If openExternal is false, only URLs listed in whitelist can be loaded. - * - * @param url The url to load. - * @param openExternal Load url in browser instead of Cordova webview. - * @param clearHistory Clear the history stack, so new page becomes top of history - * @param params DroidGap parameters for new app - */ - public void showWebPage(String url, boolean openExternal, boolean clearHistory, HashMap params) { //throws android.content.ActivityNotFoundException { - LOG.d(TAG, "showWebPage(%s, %b, %b, HashMap", url, openExternal, clearHistory); - - // If clearing history - if (clearHistory) { - this.clearHistory(); - } - - // If loading into our webview - if (!openExternal) { - - // Make sure url is in whitelist - if (url.startsWith("file://") || url.indexOf(this.baseUrl) == 0 || isUrlWhiteListed(url)) { - // TODO: What about params? - - // Clear out current url from history, since it will be replacing it - if (clearHistory) { - this.urls.clear(); - } - - // Load new URL - this.loadUrl(url); - } - // Load in default viewer if not - else { - LOG.w(TAG, "showWebPage: Cannot load URL into webview since it is not in white list. Loading into browser instead. (URL="+url+")"); - try { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - this.startActivity(intent); - } catch (android.content.ActivityNotFoundException e) { - LOG.e(TAG, "Error loading url "+url, e); - } - } - } - - // Load in default view intent - else { - try { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - this.startActivity(intent); - } catch (android.content.ActivityNotFoundException e) { - LOG.e(TAG, "Error loading url "+url, e); - } - } - } - /** * Show the spinner. Must be called from the UI thread. * @@ -1189,4 +1129,9 @@ public class DroidGap extends Activity implements CordovaInterface { return appView.backHistory(); } + public void showWebPage(String url, boolean openExternal, + boolean clearHistory, HashMap params) { + appView.showWebPage(url, openExternal, clearHistory, params); + } + }