|
|
@@ -0,0 +1,207 @@
|
|
|
+package com.yingtang.devicemanager.ui;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.common.data.JSONUtil;
|
|
|
+import com.core.base.BaseActivity;
|
|
|
+import com.core.net.ProgressDownloader;
|
|
|
+import com.core.net.ProgressResponseBody;
|
|
|
+import com.core.utils.CommonUtil;
|
|
|
+import com.core.xmpp.utils.flie.FileUtils;
|
|
|
+import com.me.network.app.base.HttpCallback;
|
|
|
+import com.me.network.app.base.HttpParams;
|
|
|
+import com.me.network.app.http.HttpRequest;
|
|
|
+import com.me.network.app.http.Method;
|
|
|
+import com.uas.appworks.datainquiry.Constants;
|
|
|
+import com.yingtang.devicemanager.Constant;
|
|
|
+import com.yingtang.devicemanager.R;
|
|
|
+import com.yingtang.devicemanager.utils.CookieUtil;
|
|
|
+import com.yingtang.devicemanager.widget.CommonProgressPop;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by jsxiaoshui on 2021/7/19
|
|
|
+ */
|
|
|
+public class VersionUpdateActivity extends BaseActivity implements ProgressResponseBody.ProgressListener{
|
|
|
+ private TextView mOldVersionTv, mNewVersionTv, mMsgTextView;
|
|
|
+ private Button mUpgradeButton;
|
|
|
+ private CommonProgressPop mProgressPop;
|
|
|
+ private ProgressDownloader mDownloader;
|
|
|
+ private long breakPoints, contentLength, totalBytes;
|
|
|
+ private File file;
|
|
|
+ private String mAttachId;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_version_upgrade);
|
|
|
+ setTitle("版本升级");
|
|
|
+ mOldVersionTv = findViewById(R.id.version_upgrade_oldVersion_tv);
|
|
|
+ mNewVersionTv = findViewById(R.id.version_upgrade_newVersion_tv);
|
|
|
+ mMsgTextView = findViewById(R.id.version_upgrade_msg_tv);
|
|
|
+ mUpgradeButton = findViewById(R.id.version_upgrade_upgrade_btn);
|
|
|
+ mUpgradeButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ if (mProgressPop == null) {
|
|
|
+ mProgressPop = new CommonProgressPop(VersionUpdateActivity.this, "正在下载新版本");
|
|
|
+ }
|
|
|
+ mProgressPop.showPopupWindow();
|
|
|
+
|
|
|
+ breakPoints = 0L;
|
|
|
+ File directory = new File(Constant.APK_FILE_PATH);
|
|
|
+ if (!directory.exists() && !directory.isDirectory()) {
|
|
|
+ boolean mkdirs = directory.mkdirs();
|
|
|
+ } else {
|
|
|
+ FileUtils.delAllFile(Constant.APK_FILE_PATH);
|
|
|
+ }
|
|
|
+ file = new File(Constant.APK_FILE_PATH,Constant.APK_FILE_NAME);
|
|
|
+ try {
|
|
|
+ file.createNewFile();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String downloadUrl = CommonUtil.getAppBaseUrl(VersionUpdateActivity.this)+"common/downloadbyId.action" + "?id=" + mAttachId;
|
|
|
+ mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpdateActivity.this);
|
|
|
+ mDownloader.download(0L);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ mOldVersionTv.setText(CommonUtil.ApkVersionName(this));
|
|
|
+ getNewVersionMsg();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getNewVersionMsg() {
|
|
|
+ progressDialog.show();
|
|
|
+ Map<String ,String> headMap= CookieUtil.getHeadersMap(VersionUpdateActivity.this);
|
|
|
+ HttpRequest.getInstance().sendRequest(CommonUtil.getAppBaseUrl(this),
|
|
|
+ new HttpParams.Builder()
|
|
|
+ .url("api/pda/outMaterial/getPdaVersion.action")
|
|
|
+ .addHeader(Constant.HEAD_MASTER,headMap.get(Constant.HEAD_MASTER))
|
|
|
+ .addHeader(Constant.HEAD_USER_NAME,headMap.get(Constant.HEAD_USER_NAME))
|
|
|
+ .addHeader(Constant.HEAD_PWD,headMap.get(Constant.HEAD_PWD))
|
|
|
+ .addHeader(Constant.HEAD_COOKIE,headMap.get(Constant.HEAD_COOKIE))
|
|
|
+ .addHeader(Constant.HEAD_VERSION_CODE,headMap.get(Constant.HEAD_VERSION_CODE))
|
|
|
+ .connectTimeOut(5)
|
|
|
+ .readTimeOut(5)
|
|
|
+ .method(Method.GET)
|
|
|
+ .build(), new HttpCallback() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(int flag, Object o) throws Exception {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ String result = o.toString();
|
|
|
+ JSONObject resultObject = JSON.parseObject(result);
|
|
|
+ JSONObject dataObject = resultObject.getJSONObject("data");
|
|
|
+ if (dataObject != null&&!dataObject.isEmpty()) {
|
|
|
+ mNewVersionTv.setText(JSONUtil.getText(dataObject, "pp_versionname"));
|
|
|
+ mMsgTextView.setText(JSONUtil.getText(dataObject, "pp_remark"));
|
|
|
+
|
|
|
+ mAttachId = JSONUtil.getText(dataObject, "pp_attach");
|
|
|
+ if (!TextUtils.isEmpty(mAttachId)) {
|
|
|
+ mUpgradeButton.setVisibility(View.VISIBLE);
|
|
|
+ } else {
|
|
|
+ mUpgradeButton.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ showToast("线上升级功能暂未开放,请通过线下手动安装APK升级.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFail(int flag, String failStr) throws Exception {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ if(failStr.equals("服务器异常")){
|
|
|
+ showToast("线上升级功能暂未开放,请通过线下手动安装APK升级.");
|
|
|
+ }else{
|
|
|
+ showToast(failStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPreExecute(long contentLength) {
|
|
|
+ if (this.contentLength == 0L) {
|
|
|
+ this.contentLength = contentLength;
|
|
|
+ if (mProgressPop != null) {
|
|
|
+ mProgressPop.setProgressMax((int) (contentLength / 1024));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void update(long totalBytes, boolean done) {
|
|
|
+ // 注意加上断点的长度
|
|
|
+ this.totalBytes = totalBytes + breakPoints;
|
|
|
+ int progress = (int) (totalBytes + breakPoints) / 1024;
|
|
|
+ Message message = Message.obtain();
|
|
|
+ message.what = Constants.CONSTANT.DOWNLOAD_PROGRESS;
|
|
|
+ message.obj = progress;
|
|
|
+ mHandler.sendMessage(message);
|
|
|
+ if (done) {
|
|
|
+ mHandler.sendEmptyMessage(Constants.CONSTANT.DOWNLOAD_SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private Handler mHandler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ switch (msg.what) {
|
|
|
+ case Constants.CONSTANT.DOWNLOAD_PROGRESS:
|
|
|
+ try {
|
|
|
+ int progress = (int) msg.obj;
|
|
|
+ mProgressPop.setProgress(progress);
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case Constants.CONSTANT.DOWNLOAD_SUCCESS:
|
|
|
+ // CommonUtil.toastNoRepeat(mActivity, "下载完成");
|
|
|
+ showToast("下载完成");
|
|
|
+ mProgressPop.dismiss();
|
|
|
+ try {
|
|
|
+ File apk = new File(Constant.APK_FILE_PATH, Constant.APK_FILE_NAME);
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ if (Build.VERSION.SDK_INT >= 24) {
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(VersionUpdateActivity.this,
|
|
|
+ "com.yingtang.devicemanager.fileprovider", apk);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
+ intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
|
|
|
+ } else {
|
|
|
+ intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ }
|
|
|
+ VersionUpdateActivity.this.startActivity(intent);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case Constants.CONSTANT.DOWNLOAD_FAILED:
|
|
|
+ mProgressPop.dismiss();
|
|
|
+ showToast("下载失败");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+}
|