Search in sources :

Example 41 with WebChromeClient

use of android.webkit.WebChromeClient in project SmartCampus by Vegen.

the class TopFunArticleDetailActivity method initData.

@Override
protected void initData() {
    webView.loadUrl(url);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100 && refreshLayout != null) {
                refreshLayout.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        refreshLayout.finishRefreshing();
                        refreshLayout.setEnableRefresh(false);
                        refreshLayout.setEnabled(false);
                        refreshLayout.setHeaderHeight(0);
                    }
                }, 600);
            }
        }
    });
}
Also used : WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 42 with WebChromeClient

use of android.webkit.WebChromeClient in project SmartCampus by Vegen.

the class ArticleDetailActivity method hideLoading.

@Override
public void hideLoading(boolean isFail) {
    super.hideLoading(isFail);
    if (isFail) {
        if (refreshLayout == null)
            return;
        refreshLayout.postDelayed(() -> {
            refreshLayout.finishRefreshing();
            refreshLayout.setEnableRefresh(false);
        }, 600);
    } else {
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100 && refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.setEnableRefresh(false);
                }
            }
        });
    }
}
Also used : WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView)

Example 43 with WebChromeClient

use of android.webkit.WebChromeClient in project WeexErosFramework by bmfe.

the class BMChart method initComponentHostView.

@Override
protected View initComponentHostView(@NonNull Context context) {
    View view = mWebView.getView();
    mWeb = mWebView.getWebView();
    view.setBackgroundColor(Color.TRANSPARENT);
    WebSettings settings = mWeb.getSettings();
    settings.setJavaScriptEnabled(true);
    mWebView.setOnPageListener(this);
    s = new Date();
    Log.e("bmChart", "start" + s.getTime());
    mWeb.loadUrl("file:///android_asset/bm-chart.html");
    mWeb.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            return false;
        }

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            String message = consoleMessage.message();
            Log.e("onConsoleMessage", ">>>>>>" + message);
            return super.onConsoleMessage(consoleMessage);
        }
    });
    return view;
}
Also used : JsResult(android.webkit.JsResult) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebView(android.webkit.WebView) IWebView(com.taobao.weex.ui.view.IWebView) WXWebView(com.taobao.weex.ui.view.WXWebView) View(android.view.View) WebView(android.webkit.WebView) IWebView(com.taobao.weex.ui.view.IWebView) WXWebView(com.taobao.weex.ui.view.WXWebView) Date(java.util.Date) ConsoleMessage(android.webkit.ConsoleMessage)

Example 44 with WebChromeClient

use of android.webkit.WebChromeClient in project openremote by openremote.

the class MainActivity method initializeWebView.

