mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Merge IceCreamCordovaWebViewClient into AndroidWebViewClient.
There was no reason to have it separate.
This commit is contained in:
parent
c12d93e77f
commit
f1d4c01190
@ -49,7 +49,6 @@ import android.webkit.WebSettings;
|
|||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebSettings.LayoutAlgorithm;
|
import android.webkit.WebSettings.LayoutAlgorithm;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import android.webkit.CookieManager;
|
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
|
|
||||||
@ -144,12 +143,9 @@ public class AndroidWebView extends WebView implements CordovaWebView {
|
|||||||
pluginManager.init();
|
pluginManager.init();
|
||||||
|
|
||||||
if (this.viewClient == null) {
|
if (this.viewClient == null) {
|
||||||
setWebViewClient(Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
|
setWebViewClient(new AndroidWebViewClient(cordova, this));
|
||||||
new AndroidWebViewClient(cordova, this) :
|
|
||||||
new IceCreamCordovaWebViewClient(cordova, this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (this.chromeClient == null) {
|
if (this.chromeClient == null) {
|
||||||
setWebChromeClient(new AndroidChromeClient(cordova, this));
|
setWebChromeClient(new AndroidChromeClient(cordova, this));
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.cordova;
|
package org.apache.cordova;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -28,11 +30,14 @@ 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.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.net.Uri;
|
||||||
import android.net.http.SslError;
|
import android.net.http.SslError;
|
||||||
|
import android.os.Build;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.webkit.ClientCertRequest;
|
import android.webkit.ClientCertRequest;
|
||||||
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;
|
||||||
|
|
||||||
@ -358,4 +363,60 @@ public class AndroidWebViewClient extends WebViewClient {
|
|||||||
public void clearAuthenticationTokens() {
|
public void clearAuthenticationTokens() {
|
||||||
this.authenticationTokens.clear();
|
this.authenticationTokens.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
|
@Override
|
||||||
|
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
||||||
|
try {
|
||||||
|
// Check the against the whitelist and lock out access to the WebView directory
|
||||||
|
// Changing this will cause problems for your application
|
||||||
|
if (!appView.getPluginManager().shouldAllowRequest(url)) {
|
||||||
|
LOG.w(TAG, "URL blocked by whitelist: " + url);
|
||||||
|
// Results in a 404.
|
||||||
|
return new WebResourceResponse("text/plain", "UTF-8", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
CordovaResourceApi resourceApi = appView.getResourceApi();
|
||||||
|
Uri origUri = Uri.parse(url);
|
||||||
|
// Allow plugins to intercept WebView requests.
|
||||||
|
Uri remappedUri = resourceApi.remapUri(origUri);
|
||||||
|
|
||||||
|
if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) {
|
||||||
|
CordovaResourceApi.OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
|
||||||
|
return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
|
||||||
|
}
|
||||||
|
// If we don't need to special-case the request, let the browser load it.
|
||||||
|
return null;
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (!(e instanceof FileNotFoundException)) {
|
||||||
|
LOG.e("IceCreamCordovaWebViewClient", "Error occurred while loading a file (returning a 404).", e);
|
||||||
|
}
|
||||||
|
// Results in a 404.
|
||||||
|
return new WebResourceResponse("text/plain", "UTF-8", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean needsKitKatContentUrlFix(Uri uri) {
|
||||||
|
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && "content".equals(uri.getScheme());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean needsSpecialsInAssetUrlFix(Uri uri) {
|
||||||
|
if (CordovaResourceApi.getUriType(uri) != CordovaResourceApi.URI_TYPE_ASSET) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (uri.getQuery() != null || uri.getFragment() != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!uri.toString().contains("%")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(android.os.Build.VERSION.SDK_INT){
|
||||||
|
case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH:
|
||||||
|
case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,97 +0,0 @@
|
|||||||
/*
|
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
|
||||||
or more contributor license agreements. See the NOTICE file
|
|
||||||
distributed with this work for additional information
|
|
||||||
regarding copyright ownership. The ASF licenses this file
|
|
||||||
to you under the Apache License, Version 2.0 (the
|
|
||||||
"License"); you may not use this file except in compliance
|
|
||||||
with the License. You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing,
|
|
||||||
software distributed under the License is distributed on an
|
|
||||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
||||||
KIND, either express or implied. See the License for the
|
|
||||||
specific language governing permissions and limitations
|
|
||||||
under the License.
|
|
||||||
*/
|
|
||||||
package org.apache.cordova;
|
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.apache.cordova.CordovaInterface;
|
|
||||||
import org.apache.cordova.CordovaResourceApi.OpenForReadResult;
|
|
||||||
import org.apache.cordova.LOG;
|
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
|
||||||
import android.net.Uri;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.webkit.WebResourceResponse;
|
|
||||||
import android.webkit.WebView;
|
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
|
||||||
public class IceCreamCordovaWebViewClient extends AndroidWebViewClient {
|
|
||||||
|
|
||||||
private static final String TAG = "IceCreamCordovaWebViewClient";
|
|
||||||
|
|
||||||
public IceCreamCordovaWebViewClient(CordovaInterface cordova, AndroidWebView view) {
|
|
||||||
super(cordova, view);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
|
||||||
try {
|
|
||||||
// Check the against the whitelist and lock out access to the WebView directory
|
|
||||||
// Changing this will cause problems for your application
|
|
||||||
if (!appView.getPluginManager().shouldAllowRequest(url)) {
|
|
||||||
LOG.w(TAG, "URL blocked by whitelist: " + url);
|
|
||||||
// Results in a 404.
|
|
||||||
return new WebResourceResponse("text/plain", "UTF-8", null);
|
|
||||||
}
|
|
||||||
|
|
||||||
CordovaResourceApi resourceApi = appView.getResourceApi();
|
|
||||||
Uri origUri = Uri.parse(url);
|
|
||||||
// Allow plugins to intercept WebView requests.
|
|
||||||
Uri remappedUri = resourceApi.remapUri(origUri);
|
|
||||||
|
|
||||||
if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) {
|
|
||||||
OpenForReadResult result = resourceApi.openForRead(remappedUri, true);
|
|
||||||
return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream);
|
|
||||||
}
|
|
||||||
// If we don't need to special-case the request, let the browser load it.
|
|
||||||
return null;
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (!(e instanceof FileNotFoundException)) {
|
|
||||||
LOG.e("IceCreamCordovaWebViewClient", "Error occurred while loading a file (returning a 404).", e);
|
|
||||||
}
|
|
||||||
// Results in a 404.
|
|
||||||
return new WebResourceResponse("text/plain", "UTF-8", null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean needsKitKatContentUrlFix(Uri uri) {
|
|
||||||
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && "content".equals(uri.getScheme());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean needsSpecialsInAssetUrlFix(Uri uri) {
|
|
||||||
if (CordovaResourceApi.getUriType(uri) != CordovaResourceApi.URI_TYPE_ASSET) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (uri.getQuery() != null || uri.getFragment() != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!uri.toString().contains("%")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(android.os.Build.VERSION.SDK_INT){
|
|
||||||
case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH:
|
|
||||||
case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user