@@ -12,9 +12,14 @@ import android.webkit.WebView;
1212import android.webkit.WebViewClient;
1313import android.webkit.WebChromeClient;
1414import android.webkit.PermissionRequest;
15+import android.webkit.MimeTypeMap;
16+import android.webkit.WebResourceRequest;
17+import android.webkit.WebResourceResponse;
1518import android.net.Uri;
1619import android.annotation.TargetApi;
1720import android.os.Build;
21+import java.io.InputStream;
22+import java.io.IOException;
1823
1924import java.nio.charset.StandardCharsets;
2025
@@ -42,6 +47,30 @@ public class MainWidget {
4247 public void onPageFinished(WebView _view, String _url) {
4348 wv.evaluateJavascript(initialJS, null);
4449 }
50+
51+ // Re-route / to /android_asset
52+ public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request) {
53+ Uri uri = request.getUrl();
54+ if(!uri.getScheme().equals("file"))
55+ return null;
56+
57+ String path = uri.getPath();
58+ path = getAssetPath(path);
59+
60+ String mimeType = getMimeType(uri.toString());
61+ String encoding = "";
62+
63+ try {
64+ InputStream data = a.getApplicationContext().getAssets().open(path);
65+ return new WebResourceResponse(mimeType, encoding, data);
66+ }
67+ catch (IOException e) {
68+ Log.i("reflex", "Opening resource failed, Webview will handle the request ..");
69+ e.printStackTrace();
70+ }
71+
72+ return null;
73+ }
4574 });
4675
4776 wv.setWebChromeClient(new WebChromeClient() {
@@ -82,6 +111,22 @@ public class MainWidget {
82111 };
83112 }
84113
114+ private static String getMimeType(String url) {
115+ String type = "";
116+ String extension = MimeTypeMap.getFileExtensionFromUrl(url);
117+ if (extension != null) {
118+ type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
119+ }
120+ return type;
121+ }
122+
123+ /** Get the path of an asset. Strips leading / and leading /android_asset/ */
124+ private static String getAssetPath(String path) {
125+ path = path.startsWith("/android_asset") ? path.substring("/android_asset".length()) : path;
126+ path = path.startsWith("/") ? path.substring(1) : path;
127+ return path;
128+ }
129+
85130 private static class JSaddleCallbacks {
86131 private final long callbacks;
87132 private native void startProcessing(long callbacks);