com.nxt.yn.app.ui.activity.WebViewUrlActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.nxt.yn.app.ui.activity.WebViewUrlActivity.java

Source

/*
 *  Copyright (C) 2015 MummyDing
 *
 *  This file is part of Leisure( <https://github.com/MummyDing/Leisure> )
 *
 *  Leisure is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *                             
 *  Leisure is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Leisure.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nxt.yn.app.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.LogUtils;
import com.hyphenate.easeui.utils.ZPreferenceUtils;
import com.nxt.yn.app.R;
import com.nxt.yn.app.base.BaseTitleActivity;
import com.nxt.yn.app.util.Constant;
import com.nxt.yn.app.util.Settings;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebViewUrlActivity extends BaseTitleActivity {
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout refreshLayout;
    protected boolean isLoading = true;
    protected int newsid;
    private boolean isfirsarload = true;

    @Override
    protected void initView() {
        initTopbar(this, getString(R.string.news_detail));
        ButterKnife.bind(this);
        initData();
        autorefresh();

    }

    @Override
    protected void initEvent() {

    }

    private void autorefresh() {
        refreshLayout.setRefreshing(true);
        handler.sendEmptyMessageDelayed(0, 1000);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_webview;
    }

    private void initData() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        });
        refreshLayout.setColorSchemeResources(R.color.title_color, R.color.title_color, R.color.title_color,
                R.color.title_color);

        newsid = getIntent().getIntExtra("newsid", 0);
        LogUtils.i("ssss", "newsid---------->" + newsid);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);

        /*  if(Settings.isNightMode) {
        webView.setBackgroundColor(ContextCompat.getColor(this, R.color.night_primary));
         }*/
        if (EaseCommonUtils.isWiFi(this) == false) {
            webView.getSettings()
                    .setBlockNetworkImage(ZPreferenceUtils.getPrefBoolean(Settings.NO_PIC_MODE, false));
        } else {
            // fix issue #13
            webView.getSettings().setBlockNetworkImage(false);
        }
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                refreshLayout.setRefreshing(false);
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                view.loadUrl("file:///android_asset/error.html");
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/error.html");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (isfirsarload) {
                    isfirsarload = false;
                    return false;
                } else {
                    startActivity(new Intent(WebViewUrlActivity.this, WebViewUrlActivity.class)
                            .putExtra("title", getString(R.string.app_name)).putExtra("url", url));
                }
                return true;
            }
        });

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:
                LogUtils.i("ssss", String.format(Constant.NEWS_DETAIL_URL, newsid));
                webView.loadUrl(String.format(Constant.NEWS_DETAIL_URL, newsid));
                break;
            }
            super.handleMessage(msg);
        }
    };
}