package com.arpa.hndahesudintocctmsdriver.util; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.webkit.PermissionRequest; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; /** * @author hlh * @version 1.0.0 * @date 2022/1/20 17:27 * @description: */ public class PaxWebChromeClient extends WebChromeClient { private static final int CHOOSE_REQUEST_CODE = 0x9001; private Activity mActivity; private ValueCallback uploadFile;//定义接受返回值 private ValueCallback uploadFiles; private ProgressBar bar; private TextView mTitle; public PaxWebChromeClient(@NonNull Activity mActivity, ProgressBar bar, TextView title) { this.mActivity = mActivity; this.bar=bar; this.mTitle=title; } @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { //bar.setVisibility(View.INVISIBLE); } else { // if (View.INVISIBLE == bar.getVisibility()) { // bar.setVisibility(View.VISIBLE); // } // bar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); // mTitle.setText(title); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public void onPermissionRequest(PermissionRequest request) { // super.onPermissionRequest(request);//必须要注视掉 request.grant(request.getResources()); } // For Android 3.0+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) { this.uploadFile = uploadFile; openFileChooseProcess(); } // For Android < 3.0 public void openFileChooser(ValueCallback uploadMsgs) { this.uploadFile = uploadFile; openFileChooseProcess(); } // For Android > 4.1.1 // @Override public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { this.uploadFile = uploadFile; openFileChooseProcess(); } // For Android >= 5.0 @Override public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { this.uploadFiles = filePathCallback; openFileChooseProcess(); return true; } private void openFileChooseProcess() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); mActivity.startActivityForResult(Intent.createChooser(i, "Choose"), CHOOSE_REQUEST_CODE); } public void onActivityResult(int requestCode, int resultCode, Intent data) { //LogCat.d("requestCode===",requestCode+"===="); if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case CHOOSE_REQUEST_CODE: if (null != uploadFile) { Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); uploadFile.onReceiveValue(result); uploadFile = null; } if (null != uploadFiles) { Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); uploadFiles.onReceiveValue(new Uri[]{result}); uploadFiles = null; } break; default: break; } } else if (resultCode == Activity.RESULT_CANCELED) { if (null != uploadFile) { uploadFile.onReceiveValue(null); uploadFile = null; } if (null != uploadFiles) { uploadFiles.onReceiveValue(null); uploadFiles = null; } } } }