From d0ae3c49d4a8c118ad9ead6841993dec9b999dfd Mon Sep 17 00:00:00 2001
From: hch <16607480311@163.com>
Date: Wed, 25 Oct 2023 17:43:56 +0800
Subject: [PATCH] =?UTF-8?q?=E6=88=91=E7=9A=84=E9=A1=B5=E9=9D=A2=EF=BC=8C?=
=?UTF-8?q?=E8=8F=9C=E5=8D=95=E5=85=A8=E9=83=A8=E6=94=B9=E4=B8=BAwebView?=
=?UTF-8?q?=E8=B7=B3=E8=BD=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
OneToOne/src/main/AndroidManifest.xml | 6 +
.../activity/fragments/MyFragment.java | 12 +-
.../activity/setting/WebViewActivity.java | 443 ++++++++++++++++++
.../src/main/res/layout/activity_webview.xml | 83 +++-
4 files changed, 520 insertions(+), 24 deletions(-)
create mode 100644 OneToOne/src/main/java/com/shayu/onetoone/activity/setting/WebViewActivity.java
diff --git a/OneToOne/src/main/AndroidManifest.xml b/OneToOne/src/main/AndroidManifest.xml
index 84e2ac6ad..cb37dc4dd 100644
--- a/OneToOne/src/main/AndroidManifest.xml
+++ b/OneToOne/src/main/AndroidManifest.xml
@@ -224,6 +224,12 @@
android:label="修改密码"
android:windowSoftInputMode="stateHidden|adjustResize" />
+
+
+
mValueCallback;
+ private ValueCallback mValueCallback2;
+ private int indexInto = 0;
+ private View v_spacing;
+
+ @Override
+ protected int getLayoutId() {
+ return R.layout.activity_webview;
+ }
+
+ @Override
+ protected void main() {
+ String url = getIntent().getStringExtra(Constants.URL);
+ L.e("H5--->" + url);
+ Bus.getOn(this);
+ LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
+ mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
+ btnEdit = (ImageView) findViewById(R.id.btn_edit);
+ ft_title = (FrameLayout) findViewById(R.id.ft_title);
+ v_spacing = (View) findViewById(R.id.v_spacing);
+ mWebView = findViewById(R.id.webView);
+
+ LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
+ params.topMargin = DpUtil.dp2px(1);
+ // 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 {
+ view.loadUrl(url);
+ }
+ return true;
+ }
+
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ setTitle(view.getTitle());
+ if (url.contains("for")) {
+ mWebView.loadUrl("javascript:goAnchorTab()");
+ }
+ //真实屏幕高度-(ft_title的高度+导航栏高度)
+ //屏蔽掉是因为在线客服页面 AndroidBug5497Workaround会失效
+ int height = DeviceUtils.getScreenRealHeight(mContext) - DpUtil.dp2px(72) - getCurrentNavigationBarHeight(mContext);
+ if (!navigationGestureEnabled(mContext)) {
+ view.loadUrl("javascript:window.androidObject.setHeight(" + height + ",0,false)");
+ }
+
+ }
+ });
+ mWebView.setWebChromeClient(new WebChromeClient() {
+ @Override
+ public void onProgressChanged(WebView view, int newProgress) {
+ if (mProgressBar != null) {
+ 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);
+
+
+ if (btnEdit != null) {
+ if (Constants.myIntoIndex == 2) {
+ ft_title.setVisibility(View.GONE);
+ btnEdit.setVisibility(View.GONE);
+ v_spacing.setVisibility(View.VISIBLE);
+
+ } else if (Constants.myIntoIndex == 1) {
+ v_spacing.setVisibility(View.GONE);
+ } else {
+ btnEdit.setVisibility(View.GONE);
+ ft_title.setVisibility(View.VISIBLE);
+ v_spacing.setVisibility(View.GONE);
+ }
+ }
+ }
+
+
+ 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;
+ }
+
+ private static boolean mIsLive = false;
+
+ public static void forward(Context context, String url, boolean addArgs, boolean isLive) {
+ mIsLive = isLive;
+ if (Constants.LoginKefu) {
+ if (addArgs) {
+ url += "&uid=" + CommonAppConfig.getInstance().getUid() + "&token=" + CommonAppConfig.getInstance().getToken();
+ }
+ }
+ Intent intent = new Intent(context, WebViewActivity.class);
+ intent.putExtra(Constants.URL, url + "&isZh=" + ((IMLoginManager.get(CommonAppContext.sInstance.getBaseContext()).getLocaleLanguage() == Locale.SIMPLIFIED_CHINESE) ? "1" : "0"));
+ context.startActivity(intent);
+ }
+
+ public static void forward(Context context, String url, boolean isLive) {
+ forward(context, url, true, isLive);
+ }
+
+
+ @Override
+ protected void onDestroy() {
+ if (mWebView != null) {
+ ViewGroup parent = (ViewGroup) mWebView.getParent();
+ if (parent != null) {
+ parent.removeView(mWebView);
+ }
+ mWebView.destroy();
+ }
+ Constants.myIntoIndex = 0;
+ indexInto = 0;
+ Constants.LoginKefu = true;
+ Bus.getOff(this);
+ 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));
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+ if (Constants.myIntoIndex == 2 && mWebView != null && indexInto == 1) {
+ if (Build.VERSION.SDK_INT >= 21) {
+ mWebView.post(new Runnable() {
+ @Override
+ public void run() {
+ String method = "reload()";//拼接参数,就可以把数据传递给js
+ mWebView.evaluateJavascript(method, new ValueCallback() {
+ @Override
+ public void onReceiveValue(String value) {
+// Log.i("qcl0228", "js返回的数据" + value);
+ }
+ });
+ }
+ });
+ } else {
+ mWebView.post(new Runnable() {
+ @Override
+ public void run() {
+ mWebView.setWebViewClient(new WebViewClient() {
+ @Override
+ public void onPageFinished(WebView view, String url) {
+ super.onPageFinished(view, url);
+ mWebView.loadUrl("javascript:reload()");
+ }
+ });
+
+ }
+ });
+ }
+ indexInto = 0;
+ }
+ if (Constants.isShowPage != -1) {
+ finish();
+ }
+ }
+
+ @Subscribe(threadMode = ThreadMode.MAIN)
+ public void onJavascriptInterfaceEvent(JavascriptInterfaceEvent event) {
+ indexInto = event.getIndexInto();
+ if (event.getMethod().equals("androidInviteShare")) {
+ try {
+ Class> clz = mContext.getClassLoader().loadClass("com.yunbao.share.ui.InvitePopDialog");
+ Object invite = clz.getConstructor(Context.class).newInstance(mContext);
+ invite = invite.getClass().getMethod("setUrl", String.class).invoke(invite, event.getData());
+ assert invite != null;
+ invite.getClass().getMethod("showDialog").invoke(invite);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ } else if (TextUtils.equals(event.getMethod(), "clickLogOffAccount")) {
+ new XPopup.Builder(mContext)
+ .asCustom(new HintCustomPopup(mContext,
+ mContext.getString(R.string.delete_account1),
+ mContext.getString(R.string.delete_account2))
+ .setLiveOpenOk(mContext.getString(R.string.delete_account3))
+ .setLiveOpenCancel(mContext.getString(R.string.cancel))
+ .setCallBack(new HintCustomPopup.HintCustomCallBack() {
+ @Override
+ public void onSure() {
+ LiveNetManager.get(mContext).
+ setLogOff(new HttpCallback() {
+ @Override
+ public void onSuccess(String data) {
+ Stack allActivityStacks = AppManager.getInstance().getAllActivityStacks();
+ for (int i = 0; i < allActivityStacks.size(); i++) {
+ if (!(allActivityStacks.get(i) instanceof WebViewActivity)) {
+ allActivityStacks.get(i).finish();
+ }
+ }
+ IMLoginManager.get(mContext).logout(mContext);
+ CommonAppConfig.getInstance().clearLoginInfo();
+ mContext.finish();
+ RouteUtil.forwardLoginActivity();
+
+ }
+
+ @Override
+ public void onError(String error) {
+
+ }
+ });
+ }
+
+ @Override
+ public void onCancel() {
+
+ }
+ }))
+ .show();
+ } else if (TextUtils.equals(event.getMethod(), "androidMethodLookToLive")) {
+ //看直播
+ gotoLive(event.getLiveId());
+ }
+ }
+
+ private void gotoLive(final String live_id) {
+
+ LiveHttpUtil.getLiveInfo(live_id, new com.yunbao.common.http.HttpCallback() {
+ @Override
+ public void onSuccess(int code, String msg, String[] info) {
+ if (code == 0 && info.length > 0) {
+ LiveBean liveBean = JSON.parseObject(info[0], LiveBean.class);
+ new LiveRoomCheckLivePresenter(mContext, liveBean.getUid(), liveBean.getStream(), new LiveRoomCheckLivePresenter.NewActionListener() {
+ @Override
+ public void onLiveRoomChanged(String liveUid, String stream, int liveType, String liveTypeVal, String liveSdk) {
+ if (liveBean == null) {
+ return;
+ }
+ if (MicStatusManager.getInstance().isMic(liveUid)) {
+ MicStatusManager.getInstance().showDownMicDialog(mContext);
+ return;
+ }
+ if (mIsLive) {
+ finish();
+ if (MicStatusManager.getInstance().isMic(liveUid)) {
+ MicStatusManager.getInstance().showDownMicDialog(mContext);
+ return;
+ }
+ EventBus.getDefault().post(new LiveRoomChangeEvent(liveBean, liveType, Integer.parseInt(liveTypeVal)));
+ } else {
+ RouteUtil.forwardLiveAudienceActivity(liveBean, liveType, Integer.parseInt(liveTypeVal), Integer.parseInt(liveSdk));
+ }
+ finish();
+ }
+
+ @Override
+ public void onCheckError(String contextError) {
+
+ }
+ });
+ } else {
+ RouteUtil.forwardUserHome(mContext, live_id, 0);
+ }
+ }
+ });
+
+
+ }
+}
diff --git a/OneToOne/src/main/res/layout/activity_webview.xml b/OneToOne/src/main/res/layout/activity_webview.xml
index a362d0fee..fcfdb32b0 100644
--- a/OneToOne/src/main/res/layout/activity_webview.xml
+++ b/OneToOne/src/main/res/layout/activity_webview.xml
@@ -1,26 +1,73 @@
-
+ android:orientation="vertical">
-
+ android:layout_height="24dp"
+ android:visibility="gone" />
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
\ No newline at end of file
+ android:layout_width="match_parent"
+ android:layout_marginBottom="15dp"
+ android:layout_height="match_parent" />
+
\ No newline at end of file