|
@@ -0,0 +1,213 @@
|
|
|
|
+package com.edufound.mobile.activity;
|
|
|
|
+
|
|
|
|
+import android.app.AlertDialog;
|
|
|
|
+import android.content.DialogInterface;
|
|
|
|
+import android.graphics.Bitmap;
|
|
|
|
+import android.net.http.SslError;
|
|
|
|
+import android.os.Build;
|
|
|
|
+import android.os.Bundle;
|
|
|
|
+import android.os.Handler;
|
|
|
|
+import android.os.Message;
|
|
|
|
+import android.view.KeyEvent;
|
|
|
|
+import android.view.View;
|
|
|
|
+import android.webkit.JsResult;
|
|
|
|
+import android.webkit.SslErrorHandler;
|
|
|
|
+import android.webkit.WebChromeClient;
|
|
|
|
+import android.webkit.WebSettings;
|
|
|
|
+import android.webkit.WebView;
|
|
|
|
+import android.webkit.WebViewClient;
|
|
|
|
+import android.widget.ProgressBar;
|
|
|
|
+
|
|
|
|
+import com.edufound.mobile.R;
|
|
|
|
+import com.edufound.mobile.Util.AndroidUtil;
|
|
|
|
+import com.facebook.react.ReactActivity;
|
|
|
|
+import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
|
+import com.facebook.react.bridge.ReactContext;
|
|
|
|
+
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+public class WebActivity extends ReactActivity {
|
|
|
|
+
|
|
|
|
+ ProgressBar mProgressBar;
|
|
|
|
+ WebView mWebView;
|
|
|
|
+ boolean SSL_OK = false;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
+ setContentView(R.layout.activity_web);
|
|
|
|
+ String json = getIntent().getStringExtra("json");
|
|
|
|
+ init();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void init() {
|
|
|
|
+ handler.sendEmptyMessage(1);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Handler handler = new Handler(new Handler.Callback() {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean handleMessage(Message msg) {
|
|
|
|
+ switch (msg.what) {
|
|
|
|
+ case 1:
|
|
|
|
+ mProgressBar = findViewById(R.id.web_progress);
|
|
|
|
+ mProgressBar.setMax(100);
|
|
|
|
+ mWebView = findViewById(R.id.mWebView);
|
|
|
|
+ setWebView();
|
|
|
|
+ mWebView.loadUrl("https://ottweb.ai160.com/stage/index/index.html");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 设置WebView
|
|
|
|
+ public void setWebView() {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= 19) {
|
|
|
|
+ mWebView.getSettings().setLoadsImagesAutomatically(true);
|
|
|
|
+ } else {
|
|
|
|
+ mWebView.getSettings().setLoadsImagesAutomatically(false);
|
|
|
|
+ }
|
|
|
|
+ // android 5.0以上默认不支持Mixed Content
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
+ mWebView.getSettings().setMixedContentMode(
|
|
|
|
+ WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
|
|
|
|
+ }
|
|
|
|
+ mWebView.setFocusable(false);
|
|
|
|
+ mWebView.getSettings().setJavaScriptEnabled(true);
|
|
|
|
+ mWebView.getSettings().setDomStorageEnabled(true);
|
|
|
|
+ // mWebView.getSettings().set
|
|
|
|
+ mWebView.setBackgroundColor(0);
|
|
|
|
+ mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
|
|
|
|
+ mWebView.getSettings().setUseWideViewPort(true);
|
|
|
|
+ mWebView.getSettings().setLoadWithOverviewMode(true);
|
|
|
|
+ mWebView.setWebChromeClient(new WebChromeClient() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
|
|
|
|
+
|
|
|
|
+ return super.onJsAlert(view, url, message, result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onShowCustomView(View view, CustomViewCallback callback) {
|
|
|
|
+ super.onShowCustomView(view, callback);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onProgressChanged(WebView view, int newProgress) {
|
|
|
|
+ mProgressBar.setProgress(newProgress);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ mWebView.setWebViewClient(new WebViewClient() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
+ view.loadUrl(url);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
+ super.onReceivedError(view, errorCode, description, failingUrl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
|
+ mProgressBar.setProgress(0);
|
|
|
|
+ mProgressBar.setVisibility(View.VISIBLE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onPageFinished(WebView view, String url) {
|
|
|
|
+
|
|
|
|
+ if (!mWebView.getSettings().getLoadsImagesAutomatically()) {
|
|
|
|
+ mWebView.getSettings().setLoadsImagesAutomatically(true);
|
|
|
|
+ }
|
|
|
|
+ mProgressBar.setVisibility(View.GONE);
|
|
|
|
+ super.onPageFinished(view, url);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
+ mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
|
+ }
|
|
|
|
+ if (SSL_OK == false) {
|
|
|
|
+ final AlertDialog.Builder builder = new AlertDialog.Builder(WebActivity.this);
|
|
|
|
+ builder.setMessage("证书出现问题,是否继续?");
|
|
|
|
+ builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
|
+ handler.proceed();
|
|
|
|
+ SSL_OK = true;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onClick(DialogInterface dialog, int which) {
|
|
|
|
+ handler.cancel();
|
|
|
|
+ SSL_OK = false;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ final AlertDialog dialog = builder.create();
|
|
|
|
+ dialog.show();
|
|
|
|
+ } else {
|
|
|
|
+ handler.proceed();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
|
+ // TODO Auto-generated method stub
|
|
|
|
+
|
|
|
|
+ switch (keyCode) {
|
|
|
|
+ case KeyEvent.KEYCODE_DPAD_CENTER:
|
|
|
|
+ case KeyEvent.KEYCODE_ENTER:
|
|
|
|
+ // 确认键
|
|
|
|
+ mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_SELECT')");
|
|
|
|
+ break;
|
|
|
|
+ case KeyEvent.KEYCODE_BACK:
|
|
|
|
+ case KeyEvent.KEYCODE_ESCAPE:
|
|
|
|
+ // 返回键
|
|
|
|
+ // 返回键
|
|
|
|
+// mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_BACK')");
|
|
|
|
+ finish();
|
|
|
|
+ return true;
|
|
|
|
+ case KeyEvent.KEYCODE_HOME:
|
|
|
|
+ // Home键 -,- P.S:不一定能监听到
|
|
|
|
+ break;
|
|
|
|
+ case KeyEvent.KEYCODE_DPAD_UP:
|
|
|
|
+ // 上
|
|
|
|
+ mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_UP')");
|
|
|
|
+ break;
|
|
|
|
+ case KeyEvent.KEYCODE_DPAD_RIGHT:
|
|
|
|
+ // 右
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
+ map.put("name", "zzzzzzzzmj");
|
|
|
|
+ map.put("sex", "1");
|
|
|
|
+ map.put("age", "16");
|
|
|
|
+ AndroidUtil.callBackJS(map, "toJsByAndroid");
|
|
|
|
+ mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_RIGHT')");
|
|
|
|
+ break;
|
|
|
|
+ case KeyEvent.KEYCODE_DPAD_DOWN:
|
|
|
|
+ // 下
|
|
|
|
+ mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_DOWN')");
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ case KeyEvent.KEYCODE_DPAD_LEFT:
|
|
|
|
+ // 左
|
|
|
|
+ mWebView.loadUrl("javascript:boxEventHandler('key_down','KEY_LEFT')");
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ return super.onKeyDown(keyCode, event);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|