import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
 
 
public class WebViewDialog extends Dialog {
 
    public WebViewDialog(Context context) {
        super(context);
        setContentView(R.layout.layout_webview);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        mWebView = (WebView) findViewById(R.id.web_view);
        mWebView.setWebViewClient(mWebViewClientBase);
        /*此处禁用JavaScript,而是在后面onAttachedToWindow事件中再启用JavaScript的支持,同时强制页面刷新
        * 这么处理的原因是由于前端同学在开发页面时候,会使用一个自有的JavaScript框架,这个框架会在页面
        * 加载完成后,立即获取页面的宽高,但是此时获取到的宽高都是 0 ,因为此时Chrome还没有完成整个页面的Layout
        * 因此我们需要在页面完成Layout后再次加载页面才可以,但是如果此处启用JavaScript的支持会导致埋点数据的意外
        * 上行,导致双份的埋点问题,因此,此处强制禁用JavaScript
        * */
        mWebView.getSettings().setJavaScriptEnabled(false);
        mWebView.addJavascriptInterface(new JsBridge(),"JsBridge");
    }
 
    @Override
    public void onAttachedToWindow() {
        /*此处我们呼应下面代码中禁用JavaScript的支持的部分代码
        * 原因也已经解释的非常详细了
        * 但是此处需要注意,就是先reload再次启用JavaScript这个顺序不要乱掉,否则
        * 可能还没有调用reload之前,前一个页面已经执行了JavaScript导致页面上面的埋点两次执行。
        *
        * 关于性能的隐忧,由于我们重新reload了页面,地址链接并没有改变,因此并不会去服务器上面重新获取页面
        * 此处的性能隐忧,应该是不存在的
        *
        * 至于是不是需要手工设置一下Chrome内核的缓存时间,这个在目前的实际实验观察看来,是不需要的。
        *
        * */
        mWebView.reload();
        mWebView.getSettings().setJavaScriptEnabled(true);
    }
 
 
    public void showWebView(){
        final String strHtml = "<html>" +
                "   <head>" +
                "       <script>" +
                "               window.JsBridge.jsGetWindowWithHeight(document.documentElement.clientWidth,document.documentElement.clientHeight);" +
                "       </script>" +
                "   </head>" +
                "</html>";
        mWebView.loadData(strHtml,"text/html","UTF-8");
    }
 
    public class JsBridge{
 
        @JavascriptInterface
        public final void jsGetWindowWithHeight(int aWidth,int aHeight) {
            Log.d("jsGetWindowWithHeight","Width=" + aWidth + " Height=" + aHeight);
            Toast.makeText(getContext(),"Width=" + aWidth + " Height=" + aHeight,Toast.LENGTH_LONG).show();
        }
    }
 
    private class WebViewClientBase extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            mHandler.removeCallbacks(mShowRunnable);
            mHandler.post(mShowRunnable);
        }
    }
 
    private final Runnable mShowRunnable = new Runnable() {
        @Override
        public void run() {
            WebViewDialog.this.show();
        }
    };
 
    private final WebViewClientBase mWebViewClientBase = new WebViewClientBase();
    private WebView mWebView;
    private static Handler mHandler = new Handler(Looper.getMainLooper());
}