WebViewFragment.java
7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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.text.TextUtils;
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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.polysoft.nafmii.bean.MenuBean;
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.IWebview;
import com.polysoft.nafmii.webview.JSApi;
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.wang.avi.AVLoadingIndicatorView;
import com.whaty.nafmii.R;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class WebViewFragment extends Fragment implements WebviewLoadListener {
protected WebView mWebview;
private ViewGroup mWebviewLayout;
private AVLoadingIndicatorView mLoading;
private WebviewHandler mWebviewHandler;
private ValueCallback<Uri[]> filePathCallback;
public static WebViewFragment newInstance(String url) {
Bundle args = new Bundle();
WebViewFragment fragment = new WebViewFragment();
args.putString("url", url);
fragment.setArguments(args);
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.mWebviewLayout = view.findViewById(R.id.webview_layout);
this.mLoading = view.findViewById(R.id.webview_loading);
this.initWebView();
}
public IWebview getWebview() {
if (null == this.mWebviewHandler) {
this.mWebviewHandler = new WebviewHandler(this, this.mWebview);
}
return this.mWebviewHandler;
}
@Override
public void onDestroy() {
super.onDestroy();
X5WebviewUtils.destroy(this.mWebview);
}
@Override
public void onWebviewLoad(int type, Object param) {
if (type == WebviewLoadListener.TYPE_FINISH) {
this.mLoading.hide();
Intent intent = getActivity().getIntent();
String changeRoute = intent.getStringExtra("changeRoute");
Map<String, Object> map = JSON.parseObject(changeRoute, Map.class);
MenuBean menuBean = JSON.parseObject(changeRoute, MenuBean.class);
if (null == menuBean || !this.getTag().equals(map.get("moduleName"))) {
return;
}
System.out.println(changeRoute);
IWebview webview = this.getWebview();
JSApi.callOnRouteChange(webview.getWebview(), JSON.toJSONString(map));
}
}
@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 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("操作失败!");
}
}
}
protected void loadUrl(String url) {
this.mWebview.loadUrl(url);
}
protected WebviewHandler initWeviewHandle(WebView webView) {
return new WebviewHandler(this, webView);
}
private void initWebView() {
this.mWebview = X5WebviewUtils.create(getActivity());
this.mWebview.setWebChromeClient(new MyWebChromeClient(this));
this.mWebview.setWebViewClient(new MyWebViewClient(this));
this.mWebviewHandler = this.initWeviewHandle(this.mWebview);
this.showWebviewLayout(this.mWebview);
this.loadUrl(super.getArguments().getString("url", ""));
}
private void showWebviewLayout(WebView webView) {
this.mWebviewLayout.removeAllViews();
int match = ViewGroup.LayoutParams.MATCH_PARENT;
this.mWebviewLayout.addView(this.mWebview, new ViewGroup.LayoutParams(match, match));
}
}