WebViewFragment.java 6.63 KB
package com.polysoft.nafmii.fragment;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.polysoft.nafmii.broadcast.PhotosReceiver;
import com.polysoft.nafmii.utils.BitmapUtils;
import com.polysoft.nafmii.utils.FileProviderUtils;
import com.polysoft.nafmii.utils.IntentUtils;
import com.polysoft.nafmii.utils.PermissionUtils;
import com.polysoft.nafmii.utils.ToastUtils;
import com.polysoft.nafmii.webview.HomeWebviewHandler;
import com.polysoft.nafmii.webview.IWebview;
import com.polysoft.nafmii.webview.MyWebChromeClient;
import com.polysoft.nafmii.webview.MyWebViewClient;
import com.polysoft.nafmii.webview.WebviewHandler;
import com.polysoft.nafmii.webview.WebviewLoadListener;
import com.polysoft.nafmii.webview.X5WebviewUtils;
import com.tencent.smtt.sdk.WebView;
import com.whaty.nafmii.R;

import java.io.File;


public class WebViewFragment extends Fragment implements WebviewLoadListener {

    private View rootView;
    private ViewGroup layout;
    private WebView webView;

    private WebviewHandler webviewHandler;
    private ValueCallback<Uri[]> filePathCallback;

    public static WebViewFragment newInstances(String url) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        super.setHasOptionsMenu(true);
        return inflater.inflate(R.layout.fragment_webview, null, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.layout = view.findViewById(R.id.webview_layout);
        this.initWebview();
    }


    private void initWebview() {
        WebView webView = X5WebviewUtils.create(getActivity());
        webView.setWebChromeClient(new MyWebChromeClient(this));
        webView.setWebViewClient(new MyWebViewClient(this));
        this.webviewHandler = new HomeWebviewHandler(this, webView);

        this.layout.removeAllViews();
        int match = ViewGroup.LayoutParams.MATCH_PARENT;
        this.layout.addView(webView, new ViewGroup.LayoutParams(match, match));

        this.webView = webView;

        Bundle bundle = getArguments();
        String url = bundle.getString("url", "");
        this.webView.loadUrl(url);
    }

    public IWebview getWebview() {
        return this.webviewHandler;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        X5WebviewUtils.destroy(this.webView);
    }

    @Override
    public void onWebviewLoad(int type, Object param) {
        if (type == WebviewLoadListener.TYPE_FINISH) {

        } else if (type == WebviewLoadListener.TYPE_ERROR) {

        }
    }

    @Override
    public void onShowFileChooser(ValueCallback<Uri[]> filePathCallback, String[] acceptTypes, boolean isCapture) {
        String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (PermissionUtils.checkAndRequestMorePermissions(getContext(), permissions)) {
            Intent intent = IntentUtils.chooserIntent(super.getContext());
            if (null != intent) {
                this.filePathCallback = filePathCallback;
                super.startActivityForResult(intent, IntentUtils.ACTIVITY_CODE_CHOOSERFILE);
            } else {
                filePathCallback.onReceiveValue(null);
            }
        } else {
            filePathCallback.onReceiveValue(null);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        if (IntentUtils.ACTIVITY_CODE_CHOOSERFILE == requestCode) {
            if (Activity.RESULT_OK != resultCode) {
                ToastUtils.showLong("操作失败!");
                this.filePathCallback.onReceiveValue(null);
            } else if (null != this.filePathCallback) {
                if (null != data && null != data.getData()) {
                    Uri imgUrl = data.getData();
                    String newFilepath = IntentUtils.getTakePhotoPath(super.getContext());
                    String filepath = FileProviderUtils.getUriPath(super.getContext(), imgUrl);
                    File file = BitmapUtils.compressBitmap(filepath, newFilepath);

                    Uri uri = FileProviderUtils.getUriForFile(super.getContext(), file);
                    this.filePathCallback.onReceiveValue(new Uri[]{uri});
                } else {
                    String filepath = IntentUtils.getTakePhotoPath(super.getContext());
                    File file = BitmapUtils.compressBitmap(filepath);
                    if (file.exists()) {
                        Uri uri = FileProviderUtils.getUriForFile(super.getContext(), file);
                        this.filePathCallback.onReceiveValue(new Uri[]{uri});
                    } else {
                        this.filePathCallback.onReceiveValue(null);
                    }
                }
            }
            this.filePathCallback = null;
        } else if (IntentUtils.ACTIVITY_CODE_CAMERA == requestCode
                || IntentUtils.ACTIVITY_CODE_ALBUM == requestCode) {
            if (Activity.RESULT_OK == resultCode) {
                if (IntentUtils.ACTIVITY_CODE_CAMERA == requestCode) {
                    String filepath = IntentUtils.getTakePhotoPath(super.getContext());
                    File file = BitmapUtils.compressBitmap(filepath);
                    PhotosReceiver.sendBroadcastCamera(super.getContext(), file.getAbsolutePath());
                } else if (null != data && null != data.getData()){
                    Uri imgUrl = data.getData();
                    String newFilepath = IntentUtils.getTakePhotoPath(super.getContext());
                    String filepath = FileProviderUtils.getUriPath(super.getContext(), imgUrl);
                    File file = BitmapUtils.compressBitmap(filepath, newFilepath);
                    PhotosReceiver.sendBroadcastAlbum(super.getContext(), file.getAbsolutePath());
                }
            } else {
                ToastUtils.showLong("操作失败!");
            }
        }
    }
}