From 376dd3cfd7208b30d3ac51491602d8d8e8db3a94 Mon Sep 17 00:00:00 2001 From: huangjian Date: Fri, 10 Dec 2021 16:46:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESimpleWebViewActivity?= =?UTF-8?q?=E6=89=93=E5=BC=80=E9=9A=90=E7=A7=81=E5=8D=8F=E8=AE=AE=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 3 + .../other/dialog/PrivacyAgreementDialog.java | 9 +- .../erban/ui/webview/SimpleJSInterface.java | 123 ++++++++++ .../ui/webview/SimpleWebViewActivity.java | 226 ++++++++++++++++++ .../res/layout/activity_simple_web_view.xml | 85 +++++++ .../xchat_android_core/UriProvider.java | 4 +- 6 files changed, 444 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/yizhuan/erban/ui/webview/SimpleJSInterface.java create mode 100644 app/src/main/java/com/yizhuan/erban/ui/webview/SimpleWebViewActivity.java create mode 100644 app/src/main/res/layout/activity_simple_web_view.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6109eb052..756760194 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -686,6 +686,9 @@ + diff --git a/app/src/main/java/com/yizhuan/erban/other/dialog/PrivacyAgreementDialog.java b/app/src/main/java/com/yizhuan/erban/other/dialog/PrivacyAgreementDialog.java index 666fa278f..58a3e9489 100644 --- a/app/src/main/java/com/yizhuan/erban/other/dialog/PrivacyAgreementDialog.java +++ b/app/src/main/java/com/yizhuan/erban/other/dialog/PrivacyAgreementDialog.java @@ -5,7 +5,6 @@ import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; -import androidx.annotation.NonNull; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; @@ -16,9 +15,11 @@ import android.view.Window; import android.view.WindowManager; import android.widget.TextView; +import androidx.annotation.NonNull; + import com.yizhuan.erban.R; import com.yizhuan.erban.common.widget.OriginalDrawStatusClickSpan; -import com.yizhuan.erban.ui.webview.CommonWebViewActivity; +import com.yizhuan.erban.ui.webview.SimpleWebViewActivity; import com.yizhuan.erban.ui.widget.magicindicator.buildins.UIUtil; import com.yizhuan.xchat_android_core.UriProvider; import com.yizhuan.xchat_android_library.utils.ScreenUtils; @@ -56,14 +57,14 @@ public class PrivacyAgreementDialog extends Dialog implements View.OnClickListen ss.setSpan(new OriginalDrawStatusClickSpan() { @Override public void onClick(@NonNull View widget) { - CommonWebViewActivity.start(getContext(), UriProvider.getPrivacyAgreement()); + SimpleWebViewActivity.start(getContext(), UriProvider.getPrivacyAgreement()); } }, privacyAgreementTipIndex, privacyAgreementTipIndex + privacyAgreementTip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(new ForegroundColorSpan(Color.parseColor("#34a7ff")), userAgreementTipIndex, userAgreementTipIndex + userAgreementTip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); ss.setSpan(new OriginalDrawStatusClickSpan() { @Override public void onClick(@NonNull View widget) { - CommonWebViewActivity.start(getContext(), UriProvider.getUserProtocolUrl()); + SimpleWebViewActivity.start(getContext(), UriProvider.getUserProtocolUrl()); } }, userAgreementTipIndex, userAgreementTipIndex + privacyAgreementTip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); diff --git a/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleJSInterface.java b/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleJSInterface.java new file mode 100644 index 000000000..2c7b64ecd --- /dev/null +++ b/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleJSInterface.java @@ -0,0 +1,123 @@ +package com.yizhuan.erban.ui.webview; + +import android.app.Activity; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.text.TextUtils; +import android.util.Log; +import android.webkit.JavascriptInterface; + +import com.alibaba.fastjson.JSONObject; +import com.orhanobut.logger.Logger; +import com.yizhuan.xchat_android_constants.XChatConstants; +import com.yizhuan.xchat_android_core.auth.AuthModel; +import com.yizhuan.xchat_android_library.utils.AppMetaDataUtil; +import com.yizhuan.xchat_android_library.utils.AppUtils; +import com.yizhuan.xchat_android_library.utils.VersionUtil; + +/** + *

html js 与webview 交互接口

+ * Created by ${user} on 2017/11/6. + */ +public class SimpleJSInterface { + private static final String TAG = SimpleJSInterface.class.getSimpleName(); + private final Context context; + + public SimpleJSInterface(Context context) { + this.context = context; + } + + /** + * 获取设备ID + * + * @return 设备id + */ + @JavascriptInterface + public String getDeviceId() { + return ""; + } + + /** + * 获取uid + * + * @return uid + */ + @JavascriptInterface + public String getUid() { + return ""; + } + + @JavascriptInterface + public void initNav(String json) { + Logger.e("initNav: " + json); + } + + /** + * 关闭浏览器 + */ + @JavascriptInterface + public void closeWebView() { + if (context instanceof Activity) { + ((Activity) context).finish(); + } + } + + /** + * 获取用户ticket + * + * @return + */ + @JavascriptInterface + public String getTicket() { + return ""; + } + + + /** + * 获取应用版本号 + */ + @JavascriptInterface + public String getAppVersion() { + return VersionUtil.getLocalName(context); + } + + /** + * 获取渠道 + * + * @return 渠道名称 + */ + @JavascriptInterface + public String getChannel() { + String channel = ""; + try { + ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); + channel = appInfo.metaData.getString("CHANNEL"); + } catch (Exception e) { + e.printStackTrace(); + } + if (TextUtils.isEmpty(channel)) { + channel = "official"; + } + return channel; + } + + + /** + * 获取 deviceInfo + * + * @return + */ + @JavascriptInterface + public String getDeviceInfo() { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("os", "android"); + jsonObject.put("app", XChatConstants.APP_MARK); + jsonObject.put("appVersion", VersionUtil.getLocalName(context)); + jsonObject.put("appVersionCode", String.valueOf(AppUtils.getVersionCode(context))); + jsonObject.put("channel", getChannel()); + Log.e(TAG, "getDeviceInfo: " + jsonObject); + return jsonObject.toJSONString(); + } + +} diff --git a/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleWebViewActivity.java b/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleWebViewActivity.java new file mode 100644 index 000000000..d5be70047 --- /dev/null +++ b/app/src/main/java/com/yizhuan/erban/ui/webview/SimpleWebViewActivity.java @@ -0,0 +1,226 @@ +package com.yizhuan.erban.ui.webview; + +import android.annotation.SuppressLint; +import android.annotation.TargetApi; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.net.http.SslError; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.text.TextUtils; +import android.view.View; +import android.webkit.SslErrorHandler; +import android.webkit.ValueCallback; +import android.webkit.WebChromeClient; +import android.webkit.WebSettings; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.FrameLayout; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.TextView; + +import androidx.annotation.LayoutRes; +import androidx.appcompat.app.AppCompatActivity; + +import com.netease.nim.uikit.StatusBarUtil; +import com.orhanobut.logger.Logger; +import com.yizhuan.erban.R; +import com.yizhuan.erban.utils.WebViewUtils; + +import java.lang.ref.WeakReference; + + +/** + * 最简单的H5页面,用于隐私协议弹窗,不继承BaseActivity + */ +public class SimpleWebViewActivity extends AppCompatActivity { + + protected FrameLayout layoutTitleBar; + protected WebView webView; + private ProgressBar mProgressBar; + private WebChromeClient wvcc; + private ImageView mIvBack; + private ImageView mIvClose; + private TextView mTitle; + private String url; + + private int mProgress; + + private Handler mHandler = new Handler(); + + private ProgressRunnable mProgressRunnable = new ProgressRunnable(this); + + public static void start(Context context, String url) { + Intent intent = new Intent(context, SimpleWebViewActivity.class); + intent.putExtra("url", url); + context.startActivity(intent); + } + + @SuppressLint("CheckResult") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(getLayoutId()); + Intent intent = getIntent(); + url = intent.getStringExtra("url"); + StatusBarUtil.transparencyBar(this); + initView(); + initData(); + setListener(); + showWebView(url); + } + + @LayoutRes + protected int getLayoutId() { + return R.layout.activity_simple_web_view; + } + + private void setListener() { + mIvBack.setOnClickListener(v -> { + if (webView.canGoBack()) { + webView.goBack(); + } else { + finish(); + } + }); + + mIvClose.setOnClickListener(v -> { + finish(); + }); + } + + @SuppressLint("SetJavaScriptEnabled") + private void initData() { + mHandler.post(mProgressRunnable); + webView.getSettings().setJavaScriptEnabled(true); + webView.getSettings().setUseWideViewPort(true); + webView.getSettings().setLoadWithOverviewMode(true); + // 设置 WebView 可以在 HTTPS 通道上加载 HTTP 资源,Android 4.4 后的暗坑 + // 因为 Android 4.4 后默认不允许在 HTTPS 通道上加载 HTTP 资源 + webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); + webView.getSettings().setTextZoom(100); + SimpleJSInterface jsInterface = new SimpleJSInterface(this); + webView.addJavascriptInterface(jsInterface, "androidJsObj"); + webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); + webView.setWebViewClient(new WebViewClient() { + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + + if (!(url.startsWith("http") || url.startsWith("https"))) { + return true; + } + + view.loadUrl(url); + return true; + } + + @Override + public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { +// super.onReceivedSslError(view, handler, error); //一定要去掉 + // handler.cancel();// Android默认的处理方式 + handler.proceed();// 接受所有网站的证书 + // handleMessage(Message msg);// 进行其他处理 + } + + @Override + public void onPageFinished(WebView view, String url) { + Logger.e("onPageFinished--------" + url); + mProgressBar.setProgress(100); + mProgressBar.setVisibility(View.GONE); + super.onPageFinished(view, url); + } + }); + //获取webviewtitle作为titlebar的title + wvcc = new WebChromeClient() { + + @Override + public void onReceivedTitle(WebView view, String title) { + super.onReceivedTitle(view, title); + mTitle.setText(title + ""); + } + + // For Android >= 4.1 + public void openFileChooser(ValueCallback uploadMsg, + String acceptType, String capture) { + + } + + // For Lollipop 5.0+ Devices + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public boolean onShowFileChooser(WebView mWebView, + ValueCallback filePathCallback, + FileChooserParams fileChooserParams) { + + return false; + } + + }; + // 设置setWebChromeClient对象 + webView.setWebChromeClient(wvcc); + // 设置Webview的user-agent + webView.getSettings().setUserAgentString(webView.getSettings().getUserAgentString() + " tutuAppAndroid erbanAppAndroid"); + } + + private void initView() { + layoutTitleBar = findViewById(R.id.layout_title_bar); + webView = (WebView) findViewById(R.id.webview); + mProgressBar = (ProgressBar) findViewById(R.id.progress_bar); + mIvBack = findViewById(R.id.iv_back); + mIvClose = findViewById(R.id.iv_close); + mTitle = findViewById(R.id.tv_title); + } + + public void showWebView(String url) { + Logger.d("ShowWebView--------" + url); + if (!TextUtils.isEmpty(url)) { + webView.loadUrl(url); + } else { + finish(); + } + } + + @Override + public void onBackPressed() { + if (webView.canGoBack()) { + webView.goBack(); + + } else { + this.finish(); + } + } + + @Override + protected void onDestroy() { + if (mHandler != null) { + mHandler.removeCallbacks(mProgressRunnable); + mProgressRunnable = null; + mHandler = null; + } + super.onDestroy(); + WebViewUtils.releaseWeb(webView); + } + + private static class ProgressRunnable implements Runnable { + private WeakReference mWeakReference; + + ProgressRunnable(SimpleWebViewActivity activity) { + mWeakReference = new WeakReference<>(activity); + } + + @Override + public void run() { + SimpleWebViewActivity activity = mWeakReference.get(); + if (activity == null) return; + if (activity.mProgress < 96) { + activity.mProgress += 3; + activity.mProgressBar.setProgress(activity.mProgress); + activity.mHandler.postDelayed(activity.mProgressRunnable, 10); + } + } + } + +} diff --git a/app/src/main/res/layout/activity_simple_web_view.xml b/app/src/main/res/layout/activity_simple_web_view.xml new file mode 100644 index 000000000..478e796aa --- /dev/null +++ b/app/src/main/res/layout/activity_simple_web_view.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/src/diff_src_erban/java/com/yizhuan/xchat_android_core/UriProvider.java b/core/src/diff_src_erban/java/com/yizhuan/xchat_android_core/UriProvider.java index 06500bfa2..f76ea43ac 100644 --- a/core/src/diff_src_erban/java/com/yizhuan/xchat_android_core/UriProvider.java +++ b/core/src/diff_src_erban/java/com/yizhuan/xchat_android_core/UriProvider.java @@ -45,8 +45,8 @@ public class UriProvider { /** * 默认以下生产环境地址 */ - public static String JAVA_WEB_URL = "http://www.daxiaomao.com"; - public static String IM_SERVER_URL = "https://www.daxiaomao.com"; + public static String JAVA_WEB_URL = "https://api.lecheng163.com/"; + public static String IM_SERVER_URL = "https://api.lecheng163.com/"; /** * @return