Commit 6a76ca33 by amateur

1

1 parent 3aac9a25
package com.polysoft.nafmii.fragment;
import android.os.Bundle;
import com.polysoft.nafmii.webview.HomeWebviewHandler;
import com.polysoft.nafmii.webview.NativeApi;
import com.tencent.smtt.sdk.WebView;
public class HomeWebviewFragment extends WebViewFragment {
public static WebViewFragment newInstances(String url) {
HomeWebviewFragment fragment = new HomeWebviewFragment();
Bundle bundle = new Bundle();
bundle.putString("url", url);
fragment.setArguments(bundle);
return fragment;
}
@Override
protected void setWebView(WebView webView) {
super.setWebView(webView);
HomeWebviewHandler handler = new HomeWebviewHandler(this, webView);
webView.addJavascriptInterface(new NativeApi(handler), "$NativeApi");
super.setWebviewHandler(handler);
}
}
...@@ -20,7 +20,6 @@ import com.polysoft.nafmii.utils.FileProviderUtils; ...@@ -20,7 +20,6 @@ import com.polysoft.nafmii.utils.FileProviderUtils;
import com.polysoft.nafmii.utils.IntentUtils; import com.polysoft.nafmii.utils.IntentUtils;
import com.polysoft.nafmii.utils.PermissionUtils; import com.polysoft.nafmii.utils.PermissionUtils;
import com.polysoft.nafmii.utils.ToastUtils; import com.polysoft.nafmii.utils.ToastUtils;
import com.polysoft.nafmii.webview.HomeWebviewHandler;
import com.polysoft.nafmii.webview.IWebview; import com.polysoft.nafmii.webview.IWebview;
import com.polysoft.nafmii.webview.MyWebChromeClient; import com.polysoft.nafmii.webview.MyWebChromeClient;
import com.polysoft.nafmii.webview.MyWebViewClient; import com.polysoft.nafmii.webview.MyWebViewClient;
...@@ -35,73 +34,50 @@ import java.io.File; ...@@ -35,73 +34,50 @@ import java.io.File;
public class WebViewFragment extends Fragment implements WebviewLoadListener { public class WebViewFragment extends Fragment implements WebviewLoadListener {
private View rootView; protected WebView mWebview;
private ViewGroup layout; private ViewGroup mWebviewLayout;
private WebView webView; private WebviewHandler mWebviewHandler;
private WebviewHandler webviewHandler;
private ValueCallback<Uri[]> filePathCallback; private ValueCallback<Uri[]> filePathCallback;
public static WebViewFragment newInstances(String url) { public static WebViewFragment newInstance(String url) {
Bundle args = new Bundle();
WebViewFragment fragment = new WebViewFragment(); WebViewFragment fragment = new WebViewFragment();
Bundle bundle = new Bundle(); args.putString("url", url);
bundle.putString("url", url); fragment.setArguments(args);
fragment.setArguments(bundle);
return fragment; return fragment;
} }
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
super.setHasOptionsMenu(true); super.setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_webview, null, false); return inflater.inflate(R.layout.fragment_webview, null, false);
} }
@Override @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
this.layout = view.findViewById(R.id.webview_layout); this.mWebviewLayout = view.findViewById(R.id.webview_layout);
this.initWebview(); 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() { public IWebview getWebview() {
return this.webviewHandler; if (null == this.mWebviewHandler) {
this.mWebviewHandler = new WebviewHandler(this, this.mWebview);
}
return this.mWebviewHandler;
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
X5WebviewUtils.destroy(this.webView); X5WebviewUtils.destroy(this.mWebview);
} }
@Override @Override
public void onWebviewLoad(int type, Object param) { public void onWebviewLoad(int type, Object param) {
if (type == WebviewLoadListener.TYPE_FINISH) {
} else if (type == WebviewLoadListener.TYPE_ERROR) {
}
} }
@Override @Override
...@@ -121,7 +97,7 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener { ...@@ -121,7 +97,7 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener {
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) { public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (IntentUtils.ACTIVITY_CODE_CHOOSERFILE == requestCode) { if (IntentUtils.ACTIVITY_CODE_CHOOSERFILE == requestCode) {
if (Activity.RESULT_OK != resultCode) { if (Activity.RESULT_OK != resultCode) {
ToastUtils.showLong("操作失败!"); ToastUtils.showLong("操作失败!");
...@@ -154,7 +130,7 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener { ...@@ -154,7 +130,7 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener {
String filepath = IntentUtils.getTakePhotoPath(super.getContext()); String filepath = IntentUtils.getTakePhotoPath(super.getContext());
File file = BitmapUtils.compressBitmap(filepath); File file = BitmapUtils.compressBitmap(filepath);
PhotosReceiver.sendBroadcastCamera(super.getContext(), file.getAbsolutePath()); PhotosReceiver.sendBroadcastCamera(super.getContext(), file.getAbsolutePath());
} else if (null != data && null != data.getData()){ } else if (null != data && null != data.getData()) {
Uri imgUrl = data.getData(); Uri imgUrl = data.getData();
String newFilepath = IntentUtils.getTakePhotoPath(super.getContext()); String newFilepath = IntentUtils.getTakePhotoPath(super.getContext());
String filepath = FileProviderUtils.getUriPath(super.getContext(), imgUrl); String filepath = FileProviderUtils.getUriPath(super.getContext(), imgUrl);
...@@ -166,4 +142,31 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener { ...@@ -166,4 +142,31 @@ public class WebViewFragment extends Fragment implements WebviewLoadListener {
} }
} }
} }
protected void loadUrl(String url) {
this.mWebview.loadUrl(url);
}
protected void setWebviewHandler(WebviewHandler handler) {
this.mWebviewHandler = handler;
}
protected void setWebView(WebView webView) {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new MyWebViewClient(this));
this.mWebview = webView;
}
private void initWebView() {
WebView webView = X5WebviewUtils.create(getActivity());
this.setWebView(webView);
this.showWebviewLayout(webView);
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));
}
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!