From 9c57f0bd96770ca335dd89da4140c6f9873057f5 Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Sat, 17 Jun 2023 17:22:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DwebView=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/activity/WebViewActivity.java | 4 +- .../com/yunbao/common/utils/RouteUtil.java | 10 + .../main/res/layout/activity_webview_cs.xml | 77 +++++ main/src/main/AndroidManifest.xml | 3 + .../CustomerServiceWebViewActivity.java | 320 ++++++++++++++++++ .../yunbao/main/views/MainMeViewHolder.java | 7 +- .../main/views/MainMessageViewHolder.java | 3 +- 7 files changed, 420 insertions(+), 4 deletions(-) create mode 100644 common/src/main/res/layout/activity_webview_cs.xml create mode 100644 main/src/main/java/com/yunbao/main/activity/CustomerServiceWebViewActivity.java diff --git a/common/src/main/java/com/yunbao/common/activity/WebViewActivity.java b/common/src/main/java/com/yunbao/common/activity/WebViewActivity.java index 0d5aa5a9b..2d3e86a75 100644 --- a/common/src/main/java/com/yunbao/common/activity/WebViewActivity.java +++ b/common/src/main/java/com/yunbao/common/activity/WebViewActivity.java @@ -110,10 +110,10 @@ public class WebViewActivity extends AbsActivity { } //真实屏幕高度-(ft_title的高度+导航栏高度) //屏蔽掉是因为在线客服页面 AndroidBug5497Workaround会失效 - /* int height = DeviceUtils.getScreenRealHeight(mContext) - DpUtil.dp2px(72) - getCurrentNavigationBarHeight(mContext); + int height = DeviceUtils.getScreenRealHeight(mContext) - DpUtil.dp2px(72) - getCurrentNavigationBarHeight(mContext); if (!navigationGestureEnabled(mContext)) { view.loadUrl("javascript:window.androidObject.setHeight(" + height + ",0,false)"); - }*/ + } } }); diff --git a/common/src/main/java/com/yunbao/common/utils/RouteUtil.java b/common/src/main/java/com/yunbao/common/utils/RouteUtil.java index 625c1b0fd..9e442e132 100644 --- a/common/src/main/java/com/yunbao/common/utils/RouteUtil.java +++ b/common/src/main/java/com/yunbao/common/utils/RouteUtil.java @@ -38,6 +38,7 @@ public class RouteUtil { public static final String PATH_FANSACTIVITY = "/main/FansActivity"; public static final String PATH_MYWEBVIEWACTIVTITY2 = "/main/MyWebViewActivity2"; public static final String PATH_LOGIN = "/main/LoginActivity"; + public static final String PATH_CS = "/main/CustomerServiceWebViewActivity"; public static void forwardFansActivity(String uid) { ARouter.getInstance().build(PATH_FANSACTIVITY) @@ -251,4 +252,13 @@ public class RouteUtil { ARouter.getInstance().build(PATH_FACEBOOKACTIVITY) .navigation(); } + + /** + * 客服页面 + */ + public static void forwardCustomerService(String url){ + ARouter.getInstance().build(PATH_CS) + .withString(Constants.URL,url) + .navigation(); + } } diff --git a/common/src/main/res/layout/activity_webview_cs.xml b/common/src/main/res/layout/activity_webview_cs.xml new file mode 100644 index 000000000..5b47ab957 --- /dev/null +++ b/common/src/main/res/layout/activity_webview_cs.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/main/src/main/AndroidManifest.xml b/main/src/main/AndroidManifest.xml index 950e8e921..5048ed398 100644 --- a/main/src/main/AndroidManifest.xml +++ b/main/src/main/AndroidManifest.xml @@ -144,6 +144,9 @@ + \ No newline at end of file diff --git a/main/src/main/java/com/yunbao/main/activity/CustomerServiceWebViewActivity.java b/main/src/main/java/com/yunbao/main/activity/CustomerServiceWebViewActivity.java new file mode 100644 index 000000000..ac473703e --- /dev/null +++ b/main/src/main/java/com/yunbao/main/activity/CustomerServiceWebViewActivity.java @@ -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 mValueCallback; + private ValueCallback 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 valueCallback) { + openImageChooserActivity(valueCallback); + } + + // For Android >= 3.0 + public void openFileChooser(ValueCallback valueCallback, String acceptType) { + openImageChooserActivity(valueCallback); + } + + //For Android >= 4.1 + public void openFileChooser(ValueCallback valueCallback, + String acceptType, String capture) { + openImageChooserActivity(valueCallback); + } + + // For Android >= 5.0 + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + @Override + public boolean onShowFileChooser(WebView webView, + ValueCallback 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 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(); + } + } + + } +} diff --git a/main/src/main/java/com/yunbao/main/views/MainMeViewHolder.java b/main/src/main/java/com/yunbao/main/views/MainMeViewHolder.java index 5bc68d577..263ed6408 100644 --- a/main/src/main/java/com/yunbao/main/views/MainMeViewHolder.java +++ b/main/src/main/java/com/yunbao/main/views/MainMeViewHolder.java @@ -615,7 +615,12 @@ public class MainMeViewHolder extends AbsMainViewHolder implements OnItemClickLi if (!bean.getSlide_url().equals("")) { Constants.isTitle = true; String url = bean.getSlide_url() + "?uid=" + CommonAppConfig.getInstance().getUid() + "&token=" + CommonAppConfig.getInstance().getToken(); - ZhuangBanActivity.forward(mContext, url, false); + RouteUtil.forwardCustomerService(url); + /*if(bean.getSlide_id().equals("95")){ + RouteUtil.forwardCustomerService(url); + }else { + ZhuangBanActivity.forward(mContext, url, false); + }*/ } } } diff --git a/main/src/main/java/com/yunbao/main/views/MainMessageViewHolder.java b/main/src/main/java/com/yunbao/main/views/MainMessageViewHolder.java index 26e38cdb7..8a7951187 100644 --- a/main/src/main/java/com/yunbao/main/views/MainMessageViewHolder.java +++ b/main/src/main/java/com/yunbao/main/views/MainMessageViewHolder.java @@ -275,7 +275,8 @@ public class MainMessageViewHolder extends AbsMainViewHolder { FirebaseAnalytics.getInstance(mContext).logEvent("FS_customer_service", null); AppEventsLogger.newLogger(mContext).logEvent("FB_customer_service"); if (userBean.getLink() != null && !"".equals(userBean.getLink())) { - WebViewActivity.forward(mContext, userBean.getLink()); + // WebViewActivity.forward(mContext, userBean.getLink()); + RouteUtil.forwardCustomerService(userBean.getLink()); } else { mContext.startActivity(new Intent(mContext, SystemMessageActivity.class) .putExtra("type", userBean.getType())