|
|
@@ -0,0 +1,263 @@
|
|
|
+package com.uas.gdstorage.fragment;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.support.v4.content.FileProvider;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.KeyEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Button;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.android.volley.Request;
|
|
|
+import com.android.volley.toolbox.StringRequest;
|
|
|
+import com.uas.gdstorage.R;
|
|
|
+import com.uas.gdstorage.global.GloableParams;
|
|
|
+import com.uas.gdstorage.util.AndroidUtil;
|
|
|
+import com.uas.gdstorage.util.CommonUtil;
|
|
|
+import com.uas.gdstorage.util.Constants;
|
|
|
+import com.uas.gdstorage.util.FastjsonUtil;
|
|
|
+import com.uas.gdstorage.util.FileUtils;
|
|
|
+import com.uas.gdstorage.util.HttpCallback;
|
|
|
+import com.uas.gdstorage.util.HttpParams;
|
|
|
+import com.uas.gdstorage.util.VollyRequest;
|
|
|
+import com.uas.gdstorage.util.down.CommonProgressPop;
|
|
|
+import com.uas.gdstorage.util.down.ProgressDownloader;
|
|
|
+import com.uas.gdstorage.util.down.ProgressResponseBody;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+import static com.uas.gdstorage.util.Constants.CONSTANT.APK_FILE_NAME;
|
|
|
+import static com.uas.gdstorage.util.Constants.CONSTANT.APK_FILE_PATH;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by RaoMeng on 2020/4/9
|
|
|
+ * Desc: 版本升级页面
|
|
|
+ */
|
|
|
+public class VersionUpgradeFragment extends BaseFragment 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;
|
|
|
+ private StringRequest mStringRequest;
|
|
|
+ @Override
|
|
|
+ protected int getLayout() {
|
|
|
+ return R.layout.fragment_version_upgrade;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initViews() {
|
|
|
+ setTitle("版本升级");
|
|
|
+
|
|
|
+ mOldVersionTv = root.findViewById(R.id.version_upgrade_oldVersion_tv);
|
|
|
+ mNewVersionTv = root.findViewById(R.id.version_upgrade_newVersion_tv);
|
|
|
+ mMsgTextView = root.findViewById(R.id.version_upgrade_msg_tv);
|
|
|
+ mUpgradeButton = root.findViewById(R.id.version_upgrade_upgrade_btn);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initEvents() {
|
|
|
+ mUpgradeButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ if (mProgressPop == null) {
|
|
|
+ mProgressPop = new CommonProgressPop(mActivity, "正在下载新版本");
|
|
|
+ }
|
|
|
+ mProgressPop.showPopupWindow();
|
|
|
+
|
|
|
+ breakPoints = 0L;
|
|
|
+ File directory = new File(APK_FILE_PATH);
|
|
|
+ if (!directory.exists() && !directory.isDirectory()) {
|
|
|
+ boolean mkdirs = directory.mkdirs();
|
|
|
+ } else {
|
|
|
+ FileUtils.delAllFile(APK_FILE_PATH);
|
|
|
+ }
|
|
|
+ file = new File(APK_FILE_PATH, APK_FILE_NAME);
|
|
|
+ try {
|
|
|
+ file.createNewFile();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ String downloadUrl = GloableParams.ADDRESS_COMMON_DOWNLOADBYID + "?id=" + mAttachId;
|
|
|
+ mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
|
|
|
+ mDownloader.download(0L);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void initDatas() {
|
|
|
+ mOldVersionTv.setText(AndroidUtil.getVersionName(mActivity));
|
|
|
+
|
|
|
+ getNewVersionMsg();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getNewVersionMsg() {
|
|
|
+ progressDialog.show();
|
|
|
+
|
|
|
+// VolleyRequest.getInstance().stringRequest(new HttpParams.Builder()
|
|
|
+// .url(GloableParams.ADDRESS_OUTMATERIAL_GETPDAVERSION)
|
|
|
+// .method(Request.Method.GET)
|
|
|
+// .addParam("code", "UAS_SMART")
|
|
|
+// .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) {
|
|
|
+// mNewVersionTv.setText(FastjsonUtil.getText(dataObject, "pp_versionname"));
|
|
|
+// mMsgTextView.setText(FastjsonUtil.getText(dataObject, "pp_remark"));
|
|
|
+//
|
|
|
+// mAttachId = FastjsonUtil.getText(dataObject, "pp_attach");
|
|
|
+// if (!TextUtils.isEmpty(mAttachId)) {
|
|
|
+// mUpgradeButton.setVisibility(View.VISIBLE);
|
|
|
+// } else {
|
|
|
+// mUpgradeButton.setVisibility(View.VISIBLE);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onFail(int flag, String failStr) throws Exception {
|
|
|
+// progressDialog.dismiss();
|
|
|
+// CommonUtil.toastNoRepeat(mActivity, failStr);
|
|
|
+// }
|
|
|
+// });
|
|
|
+
|
|
|
+ VollyRequest.getInstance().stringRequest(mStringRequest,
|
|
|
+ new HttpParams.Builder()
|
|
|
+ .url(GloableParams.ADDRESS_OUTMATERIAL_GETPDAVERSION)
|
|
|
+ // .url("http://usoft.f3322.net:11769/ERP/api/pda/outMaterial/getPdaVersion.action")
|
|
|
+ .method(Request.Method.GET)
|
|
|
+ .addParam("code", "UAS_SMART")
|
|
|
+ .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) {
|
|
|
+ mNewVersionTv.setText(FastjsonUtil.getText(dataObject, "pp_versionname"));
|
|
|
+ mMsgTextView.setText(FastjsonUtil.getText(dataObject, "pp_remark"));
|
|
|
+
|
|
|
+ mAttachId = FastjsonUtil.getText(dataObject, "pp_attach");
|
|
|
+ if (!TextUtils.isEmpty(mAttachId)) {
|
|
|
+ mUpgradeButton.setVisibility(View.VISIBLE);
|
|
|
+ } else {
|
|
|
+ mUpgradeButton.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFail(int flag, String failStr) throws Exception {
|
|
|
+ progressDialog.dismiss();
|
|
|
+ CommonUtil.toastNoRepeat(mActivity, failStr);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPreExecute(long contentLength) {
|
|
|
+ // 文件总长只需记录一次,要注意断点续传后的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);
|
|
|
+// long remain = 100 - (((progress * 1024 * 100) / contentLength));
|
|
|
+// if (remain < 0) {
|
|
|
+// remain = 0;
|
|
|
+// }
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case Constants.CONSTANT.DOWNLOAD_SUCCESS:
|
|
|
+ CommonUtil.toastNoRepeat(mActivity, "下载完成");
|
|
|
+ mProgressPop.dismiss();
|
|
|
+ try {
|
|
|
+ File apk = new File(APK_FILE_PATH, APK_FILE_NAME);
|
|
|
+ Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
+ if (Build.VERSION.SDK_INT >= 24) {
|
|
|
+ Uri apkUri = FileProvider.getUriForFile(mActivity,
|
|
|
+ "com.uas.pda_smart_sa.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);
|
|
|
+ }
|
|
|
+ mActivity.startActivity(intent);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case Constants.CONSTANT.DOWNLOAD_FAILED:
|
|
|
+ mProgressPop.dismiss();
|
|
|
+ CommonUtil.makeNotice();
|
|
|
+ CommonUtil.toastNoRepeat(mActivity, "下载失败");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onFragmentBackPressed() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|