package com.yutou.jianr_mg.views;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.alibaba.fastjson.JSONObject;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;


/**
 * Created by 58381 on 2018/3/14.
 */

public class WebActivity extends AppCompatActivity{
    private WebView webView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        initView();
       initWeb();
        String url=getIntent().getStringExtra("url");
        if(url==null) {
            if(getIntent().getStringExtra("parameter")==null) {
                url = "http://www.jianrmod.cn";
            }else{
                url= JSONObject.parseObject(getIntent().getStringExtra("parameter")).getString("url");
            }
        }
        Log.i("网页URL",url);
        try {
            webView.loadUrl(url);
        }catch (Exception e){
            e.printStackTrace();
           finish();
        }

    }
    private void initView(){
        webView=findViewById(R.id.webView);
    }
    private void initWeb(){
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setAppCacheEnabled(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

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

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.i("加载完成",url);
                WebActivity.this.setTitle(view.getTitle());
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.i("即将打开1" + request.getUrl());
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
