|
|
|
|
@@ -0,0 +1,320 @@
|
|
|
|
|
package com.yunbao.main.activity;
|
|
|
|
|
|
|
|
|
|
import android.content.ClipData;
|
|
|
|
|
import android.content.ClipboardManager;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.webkit.ValueCallback;
|
|
|
|
|
import android.webkit.WebChromeClient;
|
|
|
|
|
import android.webkit.WebResourceRequest;
|
|
|
|
|
import android.webkit.WebResourceResponse;
|
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
|
import android.webkit.WebView;
|
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.android.arouter.facade.annotation.Route;
|
|
|
|
|
import com.opensource.svgaplayer.SVGACallback;
|
|
|
|
|
import com.opensource.svgaplayer.SVGADrawable;
|
|
|
|
|
import com.opensource.svgaplayer.SVGAImageView;
|
|
|
|
|
import com.opensource.svgaplayer.SVGAParser;
|
|
|
|
|
import com.opensource.svgaplayer.SVGAVideoEntity;
|
|
|
|
|
import com.yunbao.common.Constants;
|
|
|
|
|
import com.yunbao.common.R;
|
|
|
|
|
import com.yunbao.common.activity.AbsActivity;
|
|
|
|
|
import com.yunbao.common.event.JavascriptInterfaceEvent;
|
|
|
|
|
import com.yunbao.common.utils.AndroidBug5497Workaround;
|
|
|
|
|
import com.yunbao.common.utils.Bus;
|
|
|
|
|
import com.yunbao.common.utils.DpUtil;
|
|
|
|
|
import com.yunbao.common.utils.JavascriptInterfaceUtils;
|
|
|
|
|
import com.yunbao.common.utils.L;
|
|
|
|
|
import com.yunbao.common.utils.RouteUtil;
|
|
|
|
|
import com.yunbao.common.utils.SVGAViewUtils;
|
|
|
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
|
|
|
import com.yunbao.share.ui.InvitePopDialog;
|
|
|
|
|
|
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 专门给客服页面的WebView
|
|
|
|
|
*/
|
|
|
|
|
@Route(path = RouteUtil.PATH_CS)
|
|
|
|
|
public class CustomerServiceWebViewActivity extends AbsActivity {
|
|
|
|
|
private ProgressBar mProgressBar;
|
|
|
|
|
private WebView mWebView;
|
|
|
|
|
private final int CHOOSE = 100;//Android 5.0以下的
|
|
|
|
|
private final int CHOOSE_ANDROID_5 = 200;//Android 5.0以上的
|
|
|
|
|
private ValueCallback<Uri> mValueCallback;
|
|
|
|
|
private ValueCallback<Uri[]> mValueCallback2;
|
|
|
|
|
private FrameLayout rootView;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected int getLayoutId() {
|
|
|
|
|
return R.layout.activity_webview_cs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void main() {
|
|
|
|
|
super.main();
|
|
|
|
|
Bus.getOn(this);
|
|
|
|
|
String url = getIntent().getStringExtra(Constants.URL);
|
|
|
|
|
L.e("H5-------->" + url);
|
|
|
|
|
mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
|
|
|
|
|
rootView = findViewById(R.id.webView_root);
|
|
|
|
|
mWebView = findViewById(R.id.webView);
|
|
|
|
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
|
|
|
params.topMargin = DpUtil.dp2px(1);
|
|
|
|
|
if (getNavigationBarHeight(mContext) != 0) {
|
|
|
|
|
params.bottomMargin = getNavigationBarHeight(mContext);
|
|
|
|
|
}
|
|
|
|
|
mWebView.setLayoutParams(params);
|
|
|
|
|
mWebView.setOverScrollMode(View.OVER_SCROLL_NEVER);
|
|
|
|
|
mWebView.setWebViewClient(new WebViewClient() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
|
L.e("H5-------->" + url);
|
|
|
|
|
if (url.startsWith(Constants.COPY_PREFIX)) {
|
|
|
|
|
String content = url.substring(Constants.COPY_PREFIX.length());
|
|
|
|
|
if (!TextUtils.isEmpty(content)) {
|
|
|
|
|
copy(content);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mWebView.post(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
view.loadUrl(url);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
|
|
|
|
setTitle(view.getTitle());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
|
super.onReceivedError(view, errorCode, description, failingUrl);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
|
|
|
|
|
super.onReceivedHttpError(view, request, errorResponse);
|
|
|
|
|
// 这个方法在6.0才出现
|
|
|
|
|
int statusCode = errorResponse.getStatusCode();
|
|
|
|
|
Log.e("WebView", "code = " + statusCode + " failingUrl:" + request.getUrl());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mWebView.setWebChromeClient(new WebChromeClient() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onProgressChanged(WebView view, int newProgress) {
|
|
|
|
|
if (newProgress == 100) {
|
|
|
|
|
mProgressBar.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
|
mProgressBar.setProgress(newProgress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//以下是在各个Android版本中 WebView调用文件选择器的方法
|
|
|
|
|
// For Android < 3.0
|
|
|
|
|
public void openFileChooser(ValueCallback<Uri> valueCallback) {
|
|
|
|
|
openImageChooserActivity(valueCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For Android >= 3.0
|
|
|
|
|
public void openFileChooser(ValueCallback valueCallback, String acceptType) {
|
|
|
|
|
openImageChooserActivity(valueCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//For Android >= 4.1
|
|
|
|
|
public void openFileChooser(ValueCallback<Uri> valueCallback,
|
|
|
|
|
String acceptType, String capture) {
|
|
|
|
|
openImageChooserActivity(valueCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For Android >= 5.0
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onShowFileChooser(WebView webView,
|
|
|
|
|
ValueCallback<Uri[]> filePathCallback,
|
|
|
|
|
FileChooserParams fileChooserParams) {
|
|
|
|
|
mValueCallback2 = filePathCallback;
|
|
|
|
|
Intent intent = fileChooserParams.createIntent();
|
|
|
|
|
startActivityForResult(intent, CHOOSE_ANDROID_5);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mWebView.addJavascriptInterface(JavascriptInterfaceUtils.getInstance().setmContext(mContext, mWebView).setPageClose(true).setLiveZhuangBana(false), "androidObject");
|
|
|
|
|
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // 是否允许JS打开新窗口
|
|
|
|
|
mWebView.getSettings().setJavaScriptEnabled(true);
|
|
|
|
|
mWebView.getSettings().setDomStorageEnabled(true);
|
|
|
|
|
String appCachePath = mContext.getCacheDir().getAbsolutePath();
|
|
|
|
|
mWebView.getSettings().setAppCachePath(appCachePath);
|
|
|
|
|
mWebView.getSettings().setAllowFileAccess(true);
|
|
|
|
|
mWebView.getSettings().setUseWideViewPort(true); // 关键点
|
|
|
|
|
mWebView.getSettings().setAllowFileAccess(true); // 允许访问文件
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
|
mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
|
|
}
|
|
|
|
|
mWebView.loadUrl(url);
|
|
|
|
|
AndroidBug5497Workaround.assistActivity(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openImageChooserActivity(ValueCallback<Uri> valueCallback) {
|
|
|
|
|
mValueCallback = valueCallback;
|
|
|
|
|
Intent intent = new Intent();
|
|
|
|
|
if (Build.VERSION.SDK_INT < 19) {
|
|
|
|
|
intent.setAction(Intent.ACTION_GET_CONTENT);
|
|
|
|
|
} else {
|
|
|
|
|
intent.setAction(Intent.ACTION_PICK);
|
|
|
|
|
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
|
|
|
|
}
|
|
|
|
|
intent.setType("image/*");
|
|
|
|
|
startActivityForResult(Intent.createChooser(intent, mContext.getString(R.string.choose_flie)), CHOOSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, intent);
|
|
|
|
|
switch (requestCode) {
|
|
|
|
|
case CHOOSE://5.0以下选择图片后的回调
|
|
|
|
|
processResult(resultCode, intent);
|
|
|
|
|
break;
|
|
|
|
|
case CHOOSE_ANDROID_5://5.0以上选择图片后的回调
|
|
|
|
|
processResultAndroid5(resultCode, intent);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void processResult(int resultCode, Intent intent) {
|
|
|
|
|
if (mValueCallback == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (resultCode == RESULT_OK && intent != null) {
|
|
|
|
|
Uri result = intent.getData();
|
|
|
|
|
mValueCallback.onReceiveValue(result);
|
|
|
|
|
} else {
|
|
|
|
|
mValueCallback.onReceiveValue(null);
|
|
|
|
|
}
|
|
|
|
|
mValueCallback = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
|
private void processResultAndroid5(int resultCode, Intent intent) {
|
|
|
|
|
if (mValueCallback2 == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (resultCode == RESULT_OK && intent != null) {
|
|
|
|
|
mValueCallback2.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
|
|
|
|
|
} else {
|
|
|
|
|
mValueCallback2.onReceiveValue(null);
|
|
|
|
|
}
|
|
|
|
|
mValueCallback2 = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected boolean canGoBack() {
|
|
|
|
|
return mWebView != null && mWebView.canGoBack();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
Bus.getOff(this);
|
|
|
|
|
if (mWebView != null) {
|
|
|
|
|
ViewGroup parent = (ViewGroup) mWebView.getParent();
|
|
|
|
|
if (parent != null) {
|
|
|
|
|
parent.removeView(mWebView);
|
|
|
|
|
}
|
|
|
|
|
mWebView.destroy();
|
|
|
|
|
}
|
|
|
|
|
super.onDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 复制到剪贴板
|
|
|
|
|
*/
|
|
|
|
|
private void copy(String content) {
|
|
|
|
|
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
|
|
|
|
ClipData clipData = ClipData.newPlainText("text", content);
|
|
|
|
|
cm.setPrimaryClip(clipData);
|
|
|
|
|
ToastUtil.show(getString(R.string.copy_success));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
|
|
public void onJavascriptInterfaceEvent(JavascriptInterfaceEvent event) {
|
|
|
|
|
if (!TextUtils.isEmpty(event.getMethod())) {
|
|
|
|
|
if (TextUtils.equals(event.getMethod(), "androidOpeningNoble")) {
|
|
|
|
|
try {
|
|
|
|
|
SVGAImageView svga = new SVGAImageView(mContext);
|
|
|
|
|
rootView.addView(svga);
|
|
|
|
|
new SVGAParser(mContext).parse(new URL(event.getSvg()), new SVGAParser.ParseCompletion() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onComplete(SVGAVideoEntity videoItem) {
|
|
|
|
|
svga.setVisibility(View.VISIBLE);
|
|
|
|
|
SVGADrawable drawable = new SVGADrawable(videoItem);
|
|
|
|
|
svga.setImageDrawable(drawable);
|
|
|
|
|
svga.setLoops(1);
|
|
|
|
|
SVGAViewUtils.playEndClear(svga);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError() {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
svga.setCallback(new SVGACallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onPause() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onFinished() {
|
|
|
|
|
svga.clear();
|
|
|
|
|
svga.setVisibility(View.GONE);
|
|
|
|
|
rootView.removeView(svga);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onRepeat() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onStep(int i, double v) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.getMethod().equals("androidInviteShare")) {
|
|
|
|
|
new InvitePopDialog(mContext)
|
|
|
|
|
.setUrl(event.getData())
|
|
|
|
|
.showDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|