mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Use FileHelper in IceCreamCordovaWebViewClient.
Removes some duplicate logic and makes it a bit more robust.
This commit is contained in:
parent
791574c26e
commit
80fe4458c6
@ -24,11 +24,12 @@ import java.io.InputStream;
|
|||||||
import org.apache.cordova.api.CordovaInterface;
|
import org.apache.cordova.api.CordovaInterface;
|
||||||
import org.apache.cordova.api.LOG;
|
import org.apache.cordova.api.LOG;
|
||||||
|
|
||||||
import android.content.res.AssetManager;
|
import android.annotation.TargetApi;
|
||||||
import android.net.Uri;
|
import android.os.Build;
|
||||||
import android.webkit.WebResourceResponse;
|
import android.webkit.WebResourceResponse;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||||||
public class IceCreamCordovaWebViewClient extends CordovaWebViewClient {
|
public class IceCreamCordovaWebViewClient extends CordovaWebViewClient {
|
||||||
|
|
||||||
|
|
||||||
@ -43,34 +44,20 @@ public class IceCreamCordovaWebViewClient extends CordovaWebViewClient {
|
|||||||
@Override
|
@Override
|
||||||
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
||||||
if(url.contains("?") || url.contains("#") || needsIceCreamSpaceInAssetUrlFix(url)){
|
if(url.contains("?") || url.contains("#") || needsIceCreamSpaceInAssetUrlFix(url)){
|
||||||
return generateWebResourceResponse(url);
|
WebResourceResponse ret = generateWebResourceResponse(url);
|
||||||
} else {
|
if (ret != null) {
|
||||||
return super.shouldInterceptRequest(view, url);
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return super.shouldInterceptRequest(view, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private WebResourceResponse generateWebResourceResponse(String url) {
|
private WebResourceResponse generateWebResourceResponse(String url) {
|
||||||
final String ANDROID_ASSET = "file:///android_asset/";
|
if (url.startsWith("file:///android_asset/")) {
|
||||||
if (url.startsWith(ANDROID_ASSET)) {
|
String mimetype = FileHelper.getMimeType(url, cordova);
|
||||||
String niceUrl = url;
|
|
||||||
niceUrl = url.replaceFirst(ANDROID_ASSET, "");
|
|
||||||
if(niceUrl.contains("?")){
|
|
||||||
niceUrl = niceUrl.split("\\?")[0];
|
|
||||||
}
|
|
||||||
if(niceUrl.contains("#"))
|
|
||||||
{
|
|
||||||
niceUrl = niceUrl.split("#")[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
String mimetype = null;
|
|
||||||
if(niceUrl.endsWith(".html")){
|
|
||||||
mimetype = "text/html";
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
AssetManager assets = cordova.getActivity().getAssets();
|
InputStream stream = FileHelper.getInputStreamFromUriString(url, cordova);
|
||||||
Uri uri = Uri.parse(niceUrl);
|
|
||||||
InputStream stream = assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
|
|
||||||
WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream);
|
WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream);
|
||||||
return response;
|
return response;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user