|
|
@@ -0,0 +1,899 @@
|
|
|
+package com.dk.mes.app.activity;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.annotation.TargetApi;
|
|
|
+import android.app.Activity;
|
|
|
+import android.app.ProgressDialog;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.SharedPreferences;
|
|
|
+import android.content.pm.PackageInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.net.Uri;
|
|
|
+import android.net.http.SslError;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.KeyEvent;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.view.WindowManager;
|
|
|
+import android.webkit.SslErrorHandler;
|
|
|
+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.ImageView;
|
|
|
+import android.widget.RelativeLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.annotation.RequiresApi;
|
|
|
+import androidx.core.app.ActivityCompat;
|
|
|
+import androidx.core.content.ContextCompat;
|
|
|
+import androidx.core.content.FileProvider;
|
|
|
+
|
|
|
+import com.dk.mes.app.R;
|
|
|
+import com.dk.mes.app.common.BasePopupWindow;
|
|
|
+import com.dk.mes.app.common.IBossBasePopupWindow;
|
|
|
+import com.dk.mes.app.common.SysApplication;
|
|
|
+import com.dk.mes.app.jsinterface.CustomWebView;
|
|
|
+import com.dk.mes.app.jsinterface.JsInterface;
|
|
|
+import com.dk.mes.app.network.ApiListener;
|
|
|
+import com.dk.mes.app.network.ApiUtil;
|
|
|
+import com.dk.mes.app.network.SetClientParams;
|
|
|
+import com.dk.mes.app.utils.CommonProgressDialog;
|
|
|
+import com.dk.mes.app.utils.LoadingView;
|
|
|
+import com.dk.mes.app.utils.NetStatusUtils;
|
|
|
+import com.google.zxing.integration.android.IntentIntegrator;
|
|
|
+import com.google.zxing.integration.android.IntentResult;
|
|
|
+
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Timer;
|
|
|
+import java.util.TimerTask;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 进入主页
|
|
|
+ */
|
|
|
+public class HomeActivity extends Activity {
|
|
|
+ private static final String TAG = "HomeActivity";
|
|
|
+ // WebView
|
|
|
+ public CustomWebView wvHome;
|
|
|
+ // SharedPreferences
|
|
|
+ private SharedPreferences sharedPreferences;
|
|
|
+ // 网络地址
|
|
|
+ private String homeUrl;
|
|
|
+ //loading
|
|
|
+ private LoadingView loading;
|
|
|
+ //缩放比例 设置了就走设置值 没设置默认一百六
|
|
|
+ private int mInitialScale = 100;
|
|
|
+ // 进度条
|
|
|
+ private ProgressDialog progressDialog;
|
|
|
+ // 退出
|
|
|
+ private BasePopupWindow basePopupWindow;
|
|
|
+ // 报错后的当前url
|
|
|
+ private String mErrorUrl = "";
|
|
|
+ // web当前调用文本框的ref的值
|
|
|
+ public String ref = "";
|
|
|
+ //升级版本弹出框
|
|
|
+ private IBossBasePopupWindow upgradeWindow;
|
|
|
+ // 下载窗体
|
|
|
+ private static CommonProgressDialog mDialog = null;
|
|
|
+ public static final int MAX_RETRY = 10;
|
|
|
+ // InputStream
|
|
|
+ private InputStream input;
|
|
|
+ // HttpURLConnection
|
|
|
+ private HttpURLConnection connection;
|
|
|
+ // apk大小
|
|
|
+ private int contentlength;
|
|
|
+ // 用于决定seek方法中是否执行文件定位
|
|
|
+ private long cur_pos;
|
|
|
+ // apk安装文件目录
|
|
|
+ private String installFilePath;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
|
|
|
+ WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
|
|
|
+
|
|
|
+ setContentView(R.layout.activity_home);
|
|
|
+ //取消状态栏
|
|
|
+ getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
|
|
|
+ WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
|
|
|
+ sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
|
|
|
+ homeUrl = sharedPreferences.getString("homeUrl", "");
|
|
|
+ //检查权限
|
|
|
+ checkPermission();
|
|
|
+ //初始化view
|
|
|
+ initView();
|
|
|
+ // 设置WebView的客户端
|
|
|
+ initWebView();
|
|
|
+ //检查更新
|
|
|
+ CheckUp();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 点击返回上一页面而不是退出浏览器
|
|
|
+ *
|
|
|
+ * @param keyCode
|
|
|
+ * @param event
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
+ if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && wvHome.canGoBack()) {
|
|
|
+ String currentUrl = wvHome.getUrl();
|
|
|
+ if ((currentUrl.contains("/app/index")
|
|
|
+ || (currentUrl.contains("/app/other") && !currentUrl.contains("/app/other-"))
|
|
|
+ || (currentUrl.contains("/app/report") && !currentUrl.contains("/app/report-"))
|
|
|
+ || currentUrl.contains("/app/mine"))) {
|
|
|
+ // 主页 弹出是否退出的窗体退出
|
|
|
+ this.backPressed();
|
|
|
+ return true;
|
|
|
+ } else if (currentUrl.contains("/app/login")) {
|
|
|
+ // 登录页 直接退出
|
|
|
+ } else {
|
|
|
+ wvHome.goBack();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return super.onKeyDown(keyCode, event);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+
|
|
|
+ IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
|
|
|
+ if (result != null) {
|
|
|
+ if (result.getContents() == null) {
|
|
|
+ Toast.makeText(this, "扫描结果为空", Toast.LENGTH_SHORT).show();
|
|
|
+ } else {
|
|
|
+ String code = result.getContents();
|
|
|
+ if (wvHome != null) {
|
|
|
+ String script = String.format("javascript:getQRCodeScan(\"" + code + "\",\"" + this.ref + "\")"); // 处理返回值
|
|
|
+ wvHome.loadUrl(script);
|
|
|
+ wvHome.evaluateJavascript(script, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 销毁Webview
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ if (wvHome != null) {
|
|
|
+ wvHome.loadDataWithBaseURL(null, "", "text/html",
|
|
|
+ "utf-8", null);
|
|
|
+ wvHome.clearHistory();
|
|
|
+ ((ViewGroup) wvHome.getParent()).removeView(wvHome);
|
|
|
+ wvHome.destroy();
|
|
|
+ wvHome = null;
|
|
|
+ }
|
|
|
+ super.onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 退出函数
|
|
|
+ */
|
|
|
+ public void backPressed() {
|
|
|
+ basePopupWindow = new BasePopupWindow(HomeActivity.this, R.layout.popup_window_exit);
|
|
|
+ basePopupWindow.setPopUpWindowCallBack(new BasePopupWindow.IPopUpWindowCallBack() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void popUpWindowCallBack(View view) {
|
|
|
+ TextView tvPopupWindowMessage = (TextView) view.findViewById(R.id.tv_popup_window_message);
|
|
|
+ TextView tvPopupWindowTitle = (TextView) view.findViewById(R.id.tv_popup_window_title);
|
|
|
+ tvPopupWindowTitle.setText("退出程序");
|
|
|
+ try {
|
|
|
+ tvPopupWindowMessage.setText("是否要退出当前登录?");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ // 对布局文件进行初始化
|
|
|
+ RelativeLayout llCancel = (RelativeLayout) view.findViewById(R.id.ll_cancel);
|
|
|
+ // 对布局中的控件添加事件监听
|
|
|
+ llCancel.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ basePopupWindow.dismiss();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ RelativeLayout llOK = (RelativeLayout) view.findViewById(R.id.ll_ok);
|
|
|
+ // 对布局中的控件添加事件监听
|
|
|
+ llOK.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ if (basePopupWindow != null) {
|
|
|
+ basePopupWindow.dismiss();
|
|
|
+ }
|
|
|
+ SysApplication.getInstance().exit();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ basePopupWindow.show(false, findViewById(R.id.layout_home_content), 0, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查权限
|
|
|
+ */
|
|
|
+ private void checkPermission() {
|
|
|
+ if (Build.VERSION.SDK_INT >= 23) {
|
|
|
+ int checkCallPhonePermission = ContextCompat.checkSelfPermission(
|
|
|
+ HomeActivity.this, Manifest.permission.CAMERA);
|
|
|
+ if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ ActivityCompat.requestPermissions(HomeActivity.this,
|
|
|
+ new String[]{Manifest.permission.CAMERA, Manifest.permission.VIBRATE}, 333);
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 500毫秒后调用关闭加载的方法
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ */
|
|
|
+ private void loadingView() {//点击加载并按钮模仿网络请求
|
|
|
+ loading = new LoadingView(this, R.style.CustomDialog);
|
|
|
+ loading.show();
|
|
|
+ new Handler().postDelayed(new Runnable() {//定义延时任务模仿网络请求
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ loading.dismiss();//3秒后调用关闭加载的方法
|
|
|
+ }
|
|
|
+ }, 500);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置WebView的客户端
|
|
|
+ */
|
|
|
+ private void initWebView() {
|
|
|
+ WebSettings webSettings = wvHome.getSettings();
|
|
|
+ webSettings.setSavePassword(false);
|
|
|
+ webSettings.setSaveFormData(false);
|
|
|
+ //支持插件
|
|
|
+// webSettings.setPluginsEnabled(true);
|
|
|
+ // 让WebView能够执行javaScript
|
|
|
+ webSettings.setJavaScriptEnabled(true);
|
|
|
+ // 让JavaScript可以自动打开windows
|
|
|
+ webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
|
|
|
+ // 设置缓存模式,一共有四种模式
|
|
|
+// webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
|
|
+ if (NetStatusUtils.isConnected(getApplicationContext())) {
|
|
|
+ webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
|
|
|
+ } else {
|
|
|
+ webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网,则从本地获取,即离线加载
|
|
|
+ }
|
|
|
+ // 设置缓存
|
|
|
+ webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
|
|
|
+ webSettings.setAppCacheMaxSize(1024 * 1024 * 8);
|
|
|
+ webSettings.setAllowFileAccess(true);
|
|
|
+ webSettings.setDatabaseEnabled(true); //开启 database storage API 功能
|
|
|
+ webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能
|
|
|
+ // 设置缓存路径
|
|
|
+// String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
|
|
|
+ //取得缓存路径
|
|
|
+ String appCachePath = getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath();
|
|
|
+ webSettings.setAppCachePath(appCachePath);
|
|
|
+ //设置数据库缓存路径
|
|
|
+ webSettings.setDatabasePath(appCachePath);
|
|
|
+ // 支持缩放(适配到当前屏幕)--放得开就是支持
|
|
|
+// webSettings.setSupportZoom(true);
|
|
|
+// webSettings.setBuiltInZoomControls(true);
|
|
|
+// webSettings.setDisplayZoomControls(false);
|
|
|
+ // 全屏显示
|
|
|
+ webSettings.setLoadWithOverviewMode(true);
|
|
|
+// // 将图片调整到合适的大小 将图片调整到适合webview的大小
|
|
|
+ webSettings.setUseWideViewPort(true);
|
|
|
+ // 支持内容重新布局,一共有四种方式
|
|
|
+ // 默认的是NARROW_COLUMNS
|
|
|
+ webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
|
|
|
+ webSettings.setLoadsImagesAutomatically(true); //-> 是否自动加载图片
|
|
|
+ webSettings.setDefaultTextEncodingName("UTF-8"); //-> 设置编码格式
|
|
|
+ // 设置默认字体大小
|
|
|
+ webSettings.setDefaultFontSize(16);
|
|
|
+ webSettings.setTextZoom(100);
|
|
|
+// 在代码中设置WebView允许自动播放声音
|
|
|
+ webSettings.setMediaPlaybackRequiresUserGesture(false);
|
|
|
+ // 远程url是https协议,图片资源是http协议时。
|
|
|
+ // 在Android 5.0之后,WebView默认不允许Https+Http的混合使用方式,
|
|
|
+ // 所以当Url是Https的,图片资源是Http时,导致页面加载失败
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ wvHome.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
+ }
|
|
|
+ String url = homeUrl;
|
|
|
+ // 补全http头
|
|
|
+ if (!url.toLowerCase().contains("http://")
|
|
|
+ && !url.toLowerCase().contains("https://")) {
|
|
|
+ url = "http://" + url;
|
|
|
+ }
|
|
|
+// if (!url.toLowerCase().contains("app/login")) {
|
|
|
+// url = new File(url, "app/login").getPath();
|
|
|
+// }
|
|
|
+
|
|
|
+ wvHome.setInitialScale(mInitialScale);
|
|
|
+ wvHome.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
|
+ wvHome.loadUrl(url);
|
|
|
+ JsInterface jsInterface = new JsInterface(HomeActivity.this);
|
|
|
+ wvHome.addJavascriptInterface(jsInterface, jsInterface.toString());
|
|
|
+ wvHome.setWebChromeClient(new WebChromeClient() {
|
|
|
+ //For 5.0+
|
|
|
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
|
|
+ @Override
|
|
|
+ public boolean onShowFileChooser(WebView webView,
|
|
|
+ ValueCallback<Uri[]> filePathCallback,
|
|
|
+ WebChromeClient.FileChooserParams fileChooserParams) {
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onReceivedTitle(WebView view, String title) {
|
|
|
+ super.onReceivedTitle(view, title);
|
|
|
+ // android 6.0 以下通过title获取
|
|
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
+ if (title.contains("404") || title.contains("500") || title.contains("Error")) {
|
|
|
+ view.loadUrl("file:///android_asset/404.html");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+ // 设置WebView的客户端
|
|
|
+ wvHome.setWebViewClient(new WebViewClient() {
|
|
|
+ private ProgressDialog progressDialog;
|
|
|
+ private Handler mHandler = new Handler();//超时之后的处理Handler
|
|
|
+ private Timer timer = new Timer();
|
|
|
+ ;//计时器
|
|
|
+ private long timeout = 3000;//超时时间
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网页页面开始加载的时候
|
|
|
+ * @param view
|
|
|
+ * @param url
|
|
|
+ * @param favicon
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
+ if (view == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /* * 超时后,首先判断页面加载是否小于100,就执行超时后的动作 */
|
|
|
+ TimerTask tt = new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ if (HomeActivity.this.wvHome.getProgress() > timeout) {
|
|
|
+ mHandler.sendEmptyMessage(0x101);
|
|
|
+ timer.cancel();
|
|
|
+ timer.purge();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ //页面加载中,请稍后
|
|
|
+ if (progressDialog == null) {
|
|
|
+ progressDialog = new ProgressDialog(view.getContext());
|
|
|
+ progressDialog.setMessage("页面加载中,请稍后。。。");
|
|
|
+ progressDialog.show();
|
|
|
+ if (view != null) {
|
|
|
+ view.setEnabled(false);// 当加载网页的时候将网页进行隐藏
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+//
|
|
|
+ super.onPageStarted(view, url, favicon);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 网页加载结束的时候
|
|
|
+ @Override
|
|
|
+ public void onPageFinished(WebView view, String url) {
|
|
|
+ if (progressDialog != null && progressDialog.isShowing()) {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ progressDialog = null;
|
|
|
+ view.setEnabled(true);
|
|
|
+ }
|
|
|
+ timer.cancel();
|
|
|
+ timer.purge();
|
|
|
+ super.onPageFinished(view, url);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
+ if (TextUtils.isEmpty(url.replace("file:///", "")) && !TextUtils.isEmpty(mErrorUrl)) {
|
|
|
+ view.loadUrl(mErrorUrl);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ view.loadUrl(url);
|
|
|
+ view.reload();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @TargetApi(Build.VERSION_CODES.M)
|
|
|
+ @Override
|
|
|
+ public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
|
|
|
+ super.onReceivedHttpError(view, request, errorResponse);
|
|
|
+ int statusCode = errorResponse.getStatusCode();
|
|
|
+ if (404 == statusCode || 500 == statusCode) {
|
|
|
+ view.loadUrl("file:///android_asset/404.html");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
+ super.onReceivedError(view, errorCode, description, failingUrl);
|
|
|
+ mErrorUrl = view.getUrl();
|
|
|
+ // 断网或者网络连接超时
|
|
|
+ if (errorCode == ERROR_HOST_LOOKUP || errorCode == ERROR_CONNECT || errorCode == ERROR_TIMEOUT) {
|
|
|
+ view.loadUrl("file:///android_asset/404.html");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 远程url是https协议, 证书问题,需重写onReceivedSslError方法
|
|
|
+ @Override
|
|
|
+ public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
|
|
|
+ super.onReceivedSslError(view, handler, error);
|
|
|
+ //https忽略证书问题
|
|
|
+ if (handler != null) {
|
|
|
+ //表示等待证书响应
|
|
|
+ handler.proceed();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 报错后的当前url
|
|
|
+ */
|
|
|
+ public void toRefreshUrl() {
|
|
|
+ wvHome.loadUrl(mErrorUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前url
|
|
|
+ */
|
|
|
+ public String getHomeUrl() {
|
|
|
+ return homeUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前版本
|
|
|
+ */
|
|
|
+ public String getVersion() {
|
|
|
+ try{
|
|
|
+ return this.getVersionName();
|
|
|
+ }catch (Exception e){
|
|
|
+ return "1.0.0.0";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前手机厂商
|
|
|
+ */
|
|
|
+ public String getManufacture() {
|
|
|
+ return Build.MANUFACTURER.toLowerCase();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化view
|
|
|
+ */
|
|
|
+ private void initView() {
|
|
|
+ wvHome = (CustomWebView) findViewById(R.id.wvHome);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查更新
|
|
|
+ */
|
|
|
+ private void CheckUp() {
|
|
|
+ //调服务器接口查看服务器版本号
|
|
|
+ HashMap<String, String> params = new HashMap<>(); //参数
|
|
|
+ params.put("code", "webview");//原生参数
|
|
|
+ String url = getUpdateHomeUrl("api/mdm-server/app/mst/common/get_apk_version");
|
|
|
+ Log.e(TAG, "CheckUp " + url);
|
|
|
+ new SetClientParams(url, params).post(new ApiListener() {
|
|
|
+ @Override
|
|
|
+ public void success(ApiUtil apiUtil) throws Exception {
|
|
|
+ JSONObject apiUtilData = new JSONObject(apiUtil.data);
|
|
|
+ contentlength = apiUtilData.optInt("apksize");
|
|
|
+ //请求 成功
|
|
|
+ String serverVersionNo = apiUtilData.optString("version").replace(".", "");// 服务器上的app版本号
|
|
|
+ String localVersionNo = getVersionName().replace(".", ""); //本地版本号
|
|
|
+ boolean check = CompareServerVersion(localVersionNo, serverVersionNo); //对比老旧版本号 决定是否升级
|
|
|
+ if (check) { // 可以升级 弹出提示框
|
|
|
+ upgradeWindow = new IBossBasePopupWindow(HomeActivity.this,
|
|
|
+ R.layout.popup_window_exit);
|
|
|
+ upgradeWindow.setPopUpWindowCallBack(new IBossBasePopupWindow.IPopUpWindowCallBack() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void popUpWindowCallBack(View view) {
|
|
|
+ TextView tvPopupWindowMessage = (TextView) view
|
|
|
+ .findViewById(R.id.tv_popup_window_message);
|
|
|
+ TextView tvPopupWindowTitle = (TextView) view
|
|
|
+ .findViewById(R.id.tv_popup_window_title);
|
|
|
+
|
|
|
+ tvPopupWindowTitle.setText("提示");
|
|
|
+ tvPopupWindowMessage.setText("发现新版本,请立刻升级!!! ");
|
|
|
+
|
|
|
+ // 对布局文件进行初始化
|
|
|
+ RelativeLayout llCancel = (RelativeLayout) view
|
|
|
+ .findViewById(R.id.ll_cancel);
|
|
|
+ // 对布局中的控件添加事件监听
|
|
|
+ TextView tvCancel = (TextView) llCancel
|
|
|
+ .findViewById(R.id.tv_popup_window_cancel);
|
|
|
+ tvCancel.setText("取消");
|
|
|
+
|
|
|
+ llCancel.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+
|
|
|
+ upgradeWindow.dismiss();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ RelativeLayout llOK = (RelativeLayout) view.findViewById(R.id.ll_ok);
|
|
|
+ // 对布局中的控件添加事件监听
|
|
|
+
|
|
|
+ TextView tvOk = (TextView) llOK.findViewById(R.id.tv_popup_window_ok);
|
|
|
+ tvOk.setText("确定");
|
|
|
+ llOK.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+
|
|
|
+ ShowDownloadDialog();
|
|
|
+ upgradeWindow.dismiss();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ upgradeWindow.show(false, findViewById(R.id.layout_home_content), 0, 0); //不懂的 可以点击进show方法看详细说明
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void failrure(ApiUtil apiUtil) {
|
|
|
+ //请求失败 弹吐司
|
|
|
+ Log.e(apiUtil.TAG, apiUtil.toString());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载窗体
|
|
|
+ */
|
|
|
+ private void ShowDownloadDialog() {
|
|
|
+ mDialog = new CommonProgressDialog(this);
|
|
|
+ mDialog.setIndeterminate(false);
|
|
|
+ mDialog.setCanceledOnTouchOutside(false);
|
|
|
+ mDialog.setCancelable(false);
|
|
|
+ mDialog.show();
|
|
|
+ UpdateThread ut = new UpdateThread();
|
|
|
+ ut.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载apk的线程
|
|
|
+ */
|
|
|
+ public class UpdateThread extends Thread {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ String pPath = getRootFilePath();
|
|
|
+ String fileDir = pPath + "/mes/";
|
|
|
+ File file = new File(fileDir);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdir();
|
|
|
+ }
|
|
|
+ String filep = "mes.apk";
|
|
|
+// File f = new File("data/data/com.dk.mes.app/mes.apk");
|
|
|
+ File f = new File(file.getAbsolutePath(), filep);
|
|
|
+ if (!f.exists()) {
|
|
|
+ try {
|
|
|
+ f.createNewFile();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ installFilePath = f.getAbsolutePath();
|
|
|
+ String url = getUpdateHomeUrl("api/mdm-server/app/mst/common/download_apk");
|
|
|
+ Log.e(TAG, "downloadFileStream" + url);
|
|
|
+ downloadFileStream(url, installFilePath, "?code=webview");
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ Message m = new Message();
|
|
|
+ m.what = 2;
|
|
|
+ Bundle b = new Bundle();
|
|
|
+ b.putString("message", "网络连接异常");
|
|
|
+ m.setData(b);
|
|
|
+ h.sendMessage(m);
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * url
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getUpdateHomeUrl(String fun) {
|
|
|
+ // 补全http头
|
|
|
+ String url = homeUrl;
|
|
|
+ url = new File(url.replace("/app/login", ""), fun).getPath();
|
|
|
+ if (!url.toLowerCase().contains("http:/")
|
|
|
+ && !url.toLowerCase().contains("https:/")) {
|
|
|
+ url = "http://" + url;
|
|
|
+ }
|
|
|
+
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载数据流
|
|
|
+ *
|
|
|
+ * @param url
|
|
|
+ * @param filePath
|
|
|
+ * @param content
|
|
|
+ */
|
|
|
+ public void downloadFileStream(String url, String filePath, String content) {
|
|
|
+ FileOutputStream output = null;
|
|
|
+ try {
|
|
|
+ int retry = 0;
|
|
|
+ while (retry < MAX_RETRY)
|
|
|
+ try {
|
|
|
+ seek(0, url, content);
|
|
|
+ break;
|
|
|
+ } catch (Exception e) {
|
|
|
+ retry++;
|
|
|
+ }
|
|
|
+
|
|
|
+ File wdFile = new File(filePath);
|
|
|
+ output = new FileOutputStream(wdFile);
|
|
|
+ input = connection.getInputStream();
|
|
|
+ // 这将是有用的,这样你可以显示一个典型的0-100%的进度条
|
|
|
+ // 下载文件
|
|
|
+ mDialog.mProgress.setMax(contentlength);
|
|
|
+ byte data[] = new byte[1024];
|
|
|
+ int count = 0;
|
|
|
+ do {
|
|
|
+ int numread = input.read(data);
|
|
|
+ count += numread;
|
|
|
+ Message m = new Message();
|
|
|
+ Bundle b = new Bundle();
|
|
|
+ b.putInt("process", count);
|
|
|
+ m.what = 3;
|
|
|
+ m.setData(b);
|
|
|
+ h.sendMessage(m);
|
|
|
+ if (numread <= 0) {
|
|
|
+ // 下载完成通知安装
|
|
|
+ h.sendEmptyMessage(4);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ output.write(data, 0, numread);
|
|
|
+ } while (true);
|
|
|
+
|
|
|
+ output.flush();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ output.close();
|
|
|
+ input.close();
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求连接
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @param conurl
|
|
|
+ * @param content
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public void seek(long start_pos, String conurl, String content)
|
|
|
+ throws IOException {
|
|
|
+ if (start_pos == cur_pos && input != null)
|
|
|
+ return;
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ connection = null;
|
|
|
+ }
|
|
|
+ if (input != null) {
|
|
|
+ input.close();
|
|
|
+ input = null;
|
|
|
+ }
|
|
|
+ URL url = new URL(conurl + content);
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json");
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setConnectTimeout(180 * 1000);
|
|
|
+ connection.setReadTimeout(180 * 1000);
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setRequestProperty("Accept-Encoding", "identity");
|
|
|
+ System.setProperty("http.keepAlive", "false");
|
|
|
+ connection.connect();
|
|
|
+// OutputStream outputStream = connection.getOutputStream();
|
|
|
+// outputStream.write(content.getBytes());
|
|
|
+// outputStream.close();
|
|
|
+ // httpConnection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ if (responseCode < 200 || responseCode >= 300) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(500);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ throw new IOException("HTTP responseCode=" + responseCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ input = connection.getInputStream();
|
|
|
+ cur_pos = start_pos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取路径
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getRootFilePath() {
|
|
|
+ return getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否包含sd卡
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean hasSDCard() {
|
|
|
+ String status = Environment.getExternalStorageState();
|
|
|
+ if (!status.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Handler处理结构
|
|
|
+ */
|
|
|
+ Handler h = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ switch (msg.what) {
|
|
|
+ case 2:
|
|
|
+ Bundle b3 = msg.getData();
|
|
|
+ String message3 = b3.getString("message");
|
|
|
+ Toast.makeText(getApplicationContext(), message3, Toast.LENGTH_SHORT);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 3:
|
|
|
+ Bundle bu = msg.getData();
|
|
|
+ int progress = bu.getInt("process");
|
|
|
+ mDialog.mProgress.setProgress(progress);
|
|
|
+ mDialog.setProgressNumber();
|
|
|
+ mDialog.setProgressPercent();
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ installApk(installFilePath, HomeActivity.this);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 安装apk
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ * @since 2016年12月7日
|
|
|
+ */
|
|
|
+ public void installApk(String installFilePath, Context context) {
|
|
|
+ File apkfile = new File(installFilePath);
|
|
|
+ if (!apkfile.exists()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ if (Build.VERSION.SDK_INT >= 24) {
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkfile);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
+ } else {
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
|
|
|
+ }
|
|
|
+ context.startActivity(intent);
|
|
|
+ android.os.Process.killProcess(android.os.Process.myPid());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 版本比较
|
|
|
+ *
|
|
|
+ * @author Administrator
|
|
|
+ * @since 2016年12月7日
|
|
|
+ */
|
|
|
+ public static boolean CompareServerVersion(String localVersion, String ServerVersion) {
|
|
|
+ boolean b = false;
|
|
|
+ String localVersionNo = localVersion.replace(".", "");
|
|
|
+ String serverVersionNo = ServerVersion.replace(".", "");
|
|
|
+ if (localVersionNo.startsWith("0")) {
|
|
|
+ localVersionNo = localVersionNo.replaceFirst("^0*", "");
|
|
|
+ }
|
|
|
+ if (serverVersionNo.startsWith("0")) {
|
|
|
+ serverVersionNo = serverVersionNo.replaceFirst("^0*", "");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Long.parseLong(serverVersionNo) > Long.parseLong(localVersionNo)) {
|
|
|
+ b = true;
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取app版本号
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private String getVersionName() throws Exception {
|
|
|
+
|
|
|
+ // 获取packagemanager的实例
|
|
|
+ PackageManager packageManager = getPackageManager();
|
|
|
+ // getPackageName()是你当前类的包名,0代表是获取版本信息
|
|
|
+ PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
|
|
|
+ String version = packInfo.versionName;
|
|
|
+ return version;
|
|
|
+ }
|
|
|
+}
|