Этот коммит содержится в:
Anton Tananaev
2020-10-07 12:00:47 -07:00
родитель 0efaa9b358
Коммит ba2a3121d4
+3 -73
Просмотреть файл
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,29 +22,20 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.MimeTypeMap;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebViewFragment;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
public class MainFragment extends WebViewFragment {
@@ -54,7 +45,6 @@ public class MainFragment extends WebViewFragment {
private final static int REQUEST_FILE_CHOOSER = 1;
private AssetManager assetManager;
private LocalBroadcastManager broadcastManager;
public class AppInterface {
@@ -71,7 +61,6 @@ public class MainFragment extends WebViewFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
assetManager = getActivity().getAssets();
broadcastManager = LocalBroadcastManager.getInstance(getActivity());
}
@@ -85,7 +74,6 @@ public class MainFragment extends WebViewFragment {
}
}
getWebView().setWebViewClient(webViewClient);
getWebView().setWebChromeClient(webChromeClient);
getWebView().addJavascriptInterface(new AppInterface(), "appInterface");
@@ -123,64 +111,6 @@ public class MainFragment extends WebViewFragment {
broadcastManager.unregisterReceiver(broadcastReceiver);
}
public String getMimeType(String url) {
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
switch (extension) {
case "js":
return "text/javascript";
case "woff":
return "application/font-woff";
case "woff2":
return "application/font-woff2";
case "ttf":
return "application/x-font-ttf";
case "eot":
return "application/vnd.ms-fontobject";
case "svg":
return "image/svg+xml";
default:
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
}
return null;
}
public WebResourceResponse loadFileFromAssets(String url, String file) throws IOException {
String mimeType = getMimeType(url);
String encoding = "UTF-8";
InputStream inputStream = assetManager.open(file);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int statusCode = HttpURLConnection.HTTP_OK;
String reasonPhase = "OK";
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put("Access-Control-Allow-Origin", "*");
return new WebResourceResponse(mimeType, encoding, statusCode, reasonPhase, responseHeaders, inputStream);
} else {
return new WebResourceResponse(mimeType, encoding, inputStream);
}
}
private WebViewClient webViewClient = new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Uri uri = Uri.parse(url);
String host = uri.getHost();
if (host != null && host.equals("cdnjs.cloudflare.com")) {
String path = uri.getPath().substring("/ajax/libs".length());
try {
return loadFileFromAssets(url, "cdnjs" + path);
} catch (IOException e) {
return null;
}
}
return null;
}
};
private ValueCallback<Uri> openFileCallback;
private ValueCallback<Uri[]> openFileCallback2;