ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidsystemimpl/webview/BaseWebView.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.androidsystemimpl.webview; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.annotation.SuppressLint; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.content.Context; |
| TMBOY | 45:2aa9f933c8d2 | 20 | import android.graphics.Bitmap; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import android.net.http.SslError; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import android.os.Build; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import android.util.AttributeSet; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import android.webkit.CookieManager; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import android.webkit.CookieSyncManager; |
| TMBOY | 45:2aa9f933c8d2 | 26 | import android.webkit.DownloadListener; |
| TMBOY | 45:2aa9f933c8d2 | 27 | import android.webkit.SslErrorHandler; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import android.webkit.WebSettings; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import android.webkit.WebSettings.PluginState; |
| TMBOY | 45:2aa9f933c8d2 | 30 | import android.webkit.WebView; |
| TMBOY | 45:2aa9f933c8d2 | 31 | import android.webkit.WebViewClient; |
| TMBOY | 45:2aa9f933c8d2 | 32 | |
| TMBOY | 45:2aa9f933c8d2 | 33 | import com.baidu.duer.dcs.systeminterface.IWebView; |
| TMBOY | 45:2aa9f933c8d2 | 34 | |
| TMBOY | 45:2aa9f933c8d2 | 35 | import java.io.UnsupportedEncodingException; |
| TMBOY | 45:2aa9f933c8d2 | 36 | import java.net.URLDecoder; |
| TMBOY | 45:2aa9f933c8d2 | 37 | import java.util.ArrayList; |
| TMBOY | 45:2aa9f933c8d2 | 38 | import java.util.Collections; |
| TMBOY | 45:2aa9f933c8d2 | 39 | import java.util.List; |
| TMBOY | 45:2aa9f933c8d2 | 40 | |
| TMBOY | 45:2aa9f933c8d2 | 41 | /** |
| TMBOY | 45:2aa9f933c8d2 | 42 | * common webView |
| TMBOY | 45:2aa9f933c8d2 | 43 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 44 | * Created by zhangyan42@baidu.com on 2017/5/19. |
| TMBOY | 45:2aa9f933c8d2 | 45 | */ |
| TMBOY | 45:2aa9f933c8d2 | 46 | @SuppressLint("SetJavaScriptEnabled") |
| TMBOY | 45:2aa9f933c8d2 | 47 | public class BaseWebView extends WebView implements IWebView { |
| TMBOY | 45:2aa9f933c8d2 | 48 | private final List<IWebViewListener> webViewListeners = |
| TMBOY | 45:2aa9f933c8d2 | 49 | Collections.synchronizedList(new ArrayList<IWebViewListener>()); |
| TMBOY | 45:2aa9f933c8d2 | 50 | |
| TMBOY | 45:2aa9f933c8d2 | 51 | public BaseWebView(Context context, AttributeSet attrs) { |
| TMBOY | 45:2aa9f933c8d2 | 52 | super(context, attrs); |
| TMBOY | 45:2aa9f933c8d2 | 53 | init(context); |
| TMBOY | 45:2aa9f933c8d2 | 54 | } |
| TMBOY | 45:2aa9f933c8d2 | 55 | |
| TMBOY | 45:2aa9f933c8d2 | 56 | public BaseWebView(Context context, AttributeSet attrs, int defStyle) { |
| TMBOY | 45:2aa9f933c8d2 | 57 | super(context, attrs, defStyle); |
| TMBOY | 45:2aa9f933c8d2 | 58 | init(context); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } |
| TMBOY | 45:2aa9f933c8d2 | 60 | |
| TMBOY | 45:2aa9f933c8d2 | 61 | public BaseWebView(Context context) { |
| TMBOY | 45:2aa9f933c8d2 | 62 | super(context); |
| TMBOY | 45:2aa9f933c8d2 | 63 | init(context); |
| TMBOY | 45:2aa9f933c8d2 | 64 | } |
| TMBOY | 45:2aa9f933c8d2 | 65 | |
| TMBOY | 45:2aa9f933c8d2 | 66 | @SuppressWarnings("deprecation") |
| TMBOY | 45:2aa9f933c8d2 | 67 | @SuppressLint("NewApi") |
| TMBOY | 45:2aa9f933c8d2 | 68 | private void init(Context context) { |
| TMBOY | 45:2aa9f933c8d2 | 69 | this.setVerticalScrollBarEnabled(false); |
| TMBOY | 45:2aa9f933c8d2 | 70 | this.setHorizontalScrollBarEnabled(false); |
| TMBOY | 45:2aa9f933c8d2 | 71 | if (Build.VERSION.SDK_INT < 19) { |
| TMBOY | 45:2aa9f933c8d2 | 72 | removeJavascriptInterface("searchBoxJavaBridge_"); |
| TMBOY | 45:2aa9f933c8d2 | 73 | } |
| TMBOY | 45:2aa9f933c8d2 | 74 | |
| TMBOY | 45:2aa9f933c8d2 | 75 | WebSettings localWebSettings = this.getSettings(); |
| TMBOY | 45:2aa9f933c8d2 | 76 | try { |
| TMBOY | 45:2aa9f933c8d2 | 77 | // 禁用file协议,http://www.tuicool.com/articles/Q36ZfuF, 防止Android WebView File域攻击 |
| TMBOY | 45:2aa9f933c8d2 | 78 | localWebSettings.setAllowFileAccess(false); |
| TMBOY | 45:2aa9f933c8d2 | 79 | localWebSettings.setSupportZoom(false); |
| TMBOY | 45:2aa9f933c8d2 | 80 | localWebSettings.setBuiltInZoomControls(false); |
| TMBOY | 45:2aa9f933c8d2 | 81 | localWebSettings.setUseWideViewPort(true); |
| TMBOY | 45:2aa9f933c8d2 | 82 | localWebSettings.setDomStorageEnabled(true); |
| TMBOY | 45:2aa9f933c8d2 | 83 | localWebSettings.setLoadWithOverviewMode(true); |
| TMBOY | 45:2aa9f933c8d2 | 84 | localWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); |
| TMBOY | 45:2aa9f933c8d2 | 85 | localWebSettings.setPluginState(PluginState.ON); |
| TMBOY | 45:2aa9f933c8d2 | 86 | // 启用数据库 |
| TMBOY | 45:2aa9f933c8d2 | 87 | localWebSettings.setDatabaseEnabled(true); |
| TMBOY | 45:2aa9f933c8d2 | 88 | // 设置定位的数据库路径 |
| TMBOY | 45:2aa9f933c8d2 | 89 | String dir = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); |
| TMBOY | 45:2aa9f933c8d2 | 90 | localWebSettings.setGeolocationDatabasePath(dir); |
| TMBOY | 45:2aa9f933c8d2 | 91 | localWebSettings.setGeolocationEnabled(true); |
| TMBOY | 45:2aa9f933c8d2 | 92 | localWebSettings.setJavaScriptEnabled(true); |
| TMBOY | 45:2aa9f933c8d2 | 93 | localWebSettings.setSavePassword(false); |
| TMBOY | 45:2aa9f933c8d2 | 94 | String agent = localWebSettings.getUserAgentString(); |
| TMBOY | 45:2aa9f933c8d2 | 95 | |
| TMBOY | 45:2aa9f933c8d2 | 96 | localWebSettings.setUserAgentString(agent); |
| TMBOY | 45:2aa9f933c8d2 | 97 | // setCookie(context, ".baidu.com", bdussCookie); |
| TMBOY | 45:2aa9f933c8d2 | 98 | |
| TMBOY | 45:2aa9f933c8d2 | 99 | } catch (Exception e1) { |
| TMBOY | 45:2aa9f933c8d2 | 100 | e1.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 101 | } |
| TMBOY | 45:2aa9f933c8d2 | 102 | this.setWebViewClient(new BridgeWebViewClient()); |
| TMBOY | 45:2aa9f933c8d2 | 103 | } |
| TMBOY | 45:2aa9f933c8d2 | 104 | |
| TMBOY | 45:2aa9f933c8d2 | 105 | private void setCookie(Context context, String domain, String sessionCookie) { |
| TMBOY | 45:2aa9f933c8d2 | 106 | CookieSyncManager.createInstance(context); |
| TMBOY | 45:2aa9f933c8d2 | 107 | CookieManager cookieManager = CookieManager.getInstance(); |
| TMBOY | 45:2aa9f933c8d2 | 108 | cookieManager.setAcceptCookie(true); |
| TMBOY | 45:2aa9f933c8d2 | 109 | if (sessionCookie != null) { |
| TMBOY | 45:2aa9f933c8d2 | 110 | // delete old cookies |
| TMBOY | 45:2aa9f933c8d2 | 111 | cookieManager.removeSessionCookie(); |
| TMBOY | 45:2aa9f933c8d2 | 112 | } |
| TMBOY | 45:2aa9f933c8d2 | 113 | try { |
| TMBOY | 45:2aa9f933c8d2 | 114 | Thread.sleep(200); |
| TMBOY | 45:2aa9f933c8d2 | 115 | } catch (InterruptedException e) { |
| TMBOY | 45:2aa9f933c8d2 | 116 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 117 | } |
| TMBOY | 45:2aa9f933c8d2 | 118 | cookieManager.setCookie(domain, sessionCookie); |
| TMBOY | 45:2aa9f933c8d2 | 119 | |
| TMBOY | 45:2aa9f933c8d2 | 120 | CookieSyncManager.createInstance(context); |
| TMBOY | 45:2aa9f933c8d2 | 121 | CookieSyncManager.getInstance().sync(); |
| TMBOY | 45:2aa9f933c8d2 | 122 | } |
| TMBOY | 45:2aa9f933c8d2 | 123 | |
| TMBOY | 45:2aa9f933c8d2 | 124 | public void setDownloadListener(DownloadListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 125 | super.setDownloadListener(listener); |
| TMBOY | 45:2aa9f933c8d2 | 126 | } |
| TMBOY | 45:2aa9f933c8d2 | 127 | |
| TMBOY | 45:2aa9f933c8d2 | 128 | private WebViewClientListener webViewClientListen; |
| TMBOY | 45:2aa9f933c8d2 | 129 | |
| TMBOY | 45:2aa9f933c8d2 | 130 | public void setWebViewClientListen(WebViewClientListener webViewClientListen) { |
| TMBOY | 45:2aa9f933c8d2 | 131 | this.webViewClientListen = webViewClientListen; |
| TMBOY | 45:2aa9f933c8d2 | 132 | } |
| TMBOY | 45:2aa9f933c8d2 | 133 | |
| TMBOY | 45:2aa9f933c8d2 | 134 | /** |
| TMBOY | 45:2aa9f933c8d2 | 135 | * 枚举网络加载返回状态 STATUS_FALSE:false |
| TMBOY | 45:2aa9f933c8d2 | 136 | * STATUS_TRUE:true |
| TMBOY | 45:2aa9f933c8d2 | 137 | * STATUS_UNKNOW:不知道 |
| TMBOY | 45:2aa9f933c8d2 | 138 | * NET_UNKNOWN:未知网络 |
| TMBOY | 45:2aa9f933c8d2 | 139 | */ |
| TMBOY | 45:2aa9f933c8d2 | 140 | public enum LoadingWebStatus { |
| TMBOY | 45:2aa9f933c8d2 | 141 | STATUS_FALSE, STATUS_TRUE, STATUS_UNKNOW |
| TMBOY | 45:2aa9f933c8d2 | 142 | } |
| TMBOY | 45:2aa9f933c8d2 | 143 | |
| TMBOY | 45:2aa9f933c8d2 | 144 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 145 | public void linkClicked(String url) { |
| TMBOY | 45:2aa9f933c8d2 | 146 | fireLinkClicked(url); |
| TMBOY | 45:2aa9f933c8d2 | 147 | } |
| TMBOY | 45:2aa9f933c8d2 | 148 | |
| TMBOY | 45:2aa9f933c8d2 | 149 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 150 | public void addWebViewListener(IWebViewListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 151 | this.webViewListeners.add(listener); |
| TMBOY | 45:2aa9f933c8d2 | 152 | } |
| TMBOY | 45:2aa9f933c8d2 | 153 | |
| TMBOY | 45:2aa9f933c8d2 | 154 | void fireLinkClicked(String url) { |
| TMBOY | 45:2aa9f933c8d2 | 155 | for (IWebViewListener listener : webViewListeners) { |
| TMBOY | 45:2aa9f933c8d2 | 156 | listener.onLinkClicked(url); |
| TMBOY | 45:2aa9f933c8d2 | 157 | } |
| TMBOY | 45:2aa9f933c8d2 | 158 | } |
| TMBOY | 45:2aa9f933c8d2 | 159 | |
| TMBOY | 45:2aa9f933c8d2 | 160 | public interface WebViewClientListener { |
| TMBOY | 45:2aa9f933c8d2 | 161 | LoadingWebStatus shouldOverrideUrlLoading(WebView view, String url); |
| TMBOY | 45:2aa9f933c8d2 | 162 | |
| TMBOY | 45:2aa9f933c8d2 | 163 | void onPageStarted(WebView view, String url, Bitmap favicon); |
| TMBOY | 45:2aa9f933c8d2 | 164 | |
| TMBOY | 45:2aa9f933c8d2 | 165 | void onPageFinished(WebView view, String url); |
| TMBOY | 45:2aa9f933c8d2 | 166 | |
| TMBOY | 45:2aa9f933c8d2 | 167 | void onReceivedError(WebView view, int errorCode, String description, String failingUrl); |
| TMBOY | 45:2aa9f933c8d2 | 168 | } |
| TMBOY | 45:2aa9f933c8d2 | 169 | |
| TMBOY | 45:2aa9f933c8d2 | 170 | public class BridgeWebViewClient extends WebViewClient { |
| TMBOY | 45:2aa9f933c8d2 | 171 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 172 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 173 | LoadingWebStatus loadWebStatus = LoadingWebStatus.STATUS_UNKNOW; |
| TMBOY | 45:2aa9f933c8d2 | 174 | String mUrl = url; |
| TMBOY | 45:2aa9f933c8d2 | 175 | try { |
| TMBOY | 45:2aa9f933c8d2 | 176 | mUrl = URLDecoder.decode(url, "UTF-8"); |
| TMBOY | 45:2aa9f933c8d2 | 177 | } catch (UnsupportedEncodingException e) { |
| TMBOY | 45:2aa9f933c8d2 | 178 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 179 | } catch (IllegalArgumentException e) { |
| TMBOY | 45:2aa9f933c8d2 | 180 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 181 | } |
| TMBOY | 45:2aa9f933c8d2 | 182 | if (null != webViewClientListen) { |
| TMBOY | 45:2aa9f933c8d2 | 183 | loadWebStatus = webViewClientListen.shouldOverrideUrlLoading(view, url); |
| TMBOY | 45:2aa9f933c8d2 | 184 | } |
| TMBOY | 45:2aa9f933c8d2 | 185 | if (LoadingWebStatus.STATUS_FALSE == loadWebStatus) { |
| TMBOY | 45:2aa9f933c8d2 | 186 | return false; |
| TMBOY | 45:2aa9f933c8d2 | 187 | } else if (LoadingWebStatus.STATUS_TRUE == loadWebStatus) { |
| TMBOY | 45:2aa9f933c8d2 | 188 | return true; |
| TMBOY | 45:2aa9f933c8d2 | 189 | } else { |
| TMBOY | 45:2aa9f933c8d2 | 190 | return super.shouldOverrideUrlLoading(view, url); |
| TMBOY | 45:2aa9f933c8d2 | 191 | } |
| TMBOY | 45:2aa9f933c8d2 | 192 | } |
| TMBOY | 45:2aa9f933c8d2 | 193 | |
| TMBOY | 45:2aa9f933c8d2 | 194 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 195 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| TMBOY | 45:2aa9f933c8d2 | 196 | super.onPageStarted(view, url, favicon); |
| TMBOY | 45:2aa9f933c8d2 | 197 | if (null != webViewClientListen) { |
| TMBOY | 45:2aa9f933c8d2 | 198 | webViewClientListen.onPageStarted(view, url, favicon); |
| TMBOY | 45:2aa9f933c8d2 | 199 | } |
| TMBOY | 45:2aa9f933c8d2 | 200 | } |
| TMBOY | 45:2aa9f933c8d2 | 201 | |
| TMBOY | 45:2aa9f933c8d2 | 202 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 203 | public void onPageFinished(WebView view, String url) { |
| TMBOY | 45:2aa9f933c8d2 | 204 | super.onPageFinished(view, url); |
| TMBOY | 45:2aa9f933c8d2 | 205 | if (null != webViewClientListen) { |
| TMBOY | 45:2aa9f933c8d2 | 206 | webViewClientListen.onPageFinished(view, url); |
| TMBOY | 45:2aa9f933c8d2 | 207 | } |
| TMBOY | 45:2aa9f933c8d2 | 208 | } |
| TMBOY | 45:2aa9f933c8d2 | 209 | |
| TMBOY | 45:2aa9f933c8d2 | 210 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 211 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| TMBOY | 45:2aa9f933c8d2 | 212 | super.onReceivedError(view, errorCode, description, failingUrl); |
| TMBOY | 45:2aa9f933c8d2 | 213 | if (null != webViewClientListen) { |
| TMBOY | 45:2aa9f933c8d2 | 214 | webViewClientListen.onReceivedError(view, errorCode, description, failingUrl); |
| TMBOY | 45:2aa9f933c8d2 | 215 | } |
| TMBOY | 45:2aa9f933c8d2 | 216 | } |
| TMBOY | 45:2aa9f933c8d2 | 217 | |
| TMBOY | 45:2aa9f933c8d2 | 218 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 219 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { |
| TMBOY | 45:2aa9f933c8d2 | 220 | // 当发生证书认证错误时,采用默认的处理方法handler.cancel(),停止加载问题页面 |
| TMBOY | 45:2aa9f933c8d2 | 221 | handler.cancel(); |
| TMBOY | 45:2aa9f933c8d2 | 222 | } |
| TMBOY | 45:2aa9f933c8d2 | 223 | } |
| TMBOY | 45:2aa9f933c8d2 | 224 | } |
