|
| 1 | +package com.king.base.fragment; |
| 2 | + |
| 3 | +import android.graphics.Bitmap; |
| 4 | +import android.net.http.SslError; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.text.TextUtils; |
| 7 | +import android.view.KeyEvent; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.webkit.SslErrorHandler; |
| 11 | +import android.webkit.WebChromeClient; |
| 12 | +import android.webkit.WebResourceError; |
| 13 | +import android.webkit.WebResourceRequest; |
| 14 | +import android.webkit.WebSettings; |
| 15 | +import android.webkit.WebView; |
| 16 | +import android.webkit.WebViewClient; |
| 17 | +import android.widget.LinearLayout; |
| 18 | +import android.widget.ProgressBar; |
| 19 | + |
| 20 | +import com.king.base.BaseFragment; |
| 21 | +import com.king.base.R; |
| 22 | +import com.king.base.model.EventMessage; |
| 23 | +import com.king.base.util.LogUtils; |
| 24 | +import com.king.base.util.SystemUtils; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Jenly <a href="mailto:[email protected]">Jenly</a> |
| 28 | + */ |
| 29 | +public class WebFragment extends BaseFragment{ |
| 30 | + |
| 31 | + public static final int OPR_WEBVIEW_BACK = 0x01; |
| 32 | + |
| 33 | + protected ProgressBar progressBar; |
| 34 | + |
| 35 | + protected LinearLayout vError; |
| 36 | + |
| 37 | + protected WebView webView; |
| 38 | + |
| 39 | + protected String url; |
| 40 | + |
| 41 | + protected boolean isError; |
| 42 | + |
| 43 | + private boolean isShowError; |
| 44 | + |
| 45 | + public static WebFragment newInstance(String url) { |
| 46 | + |
| 47 | + Bundle args = new Bundle(); |
| 48 | + args.putString(KEY_URL,url); |
| 49 | + WebFragment fragment = new WebFragment(); |
| 50 | + fragment.url = url; |
| 51 | + fragment.setArguments(args); |
| 52 | + return fragment; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int inflaterRootView() { |
| 57 | + return R.layout.fragment_web; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void initUI() { |
| 62 | + rootView.setFocusable(true); |
| 63 | + rootView.setFocusableInTouchMode(true); |
| 64 | + |
| 65 | + progressBar = findView(R.id.progressBar); |
| 66 | + progressBar.setMax(100); |
| 67 | + webView = findView(R.id.webView); |
| 68 | + vError = findView(R.id.vError); |
| 69 | + |
| 70 | + isShowError = addErrorView(vError); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void addListeners() { |
| 75 | + |
| 76 | + rootView.setOnKeyListener(new View.OnKeyListener() { |
| 77 | + @Override |
| 78 | + public boolean onKey(View view, int i, KeyEvent keyEvent) { |
| 79 | + |
| 80 | + if(keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK){ |
| 81 | + |
| 82 | + if(isGoBack()){ |
| 83 | + webView.goBack(); |
| 84 | + }else{ |
| 85 | + finish(); |
| 86 | + } |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void initData() { |
| 97 | + WebSettings ws = webView.getSettings(); |
| 98 | + //是否允许脚本支持 |
| 99 | + ws.setJavaScriptEnabled(true); |
| 100 | + |
| 101 | + |
| 102 | + ws.setJavaScriptCanOpenWindowsAutomatically(true); |
| 103 | + |
| 104 | + ws.setSaveFormData(false); |
| 105 | +// ws.setAppCacheEnabled(false); |
| 106 | + ws.setCacheMode(WebSettings.LOAD_NO_CACHE); |
| 107 | + |
| 108 | + |
| 109 | + webView.setHorizontalScrollBarEnabled(false); |
| 110 | + |
| 111 | +// webView.addJavascriptInterface(new WebJavascriptInterface(),"android"); |
| 112 | + |
| 113 | + String str = getIntent().getStringExtra(KEY_URL); |
| 114 | + if(!TextUtils.isEmpty(str)){ |
| 115 | + this.url = str; |
| 116 | + } |
| 117 | + |
| 118 | + webView.setWebChromeClient(new WebChromeClient(){ |
| 119 | + @Override |
| 120 | + public void onProgressChanged(WebView view, int newProgress) { |
| 121 | + super.onProgressChanged(view, newProgress); |
| 122 | + updateProgressBar(newProgress); |
| 123 | + } |
| 124 | + }); |
| 125 | + webView.setWebViewClient(getWebViewClient()); |
| 126 | + |
| 127 | + load(webView,url); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + public WebViewClient getWebViewClient(){ |
| 132 | + return new WebViewClient(){ |
| 133 | + |
| 134 | + @Override |
| 135 | + public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 136 | + super.onPageStarted(view, url, favicon); |
| 137 | + LogUtils.d("startUrl:" + url); |
| 138 | + isError = false; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 143 | + LogUtils.d("url:" + url); |
| 144 | + |
| 145 | + return super.shouldOverrideUrlLoading(view,url); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { |
| 152 | + super.onReceivedError(view, request, error); |
| 153 | + updateProgressBar(100); |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + @Override |
| 158 | + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| 159 | + super.onReceivedError(view, errorCode, description, failingUrl); |
| 160 | + LogUtils.w("errorCode:" + errorCode + "|failingUrl:" + failingUrl); |
| 161 | + showReceiveError(); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
| 167 | + super.onReceivedSslError(view, handler, error); |
| 168 | + handler.cancel(); |
| 169 | + handler.proceed(); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void onPageFinished(WebView view, String url) { |
| 174 | + super.onPageFinished(view, url); |
| 175 | + hideReceiveError(); |
| 176 | + } |
| 177 | + }; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * |
| 182 | + * @param group |
| 183 | + * @return true表示已添加ErrorView并显示ErrorView/false表示不处理 |
| 184 | + */ |
| 185 | + public boolean addErrorView(ViewGroup group){ |
| 186 | + |
| 187 | + return false; |
| 188 | + } |
| 189 | + |
| 190 | + private void showReceiveError(){ |
| 191 | + isError = true; |
| 192 | + if(SystemUtils.isNetWorkActive(context)){ |
| 193 | + LogUtils.w("Page loading failed."); |
| 194 | + }else{ |
| 195 | + LogUtils.w("Network unavailable."); |
| 196 | + } |
| 197 | + |
| 198 | + if(isShowError){ |
| 199 | + vError.setVisibility(View.VISIBLE); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + private void hideReceiveError(){ |
| 206 | + if(isError){ |
| 207 | + showReceiveError(); |
| 208 | + }else{ |
| 209 | + vError.setVisibility(View.GONE); |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * 加载url |
| 216 | + * @param webView |
| 217 | + * @param url |
| 218 | + */ |
| 219 | + private void load(WebView webView,String url){ |
| 220 | + LogUtils.d("url:" + url); |
| 221 | + if(!TextUtils.isEmpty(url)){ |
| 222 | + webView.loadUrl(url); |
| 223 | + } |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + private boolean isGoBack(){ |
| 228 | + return webView!=null && webView.canGoBack(); |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | + private void updateProgressBar(int progress){ |
| 233 | + updateProgressBar(true,progress); |
| 234 | + } |
| 235 | + |
| 236 | + |
| 237 | + private void updateProgressBar(boolean isVisibility,int progress){ |
| 238 | + |
| 239 | + progressBar.setVisibility((isVisibility && progress<100) ? View.VISIBLE : View.GONE); |
| 240 | + progressBar.setProgress(progress); |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + @Override |
| 245 | + public void onEventMessage(EventMessage em) { |
| 246 | + if(isStop){ |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + switch (em.what){ |
| 251 | + case OPR_WEBVIEW_BACK: |
| 252 | + if(isGoBack()){ |
| 253 | + webView.goBack(); |
| 254 | + }else{ |
| 255 | + finish(); |
| 256 | + } |
| 257 | + break; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | +} |
| 262 | + |
0 commit comments