123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package com.linkdialy.mobile.kuyicloud;
-
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.support.annotation.RequiresApi;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.ViewGroup;
- import android.webkit.JavascriptInterface;
- import android.widget.Toast;
-
- import com.linkdialy.mobile.kuyicloud.utils.WebViewJavaScriptFunction;
- import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
- import com.tencent.smtt.export.external.interfaces.JsPromptResult;
- import com.tencent.smtt.export.external.interfaces.JsResult;
- import com.tencent.smtt.sdk.ValueCallback;
- import com.tencent.smtt.sdk.WebChromeClient;
- import com.tencent.smtt.sdk.WebSettings;
- import com.tencent.smtt.sdk.WebView;
- import com.tencent.smtt.sdk.WebViewClient;
-
- import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.REQUEST_EXTERNAL_STORAGE;
- import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.REQUEST_GEOLOCATION;
- import static com.linkdialy.mobile.kuyicloud.utils.PermissionUtil.verifyLocationPermissions;
-
- /**
- * Demo 基础 WebViewActivity,所有WebView能力Demo继承该 Activity 开发
- */
- public class BaseWebViewActivity extends AppCompatActivity {
- private String TAG = "BaseWebViewActivity";
-
- protected WebView mWebView;
- private static final int DISABLE_ALPHA = 120;
- private static final int ENABLE_ALPHA = 255;
- private static final int FILE_CHOOSER_REQUEST = 100;
-
- private long mClickBackTime = 0;
-
-
- private ValueCallback<Uri[]> mFilePathCallback;
-
- private GeolocationPermissionsCallback mGeolocationCallback;
- private String locationPermissionUrl;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.activity_webview);
- initWebView();
- }
-
- /**
- * 自定义初始化WebView设置,此处为默认 BaseWebViewActivity 初始化
- * 可通过继承该 Activity Override 该方法做自己的实现
- */
- protected void initWebView() {
-
- Context context = this;
- mWebView = new WebView(context);
- ViewGroup mContainer = findViewById(R.id.webViewContainer);
- mContainer.addView(mWebView);
- WebSettings webSetting = mWebView.getSettings();
- webSetting.setJavaScriptEnabled(true);
- webSetting.setAllowFileAccess(true);
- webSetting.setSupportZoom(true);
- webSetting.setDatabaseEnabled(true);
- webSetting.setAllowFileAccess(true);
- webSetting.setDomStorageEnabled(true);
- initWebViewClient();
- initWebChromeClient();
- initJavaScriptInterface();
-
- mWebView.loadUrl("http://mobile.kuyicloud.com/");
- }
-
-
- private void initWebViewClient() {
- mWebView.setWebViewClient(new WebViewClient() {
-
- /**
- * 具体接口使用细节请参考文档:
- * https://x5.tencent.com/docs/webview.html
- * 或 Android WebKit 官方:
- * https://developer.android.com/reference/android/webkit/WebChromeClient
- */
-
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- Log.i(TAG, "onPageStarted, view:" + view + ", url:" + url);
- }
-
- @Override
- public void onPageFinished(WebView view, String url) {
- Log.i(TAG, "onPageFinished, view:" + view + ", url:" + url);
- }
-
- @Override
- public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
-
- }
-
-
- });
- }
-
- private void initWebChromeClient() {
- final Context context = this;
- final Activity activity = this;
- mWebView.setWebChromeClient(new WebChromeClient() {
- /**
- * 具体接口使用细节请参考文档:
- * https://x5.tencent.com/docs/webview.html
- * 或 Android WebKit 官方:
- * https://developer.android.com/reference/android/webkit/WebChromeClient
- */
-
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- Log.i(TAG, "onProgressChanged, newProgress:" + newProgress + ", view:" + view);
- }
-
- @Override
- public boolean onJsAlert(WebView webView, String url, String message, JsResult result) {
-
- return true;
- }
-
- @Override
- public boolean onJsConfirm(WebView webView, String url, String message, JsResult result) {
-
- return true;
- }
-
- @Override
- public boolean onJsBeforeUnload(WebView webView, String url, String message, JsResult result) {
-
- return true;
- }
-
- @Override
- public boolean onJsPrompt(WebView webView, String url, String message, String defaultValue, JsPromptResult result) {
-
- return true;
- }
-
- /**
- * Return value usage see FILE_CHOOSE_REQUEST in
- * {@link BaseWebViewActivity#onActivityResult(int, int, Intent)}
- */
- @Override
- public boolean onShowFileChooser(WebView webView,
- ValueCallback<Uri[]> filePathCallback,
- FileChooserParams fileChooserParams) {
- Log.i(TAG, "openFileChooser: " + fileChooserParams.getMode());
- mFilePathCallback = filePathCallback;
- openFileChooseProcess(fileChooserParams.getMode() == FileChooserParams.MODE_OPEN_MULTIPLE);
- return true;
- }
-
- @Override
- public void onGeolocationPermissionsShowPrompt(String origin,
- GeolocationPermissionsCallback geolocationPermissionsCallback) {
- if (verifyLocationPermissions(activity)) {
- geolocationPermissionsCallback.invoke(origin, true, false);
- } else {
- locationPermissionUrl = origin;
- mGeolocationCallback = geolocationPermissionsCallback;
- }
- }
- });
- }
-
- private void initJavaScriptInterface() {
- final Activity context = this;
- mWebView.addJavascriptInterface(new WebViewJavaScriptFunction() {
- @Override
- public void onJsFunctionCalled(String tag) {
-
- }
- @JavascriptInterface
- public void openDebugX5() {
- mWebView.loadUrl("http://mobile.kuyicloud.com/");
- }
-
-
- }, "Android");
- }
-
- /* Don't care about the Base UI Logic below ^_^ */
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == REQUEST_EXTERNAL_STORAGE) {
- initWebView();
- }
-
- if (mGeolocationCallback != null && requestCode == REQUEST_GEOLOCATION) {
- boolean allow = grantResults[0] == PackageManager.PERMISSION_GRANTED;
- mGeolocationCallback.invoke(locationPermissionUrl, allow,false);
- mGeolocationCallback = null;
- locationPermissionUrl = "";
- }
- }
-
- @Override
- protected void onDestroy() {
- if (mWebView != null) {
- mWebView.destroy();
- }
- super.onDestroy();
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (mWebView != null && mWebView.canGoBack()) {
- mWebView.goBack();
- return true;
- }
- long currentTime = System.currentTimeMillis();
- // 3秒内连按两次后退按钮,退出应用
- if (currentTime - mClickBackTime < 3000) {
- // android.os.Process.killProcess(android.os.Process.myPid());
- finish();
- } else {
- Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
- mClickBackTime = currentTime;
- }
-
- return true;
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
- @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == RESULT_OK) {
- if (mFilePathCallback != null) {
- if(data != null && data.getClipData() != null) {
- //有选择多个文件
- int count = data.getClipData().getItemCount();
- Log.i(TAG, "url count : " + count);
- Uri[] uris = new Uri[count];
- int currentItem = 0;
- while(currentItem < count) {
- Uri fileUri = data.getClipData().getItemAt(currentItem).getUri();
- uris[currentItem] = fileUri;
- currentItem = currentItem + 1;
- }
- mFilePathCallback.onReceiveValue(uris);
- } else {
- Uri result = data == null ? null : data.getData();
- Log.e(TAG, "" + result);
- mFilePathCallback.onReceiveValue(new Uri[]{result});
- }
- mFilePathCallback = null;
- }
- }
- }
-
- private void openFileChooseProcess(boolean isMulti) {
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setType("*/*");
- if (isMulti) {
- Log.e(TAG, "putExtra");
- intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
- }
- startActivityForResult(Intent.createChooser(intent, "FileChooser"), FILE_CHOOSER_REQUEST);
- }
-
- }
|