Search in sources :

Example 1 with NestedScrollWebView

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");
}
Also used : Message(android.os.Message) NestedScrollWebView(me.devsaki.hentoid.views.NestedScrollWebView) Handler(android.os.Handler) SuppressLint(android.annotation.SuppressLint) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) Resources(android.content.res.Resources) NestedScrollWebView(me.devsaki.hentoid.views.NestedScrollWebView) WebView(android.webkit.WebView) CookieManager(android.webkit.CookieManager) SuppressLint(android.annotation.SuppressLint)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 Handler (android.os.Handler)1 Message (android.os.Message)1 CookieManager (android.webkit.CookieManager)1 WebChromeClient (android.webkit.WebChromeClient)1 WebSettings (android.webkit.WebSettings)1 WebView (android.webkit.WebView)1 NestedScrollWebView (me.devsaki.hentoid.views.NestedScrollWebView)1