Search in sources :

Example 1 with WebViewClient

use of com.tencent.smtt.sdk.WebViewClient in project Ency by xiarunhao123.

the class WebActivity method initWebView.

@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
    WebSettings settings = webView.getSettings();
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setGeolocationEnabled(true);
    settings.setGeolocationDatabasePath(dir);
    if (sharePrefManager.getNightMode()) {
        webView.setDayOrNight(false);
    } else {
        webView.setDayOrNight(true);
    }
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String s) {
            return super.shouldOverrideUrlLoading(webView, s);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            swipeRefreshLayout.setRefreshing(true);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            swipeRefreshLayout.setRefreshing(false);
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, com.tencent.smtt.export.external.interfaces.SslError sslError) {
            sslErrorHandler.proceed();
        }
    });
    webView.setWebChromeClient(new WebChromeClient() {

        // 扩展支持alert事件
        @Override
        public boolean onJsAlert(WebView webView, String url, String message, final JsResult jsResult) {
            new MaterialDialog.Builder(mContext).title("提示").content(message).positiveText(R.string.yes).onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    jsResult.confirm();
                }
            }).cancelable(false).build().show();
            return true;
        }

        @Override
        public void onGeolocationPermissionsHidePrompt() {
            super.onGeolocationPermissionsHidePrompt();
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissionsCallback callback) {
            final boolean remember = false;
            new MaterialDialog.Builder(mContext).title("地理位置授权").content("允许" + origin + "获取您当前的地理位置信息吗").positiveText("允许").onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    callback.invoke(origin, true, remember);
                }
            }).negativeText("拒绝").negativeColorRes(R.color.black).onNegative(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    callback.invoke(origin, false, remember);
                }
            }).cancelable(false).build().show();
        }
    });
    webView.setOnScrollListener(new X5WebView.IScrollListener() {

        @Override
        public void onScrollChanged(int scrollY) {
            // webView在顶部
            if (scrollY == 0) {
                swipeRefreshLayout.setEnabled(true);
            } else // webView不是顶部
            {
                swipeRefreshLayout.setEnabled(false);
            }
        }
    });
    // 点击返回
    webView.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
                webView.goBack();
                return true;
            }
            return false;
        }
    });
    webView.loadUrl(url);
}
Also used : SslErrorHandler(com.tencent.smtt.export.external.interfaces.SslErrorHandler) GeolocationPermissionsCallback(com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback) JsResult(com.tencent.smtt.export.external.interfaces.JsResult) KeyEvent(android.view.KeyEvent) Bitmap(android.graphics.Bitmap) WebChromeClient(com.tencent.smtt.sdk.WebChromeClient) NonNull(android.support.annotation.NonNull) X5WebView(com.xxx.ency.widget.X5WebView) WebView(com.tencent.smtt.sdk.WebView) WebViewClient(com.tencent.smtt.sdk.WebViewClient) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) X5WebView(com.xxx.ency.widget.X5WebView) BindView(butterknife.BindView) View(android.view.View) WebView(com.tencent.smtt.sdk.WebView) SuppressLint(android.annotation.SuppressLint) X5WebView(com.xxx.ency.widget.X5WebView) WebSettings(com.tencent.smtt.sdk.WebSettings) DialogAction(com.afollestad.materialdialogs.DialogAction) SuppressLint(android.annotation.SuppressLint)

Example 2 with WebViewClient

use of com.tencent.smtt.sdk.WebViewClient in project GeekNews by codeestX.

the class ZhihuDetailActivity method initEventAndData.

