领透酷医云安卓客户端

BaseWebViewActivity.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package com.linkdialy.mobile.kuyicloud;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.graphics.Bitmap;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.annotation.Nullable;
  11. import android.support.annotation.RequiresApi;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.util.Log;
  14. import android.view.KeyEvent;
  15. import android.view.ViewGroup;
  16. import android.webkit.JavascriptInterface;
  17. import android.widget.Toast;
  18. import com.linkdialy.mobile.kuyicloud.utils.WebViewJavaScriptFunction;
  19. import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
  20. import com.tencent.smtt.export.external.interfaces.JsPromptResult;
  21. import com.tencent.smtt.export.external.interfaces.JsResult;
  22. import com.tencent.smtt.sdk.ValueCallback;
  23. import com.tencent.smtt.sdk.WebChromeClient;
  24. import com.tencent.smtt.sdk.WebSettings;
  25. import com.tencent.smtt.sdk.WebView;
  26. import com.tencent.smtt.sdk.WebViewClient;
  27. import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.REQUEST_EXTERNAL_STORAGE;
  28. import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.REQUEST_GEOLOCATION;
  29. import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.verifyLocationPermissions;
  30. /**
  31. * Demo 基础 WebViewActivity,所有WebView能力Demo继承该 Activity 开发
  32. */
  33. public class BaseWebViewActivity extends AppCompatActivity {
  34. private String TAG = "BaseWebViewActivity";
  35. protected WebView mWebView;
  36. private static final int DISABLE_ALPHA = 120;
  37. private static final int ENABLE_ALPHA = 255;
  38. private static final int FILE_CHOOSER_REQUEST = 100;
  39. private long mClickBackTime = 0;
  40. private ValueCallback<Uri[]> mFilePathCallback;
  41. private GeolocationPermissionsCallback mGeolocationCallback;
  42. private String locationPermissionUrl;
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_webview);
  47. initWebView();
  48. }
  49. /**
  50. * 自定义初始化WebView设置,此处为默认 BaseWebViewActivity 初始化
  51. * 可通过继承该 Activity Override 该方法做自己的实现
  52. */
  53. protected void initWebView() {
  54. Context context = this;
  55. mWebView = new WebView(context);
  56. ViewGroup mContainer = findViewById(R.id.webViewContainer);
  57. mContainer.addView(mWebView);
  58. WebSettings webSetting = mWebView.getSettings();
  59. webSetting.setJavaScriptEnabled(true);
  60. webSetting.setAllowFileAccess(true);
  61. webSetting.setSupportZoom(true);
  62. webSetting.setDatabaseEnabled(true);
  63. webSetting.setAllowFileAccess(true);
  64. webSetting.setDomStorageEnabled(true);
  65. initWebViewClient();
  66. initWebChromeClient();
  67. initJavaScriptInterface();
  68. mWebView.loadUrl("http://mobile.kuyicloud.com/");
  69. }
  70. private void initWebViewClient() {
  71. mWebView.setWebViewClient(new WebViewClient() {
  72. /**
  73. * 具体接口使用细节请参考文档:
  74. * https://x5.tencent.com/docs/webview.html
  75. * 或 Android WebKit 官方:
  76. * https://developer.android.com/reference/android/webkit/WebChromeClient
  77. */
  78. @Override
  79. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  80. Log.i(TAG, "onPageStarted, view:" + view + ", url:" + url);
  81. }
  82. @Override
  83. public void onPageFinished(WebView view, String url) {
  84. Log.i(TAG, "onPageFinished, view:" + view + ", url:" + url);
  85. }
  86. @Override
  87. public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
  88. }
  89. });
  90. }
  91. private void initWebChromeClient() {
  92. final Context context = this;
  93. final Activity activity = this;
  94. mWebView.setWebChromeClient(new WebChromeClient() {
  95. /**
  96. * 具体接口使用细节请参考文档:
  97. * https://x5.tencent.com/docs/webview.html
  98. * 或 Android WebKit 官方:
  99. * https://developer.android.com/reference/android/webkit/WebChromeClient
  100. */
  101. @Override
  102. public void onProgressChanged(WebView view, int newProgress) {
  103. Log.i(TAG, "onProgressChanged, newProgress:" + newProgress + ", view:" + view);
  104. }
  105. @Override
  106. public boolean onJsAlert(WebView webView, String url, String message, JsResult result) {
  107. return true;
  108. }
  109. @Override
  110. public boolean onJsConfirm(WebView webView, String url, String message, JsResult result) {
  111. return true;
  112. }
  113. @Override
  114. public boolean onJsBeforeUnload(WebView webView, String url, String message, JsResult result) {
  115. return true;
  116. }
  117. @Override
  118. public boolean onJsPrompt(WebView webView, String url, String message, String defaultValue, JsPromptResult result) {
  119. return true;
  120. }
  121. /**
  122. * Return value usage see FILE_CHOOSE_REQUEST in
  123. * {@link BaseWebViewActivity#onActivityResult(int, int, Intent)}
  124. */
  125. @Override
  126. public boolean onShowFileChooser(WebView webView,
  127. ValueCallback<Uri[]> filePathCallback,
  128. FileChooserParams fileChooserParams) {
  129. Log.i(TAG, "openFileChooser: " + fileChooserParams.getMode());
  130. mFilePathCallback = filePathCallback;
  131. openFileChooseProcess(fileChooserParams.getMode() == FileChooserParams.MODE_OPEN_MULTIPLE);
  132. return true;
  133. }
  134. @Override
  135. public void onGeolocationPermissionsShowPrompt(String origin,
  136. GeolocationPermissionsCallback geolocationPermissionsCallback) {
  137. if (verifyLocationPermissions(activity)) {
  138. geolocationPermissionsCallback.invoke(origin, true, false);
  139. } else {
  140. locationPermissionUrl = origin;
  141. mGeolocationCallback = geolocationPermissionsCallback;
  142. }
  143. }
  144. });
  145. }
  146. private void initJavaScriptInterface() {
  147. final Activity context = this;
  148. mWebView.addJavascriptInterface(new WebViewJavaScriptFunction() {
  149. @Override
  150. public void onJsFunctionCalled(String tag) {
  151. }
  152. @JavascriptInterface
  153. public void openDebugX5() {
  154. mWebView.loadUrl("http://mobile.kuyicloud.com/");
  155. }
  156. }, "Android");
  157. }
  158. /* Don't care about the Base UI Logic below ^_^ */
  159. @Override
  160. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  161. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  162. if (requestCode == REQUEST_EXTERNAL_STORAGE) {
  163. initWebView();
  164. }
  165. if (mGeolocationCallback != null && requestCode == REQUEST_GEOLOCATION) {
  166. boolean allow = grantResults[0] == PackageManager.PERMISSION_GRANTED;
  167. mGeolocationCallback.invoke(locationPermissionUrl, allow,false);
  168. mGeolocationCallback = null;
  169. locationPermissionUrl = "";
  170. }
  171. }
  172. @Override
  173. protected void onDestroy() {
  174. if (mWebView != null) {
  175. mWebView.destroy();
  176. }
  177. super.onDestroy();
  178. }
  179. @Override
  180. public boolean onKeyDown(int keyCode, KeyEvent event) {
  181. if (keyCode == KeyEvent.KEYCODE_BACK) {
  182. if (mWebView != null && mWebView.canGoBack()) {
  183. mWebView.goBack();
  184. return true;
  185. }
  186. long currentTime = System.currentTimeMillis();
  187. // 3秒内连按两次后退按钮,退出应用
  188. if (currentTime - mClickBackTime < 3000) {
  189. // android.os.Process.killProcess(android.os.Process.myPid());
  190. finish();
  191. } else {
  192. Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
  193. mClickBackTime = currentTime;
  194. }
  195. return true;
  196. }
  197. return super.onKeyDown(keyCode, event);
  198. }
  199. @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  200. @Override
  201. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  202. super.onActivityResult(requestCode, resultCode, data);
  203. if (resultCode == RESULT_OK) {
  204. if (mFilePathCallback != null) {
  205. if(data != null && data.getClipData() != null) {
  206. //有选择多个文件
  207. int count = data.getClipData().getItemCount();
  208. Log.i(TAG, "url count : " + count);
  209. Uri[] uris = new Uri[count];
  210. int currentItem = 0;
  211. while(currentItem < count) {
  212. Uri fileUri = data.getClipData().getItemAt(currentItem).getUri();
  213. uris[currentItem] = fileUri;
  214. currentItem = currentItem + 1;
  215. }
  216. mFilePathCallback.onReceiveValue(uris);
  217. } else {
  218. Uri result = data == null ? null : data.getData();
  219. Log.e(TAG, "" + result);
  220. mFilePathCallback.onReceiveValue(new Uri[]{result});
  221. }
  222. mFilePathCallback = null;
  223. }
  224. }
  225. }
  226. private void openFileChooseProcess(boolean isMulti) {
  227. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  228. intent.addCategory(Intent.CATEGORY_DEFAULT);
  229. intent.setType("*/*");
  230. if (isMulti) {
  231. Log.e(TAG, "putExtra");
  232. intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  233. }
  234. startActivityForResult(Intent.createChooser(intent, "FileChooser"), FILE_CHOOSER_REQUEST);
  235. }
  236. }