From ecd6ca0172de56d6524c25fd2996913184b575fb Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 31 Jul 2012 16:16:57 -0700 Subject: [PATCH] Moving the fix for # and ? to a new class to fix CB-995 --- .../org/apache/cordova/CordovaWebView.java | 19 +++++- .../apache/cordova/CordovaWebViewClient.java | 39 ----------- .../cordova/IceCreamCordovaWebViewClient.java | 65 +++++++++++++++++++ 3 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index 7fd5775a..0ce63c03 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -119,7 +119,7 @@ public class CordovaWebView extends WebView { Log.d(TAG, "Your activity must implement CordovaInterface to work"); } this.setWebChromeClient(new CordovaChromeClient(this.cordova, this)); - this.setWebViewClient(new CordovaWebViewClient(this.cordova, this)); + this.initWebViewClient(this.cordova); this.loadConfiguration(); this.setup(); } @@ -143,7 +143,7 @@ public class CordovaWebView extends WebView { Log.d(TAG, "Your activity must implement CordovaInterface to work"); } this.setWebChromeClient(new CordovaChromeClient(this.cordova, this)); - this.setWebViewClient(new CordovaWebViewClient(this.cordova, this)); + this.initWebViewClient(this.cordova); this.loadConfiguration(); this.setup(); } @@ -167,11 +167,24 @@ public class CordovaWebView extends WebView { Log.d(TAG, "Your activity must implement CordovaInterface to work"); } this.setWebChromeClient(new CordovaChromeClient(this.cordova)); - this.setWebViewClient(new CordovaWebViewClient(this.cordova)); + this.initWebViewClient(this.cordova); this.loadConfiguration(); this.setup(); } + + private void initWebViewClient(CordovaInterface cordova) { + if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB) + { + this.setWebViewClient(new CordovaWebViewClient(this.cordova, this)); + } + else + { + this.setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this)); + } + } + + /** * Initialize webview. */ diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java index 4934c8f3..6e4ff7f3 100755 --- a/framework/src/org/apache/cordova/CordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java @@ -459,43 +459,4 @@ public class CordovaWebViewClient extends WebViewClient { this.authenticationTokens.clear(); } - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, String url) { - if(url.contains("?") || url.contains("#")){ - return generateWebResourceResponse(url); - } else { - return super.shouldInterceptRequest(view, url); - } - } - - private WebResourceResponse generateWebResourceResponse(String url) { - final String ANDROID_ASSET = "file:///android_asset/"; - if (url.startsWith(ANDROID_ASSET)) { - String niceUrl = url; - niceUrl = url.replaceFirst(ANDROID_ASSET, ""); - if(niceUrl.contains("?")){ - niceUrl = niceUrl.split("\\?")[0]; - } - else if(niceUrl.contains("#")) - { - niceUrl = niceUrl.split("#")[0]; - } - - String mimetype = null; - if(niceUrl.endsWith(".html")){ - mimetype = "text/html"; - } - - try { - AssetManager assets = cordova.getActivity().getAssets(); - Uri uri = Uri.parse(niceUrl); - InputStream stream = assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING); - WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream); - return response; - } catch (IOException e) { - LOG.e("generateWebResourceResponse", e.getMessage(), e); - } - } - return null; - } } diff --git a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java new file mode 100644 index 00000000..24a9daac --- /dev/null +++ b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java @@ -0,0 +1,65 @@ +package org.apache.cordova; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.cordova.api.CordovaInterface; +import org.apache.cordova.api.LOG; + +import android.content.res.AssetManager; +import android.net.Uri; +import android.webkit.WebResourceResponse; +import android.webkit.WebView; + +public class IceCreamCordovaWebViewClient extends CordovaWebViewClient { + + + public IceCreamCordovaWebViewClient(CordovaInterface cordova) { + super(cordova); + } + + public IceCreamCordovaWebViewClient(CordovaInterface cordova, CordovaWebView view) { + super(cordova, view); + } + + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, String url) { + if(url.contains("?") || url.contains("#")){ + return generateWebResourceResponse(url); + } else { + return super.shouldInterceptRequest(view, url); + } + } + + private WebResourceResponse generateWebResourceResponse(String url) { + final String ANDROID_ASSET = "file:///android_asset/"; + if (url.startsWith(ANDROID_ASSET)) { + String niceUrl = url; + niceUrl = url.replaceFirst(ANDROID_ASSET, ""); + if(niceUrl.contains("?")){ + niceUrl = niceUrl.split("\\?")[0]; + } + else if(niceUrl.contains("#")) + { + niceUrl = niceUrl.split("#")[0]; + } + + String mimetype = null; + if(niceUrl.endsWith(".html")){ + mimetype = "text/html"; + } + + try { + AssetManager assets = cordova.getActivity().getAssets(); + Uri uri = Uri.parse(niceUrl); + InputStream stream = assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING); + WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream); + return response; + } catch (IOException e) { + LOG.e("generateWebResourceResponse", e.getMessage(), e); + } + } + return null; + } + +}