@Override
protected void initEventAndData() {
    super.initEventAndData();
    setToolBar(viewToolbar, "");
    Intent intent = getIntent();
    id = intent.getExtras().getInt(Constants.IT_ZHIHU_DETAIL_ID);
    isNotTransition = intent.getBooleanExtra("isNotTransition", false);
    mPresenter.queryLikeData(id);
    mPresenter.getDetailData(id);
    mPresenter.getExtraData(id);
    stateLoading();
    WebSettings settings = wvDetailContent.getSettings();
    if (mPresenter.getNoImageState()) {
        settings.setBlockNetworkImage(true);
    }
    if (mPresenter.getAutoCacheState()) {
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        if (SystemUtil.isNetworkConnected()) {
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        } else {
            settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
        }
    }
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setSupportZoom(true);
    wvDetailContent.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    nsvScroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

        @Override
        public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            if (scrollY - oldScrollY > 0 && isBottomShow) {
                // 下移隐藏
                isBottomShow = false;
                llDetailBottom.animate().translationY(llDetailBottom.getHeight());
            } else if (scrollY - oldScrollY < 0 && !isBottomShow) {
                // 上移出现
                isBottomShow = true;
                llDetailBottom.animate().translationY(0);
            }
        }
    });
    (getWindow().getSharedElementEnterTransition()).addListener(new Transition.TransitionListener() {

        @Override
        public void onTransitionStart(Transition transition) {
        }

        @Override
        public void onTransitionEnd(Transition transition) {
            /**
             * 测试发现部分手机(如红米note2)上加载图片会变形,没有达到centerCrop效果
             * 查阅资料发现Glide配合SharedElementTransition是有坑的,需要在Transition动画结束后再加载图片
             * https://github.com/TWiStErRob/glide-support/blob/master/src/glide3/java/com/bumptech/glide/supportapp/github/_847_shared_transition/DetailFragment.java
             */
            isTransitionEnd = true;
            if (imgUrl != null) {
                isImageShow = true;
                ImageLoader.load(mContext, imgUrl, detailBarImage);
            }
        }

        @Override
        public void onTransitionCancel(Transition transition) {
        }

        @Override
        public void onTransitionPause(Transition transition) {
        }

        @Override
        public void onTransitionResume(Transition transition) {
        }
    });
}
Also used : WebSettings(com.tencent.smtt.sdk.WebSettings) Transition(android.transition.Transition) Intent(android.content.Intent) WebView(com.tencent.smtt.sdk.WebView) NestedScrollView(android.support.v4.widget.NestedScrollView) WebViewClient(com.tencent.smtt.sdk.WebViewClient)

Example 3 with WebViewClient

use of com.tencent.smtt.sdk.WebViewClient in project GeekNews by codeestX.

the class TechDetailActivity method initEventAndData.

@Override
protected void initEventAndData() {
    mRealmHelper = App.getAppComponent().realmHelper();
    mImplPreferencesHelper = App.getAppComponent().preferencesHelper();
    Intent intent = getIntent();
    type = intent.getExtras().getInt(Constants.IT_GANK_DETAIL_TYPE);
    title = intent.getExtras().getString(Constants.IT_GANK_DETAIL_TITLE);
    url = intent.getExtras().getString(Constants.IT_GANK_DETAIL_URL);
    imgUrl = intent.getExtras().getString(Constants.IT_GANK_DETAIL_IMG_URL);
    id = intent.getExtras().getString(Constants.IT_GANK_DETAIL_ID);
    setToolBar(toolBar, title);
    WebSettings settings = wvTechContent.getSettings();
    if (mImplPreferencesHelper.getNoImageState()) {
        settings.setBlockNetworkImage(true);
    }
    if (mImplPreferencesHelper.getAutoCacheState()) {
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        if (SystemUtil.isNetworkConnected()) {
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        } else {
            settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
        }
    }
    settings.setJavaScriptEnabled(true);
    settings.setLoadWithOverviewMode(true);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setSupportZoom(true);
    wvTechContent.setWebViewClient(new WebViewClient() {

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

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (tvProgress == null)
                return;
            if (newProgress == 100) {
                tvProgress.setVisibility(View.GONE);
            } else {
                tvProgress.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams lp = tvProgress.getLayoutParams();
                lp.width = App.SCREEN_WIDTH * newProgress / 100;
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            setTitle(title);
        }
    });
    wvTechContent.loadUrl(url);
}
Also used : WebSettings(com.tencent.smtt.sdk.WebSettings) WebChromeClient(com.tencent.smtt.sdk.WebChromeClient) Intent(android.content.Intent) WebView(com.tencent.smtt.sdk.WebView) WebViewClient(com.tencent.smtt.sdk.WebViewClient)

Aggregations

WebSettings (com.tencent.smtt.sdk.WebSettings)3 WebView (com.tencent.smtt.sdk.WebView)3 WebViewClient (com.tencent.smtt.sdk.WebViewClient)3 Intent (android.content.Intent)2 WebChromeClient (com.tencent.smtt.sdk.WebChromeClient)2 SuppressLint (android.annotation.SuppressLint)1 Bitmap (android.graphics.Bitmap)1 NonNull (android.support.annotation.NonNull)1 NestedScrollView (android.support.v4.widget.NestedScrollView)1 Transition (android.transition.Transition)1 KeyEvent (android.view.KeyEvent)1 View (android.view.View)1 BindView (butterknife.BindView)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 GeolocationPermissionsCallback (com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback)1 JsResult (com.tencent.smtt.export.external.interfaces.JsResult)1 SslErrorHandler (com.tencent.smtt.export.external.interfaces.SslErrorHandler)1 X5WebView (com.xxx.ency.widget.X5WebView)1