protected void initializeWebView() {
    LOG.fine("Initializing web view");
    final WebAppInterface webAppInterface = new WebAppInterface(this);
    webView.addJavascriptInterface(webAppInterface, "MobileInterface");
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setDomStorageEnabled(true);
    webSettings.setDatabaseEnabled(true);
    webView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            return true;
        }
    });
    webView.setLongClickable(false);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            // TODO should we ignore images?
            if (request.getUrl().getLastPathSegment() != null && (request.getUrl().getLastPathSegment().endsWith("png") || request.getUrl().getLastPathSegment().endsWith("jpg") || request.getUrl().getLastPathSegment().endsWith("ico")))
                return;
            // invalid token. The web app will then start a new login.
            if (request.getUrl().getLastPathSegment() != null && request.getUrl().getLastPathSegment().equals("token") && request.getMethod().equals("POST") && errorResponse.getStatusCode() == 400) {
                webAppInterface.tokenService.clearToken();
                return;
            }
            LOG.warning("Error requesting '" + request.getUrl() + "', response code: " + errorResponse.getStatusCode());
            errorViewHolder.show(R.string.httpError, R.string.httpErrorExplain, true, true);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (Boolean.parseBoolean(getString(R.string.SSL_IGNORE))) {
                LOG.fine("Ignoring SSL certificate error: " + error.getPrimaryError());
                // Ignore SSL certificate errors
                handler.proceed();
            } else {
                LOG.severe("SSL error: " + error.getPrimaryError());
                LOG.severe("SSL certificate: " + error.getCertificate());
                errorViewHolder.show(R.string.httpError, R.string.httpErrorExplain, true, true);
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            // TODO should we ignore images?
            if (request.getUrl().getLastPathSegment() != null && (request.getUrl().getLastPathSegment().endsWith("png") || request.getUrl().getLastPathSegment().endsWith("jpg") || request.getUrl().getLastPathSegment().endsWith("ico")))
                return;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // Remote debugging sessions from Chrome trigger "ERR_CACHE_MISS" that don't hurt, but we should not redirect the view
                if (isRemoteDebuggingEnabled() && error.getErrorCode() == ERROR_UNKNOWN) {
                    return;
                }
                // Remote debugging session from Chrome wants to load about:blank and then fails with "ERROR_UNSUPPORTED_SCHEME", ignore
                if (request.getUrl().toString().equals("about:blank") && error.getErrorCode() == ERROR_UNSUPPORTED_SCHEME) {
                    return;
                }
                LOG.warning("Error requesting '" + request.getUrl() + "': " + error.getErrorCode());
            }
            errorViewHolder.show(R.string.fatalError, R.string.fatalErrorExplain, false, true);
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
            String msg = "WebApp console (" + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber() + "): " + consoleMessage.message();
            switch(consoleMessage.messageLevel()) {
                case DEBUG:
                case TIP:
                    LOG.fine(msg);
                    break;
                case LOG:
                    LOG.info(msg);
                    break;
                default:
                    LOG.severe(msg);
            }
            return true;
        }
    });
    webView.setDownloadListener(new DownloadListener() {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
            String writePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
            if (ContextCompat.checkSelfPermission(context, writePermission) != PackageManager.PERMISSION_GRANTED) {
                // Location permission has not been granted yet, request it.
                ActivityCompat.requestPermissions((MainActivity) context, new String[] { writePermission }, WRITE_PERMISSION_REQUEST);
            } else {
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setMimeType(mimetype);
                // ------------------------COOKIE!!------------------------
                String cookies = CookieManager.getInstance().getCookie(url);
                request.addRequestHeader("cookie", cookies);
                // ------------------------COOKIE!!------------------------
                request.addRequestHeader("User-Agent", userAgent);
                request.setDescription("Downloading file...");
                request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
                DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                if (dm != null) {
                    Toast.makeText(getApplicationContext(), R.string.downloading_file, Toast.LENGTH_LONG).show();
                    dm.enqueue(request);
                } else {
                    Toast.makeText(getApplicationContext(), R.string.error_downloading, Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) WebResourceRequest(android.webkit.WebResourceRequest) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) DownloadManager(android.app.DownloadManager) ConsoleMessage(android.webkit.ConsoleMessage) WebResourceResponse(android.webkit.WebResourceResponse) DownloadListener(android.webkit.DownloadListener) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 45 with WebChromeClient

use of android.webkit.WebChromeClient in project Palm300Heroes by nicolite.

the class WebViewActivity method initWebView.

private void initWebView() {
    settings = webView.getSettings();
    settings.setLoadWithOverviewMode(true);
    // 使页面支持缩放
    settings.setBuiltInZoomControls(true);
    // 支持缩放
    settings.setSupportZoom(true);
    // 缩放按钮
    settings.setDisplayZoomControls(false);
    // 支持自动加载图片
    settings.setLoadsImagesAutomatically(true);
    // 开启DOM
    settings.setDomStorageEnabled(true);
    // 关闭webview中缓存
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setAppCacheEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setDefaultTextEncodingName("UTF-8");
    // 将图片调整到适合webview的大小
    settings.setUseWideViewPort(true);
    // //当webview调用requestFocus时为webview设置节点
    settings.setNeedInitialFocus(true);
    // 允许访问文件
    settings.setAllowFileAccess(true);
    // 支持缩放
    settings.setSupportZoom(true);
    settings.setPluginState(WebSettings.PluginState.ON);
    webView.setFocusable(true);
    webView.setFocusableInTouchMode(true);
    webView.addJavascriptInterface(new JsInject(this), "imageListener");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            if (s.startsWith("http") || s.startsWith("https")) {
                webView.loadUrl(s);
            } else if (s.startsWith("intent")) {
                try {
                    Intent intent = Intent.parseUri(s, Intent.URI_INTENT_SCHEME);
                    intent.addCategory("android.intent.category.BROWSABLE");
                    intent.setComponent(null);
                    intent.setSelector(null);
                    List<ResolveInfo> resolves = context.getPackageManager().queryIntentActivities(intent, 0);
                    if (resolves.size() > 0) {
                        startActivityIfNeeded(intent, -1);
                    }
                    return true;
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            }
            return super.shouldOverrideUrlLoading(webView, s);
        }

        @Override
        public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
            super.onPageStarted(webView, s, bitmap);
            progressBar.setProgress(20);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (view != null && toolbarTitle != null) {
                String title = view.getTitle();
                if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                    toolbarTitle.setText(title);
                }
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView webView, int i) {
            super.onProgressChanged(webView, i);
            if (progressBar != null) {
                if (i == 100) {
                    progressBar.setVisibility(View.GONE);
                    if (type != TYPE_VIDEO && type != TYPE_TY_CARTOON && type != TYPE_THEME_MUSIC) {
                        addImgClickListener();
                    }
                    if (type == TYPE_THEME_MUSIC) {
                        changThemeMusicDom();
                    }
                } else {
                    if (View.GONE == progressBar.getVisibility()) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    if (i <= 20) {
                        progressBar.setProgress(20);
                    } else {
                        progressBar.setProgress(i);
                    }
                }
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (!TextUtils.isEmpty(title) && !title.contains("about:blank")) {
                toolbarTitle.setText(title);
            }
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) Intent(android.content.Intent) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) JsInject(cn.nicolite.palm300heroes.injection.JsInject) ResolveInfo(android.content.pm.ResolveInfo) Bitmap(android.graphics.Bitmap) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Aggregations

WebChromeClient (android.webkit.WebChromeClient)45 WebView (android.webkit.WebView)38 WebViewClient (android.webkit.WebViewClient)32 WebSettings (android.webkit.WebSettings)15 View (android.view.View)14 Bitmap (android.graphics.Bitmap)10 WebResourceRequest (android.webkit.WebResourceRequest)8 JsResult (android.webkit.JsResult)7 SuppressLint (android.annotation.SuppressLint)6 Intent (android.content.Intent)6 WebResourceError (android.webkit.WebResourceError)6 LinearLayout (android.widget.LinearLayout)5 Uri (android.net.Uri)4 KeyEvent (android.view.KeyEvent)4 ConsoleMessage (android.webkit.ConsoleMessage)4 JsPromptResult (android.webkit.JsPromptResult)4 WebResourceResponse (android.webkit.WebResourceResponse)4 TextView (android.widget.TextView)3 InjectView (butterknife.InjectView)3 SharedPreferences (android.content.SharedPreferences)2