use of me.devsaki.hentoid.views.NestedScrollWebView in project Hentoid by avluis.
the class BaseWebActivity method initUI.
@SuppressLint("SetJavaScriptEnabled")
private void initUI() {
try {
webView = new NestedScrollWebView(this);
} catch (Resources.NotFoundException e) {
// Some older devices can crash when instantiating a WebView, due to a Resources$NotFoundException
// Creating with the application Context fixes this, but is not generally recommended for view creation
webView = new NestedScrollWebView(Helper.getFixedContext(this));
}
webView.setHapticFeedbackEnabled(false);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
swipeLayout.post(() -> swipeLayout.setRefreshing(false));
} else {
swipeLayout.post(() -> swipeLayout.setRefreshing(true));
}
}
});
boolean bWebViewOverview = Preferences.getWebViewOverview();
int webViewInitialZoom = Preferences.getWebViewInitialZoom();
if (bWebViewOverview) {
webView.getSettings().setLoadWithOverviewMode(false);
webView.setInitialScale(webViewInitialZoom);
Timber.d("WebView Initial Scale: %s%%", webViewInitialZoom);
} else {
webView.setInitialScale(Preferences.Default.WEBVIEW_INITIAL_ZOOM);
webView.getSettings().setLoadWithOverviewMode(true);
}
if (BuildConfig.DEBUG)
WebView.setWebContentsDebuggingEnabled(true);
webClient = getWebClient();
webView.setWebViewClient(webClient);
// Download immediately on long click on a link / image link
if (Preferences.isBrowserQuickDl()) {
webView.setOnLongClickListener(v -> {
WebView.HitTestResult result = webView.getHitTestResult();
String url = "";
// Plain link
if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE && result.getExtra() != null)
url = result.getExtra();
// Image link (https://stackoverflow.com/a/55299801/8374722)
if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
Handler handler = new Handler(getMainLooper());
Message message = handler.obtainMessage();
webView.requestFocusNodeHref(message);
url = message.getData().getString("url");
}
if (url != null && !url.isEmpty() && webClient.isGalleryPage(url)) {
// Launch on a new thread to avoid crashes
webClient.parseResponseAsync(url);
return true;
} else {
return false;
}
});
webView.setLongClickThreshold(Preferences.getBrowserQuickDlThreshold());
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webView, true);
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
Timber.i("%s : using user-agent %s", getStartSite().name(), getStartSite().getUserAgent());
webSettings.setUserAgentString(getStartSite().getUserAgent());
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
if (fetchHandler != null)
webView.addJavascriptInterface(new FetchHandler(fetchHandler), "fetchHandler");
}
Aggregations