VersionUpgradeFragment.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package com.uas.equipment.fragment;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Build;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.support.v4.content.FileProvider;
  8. import android.text.TextUtils;
  9. import android.view.KeyEvent;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import com.alibaba.fastjson.JSON;
  14. import com.alibaba.fastjson.JSONObject;
  15. import com.android.volley.Request;
  16. import com.android.volley.toolbox.StringRequest;
  17. import com.uas.equipment.R;
  18. import com.uas.equipment.global.GloableParams;
  19. import com.uas.equipment.util.AndroidUtil;
  20. import com.uas.equipment.util.CommonUtil;
  21. import com.uas.equipment.util.Constants;
  22. import com.uas.equipment.util.FastjsonUtil;
  23. import com.uas.equipment.util.FileUtils;
  24. import com.uas.equipment.util.HttpCallback;
  25. import com.uas.equipment.util.HttpParams;
  26. import com.uas.equipment.util.VollyRequest;
  27. import com.uas.equipment.util.down.CommonProgressPop;
  28. import com.uas.equipment.util.down.ProgressDownloader;
  29. import com.uas.equipment.util.down.ProgressResponseBody;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import static com.uas.equipment.util.Constants.CONSTANT.APK_FILE_NAME;
  33. import static com.uas.equipment.util.Constants.CONSTANT.APK_FILE_PATH;
  34. /**
  35. * Created by RaoMeng on 2020/4/9
  36. * Desc: 版本升级页面
  37. */
  38. public class VersionUpgradeFragment extends BaseFragment implements ProgressResponseBody.ProgressListener {
  39. private TextView mOldVersionTv, mNewVersionTv, mMsgTextView;
  40. private Button mUpgradeButton;
  41. private CommonProgressPop mProgressPop;
  42. private ProgressDownloader mDownloader;
  43. private long breakPoints, contentLength, totalBytes;
  44. private File file;
  45. private String mAttachId;
  46. private StringRequest mStringRequest;
  47. @Override
  48. protected int getLayout() {
  49. return R.layout.fragment_version_upgrade;
  50. }
  51. @Override
  52. protected void initViews() {
  53. setTitle("版本升级");
  54. mOldVersionTv = root.findViewById(R.id.version_upgrade_oldVersion_tv);
  55. mNewVersionTv = root.findViewById(R.id.version_upgrade_newVersion_tv);
  56. mMsgTextView = root.findViewById(R.id.version_upgrade_msg_tv);
  57. mUpgradeButton = root.findViewById(R.id.version_upgrade_upgrade_btn);
  58. }
  59. @Override
  60. protected void initEvents() {
  61. mUpgradeButton.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. if (mProgressPop == null) {
  65. mProgressPop = new CommonProgressPop(mActivity, "正在下载新版本");
  66. }
  67. mProgressPop.showPopupWindow();
  68. breakPoints = 0L;
  69. File directory = new File(APK_FILE_PATH);
  70. if (!directory.exists() && !directory.isDirectory()) {
  71. boolean mkdirs = directory.mkdirs();
  72. } else {
  73. FileUtils.delAllFile(APK_FILE_PATH);
  74. }
  75. file = new File(APK_FILE_PATH, APK_FILE_NAME);
  76. try {
  77. file.createNewFile();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. String downloadUrl = GloableParams.ADDRESS_COMMON_DOWNLOADBYID + "?id=" + mAttachId;
  82. mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
  83. mDownloader.download(0L);
  84. }
  85. });
  86. }
  87. @Override
  88. protected void initDatas() {
  89. mOldVersionTv.setText(AndroidUtil.getVersionName(mActivity));
  90. getNewVersionMsg();
  91. }
  92. private void getNewVersionMsg() {
  93. progressDialog.show();
  94. // VolleyRequest.getInstance().stringRequest(new HttpParams.Builder()
  95. // .url(GloableParams.ADDRESS_OUTMATERIAL_GETPDAVERSION)
  96. // .method(Request.Method.GET)
  97. // .addParam("code", "UAS_SMART")
  98. // .build(), new HttpCallback() {
  99. // @Override
  100. // public void onSuccess(int flag, Object o) throws Exception {
  101. // progressDialog.dismiss();
  102. // String result = o.toString();
  103. // JSONObject resultObject = JSON.parseObject(result);
  104. // JSONObject dataObject = resultObject.getJSONObject("data");
  105. // if (dataObject != null) {
  106. // mNewVersionTv.setText(FastjsonUtil.getText(dataObject, "pp_versionname"));
  107. // mMsgTextView.setText(FastjsonUtil.getText(dataObject, "pp_remark"));
  108. //
  109. // mAttachId = FastjsonUtil.getText(dataObject, "pp_attach");
  110. // if (!TextUtils.isEmpty(mAttachId)) {
  111. // mUpgradeButton.setVisibility(View.VISIBLE);
  112. // } else {
  113. // mUpgradeButton.setVisibility(View.VISIBLE);
  114. // }
  115. // }
  116. // }
  117. //
  118. // @Override
  119. // public void onFail(int flag, String failStr) throws Exception {
  120. // progressDialog.dismiss();
  121. // CommonUtil.toastNoRepeat(mActivity, failStr);
  122. // }
  123. // });
  124. VollyRequest.getInstance().stringRequest(mStringRequest,
  125. new HttpParams.Builder()
  126. .url(GloableParams.ADDRESS_OUTMATERIAL_GETPDAVERSION)
  127. // .url("http://usoft.f3322.net:11769/ERP/api/pda/outMaterial/getPdaVersion.action")
  128. .method(Request.Method.GET)
  129. .addParam("code", "UAS设备管理")
  130. .build(), new HttpCallback() {
  131. @Override
  132. public void onSuccess(int flag, Object o) throws Exception {
  133. progressDialog.dismiss();
  134. // String result = o.toString();
  135. // JSONObject resultObject = JSON.parseObject(result);
  136. // JSONArray dataArray = resultObject.getJSONArray("data");
  137. // List<VersionUpgradeBean> list=new ArrayList<>();
  138. // list.clear();
  139. // if (dataArray != null && dataArray.size() > 0) {
  140. // for (int i = 0; i < dataArray.size(); i++) {
  141. // JSONObject dataObject = dataArray.getJSONObject(i);
  142. // if (dataObject != null) {
  143. // VersionUpgradeBean jltStorageInBean = new VersionUpgradeBean();
  144. // if (FastjsonUtil.getText(dataObject, "PP_PDACODE").equals("UAS设备管理")){
  145. // jltStorageInBean.setPP_PDACODE(FastjsonUtil.getText(dataObject, "PP_PDACODE"));
  146. // jltStorageInBean.setPP_VERSIONNUM(FastjsonUtil.getText(dataObject, "PP_VERSIONNUM"));
  147. // jltStorageInBean.setPP_ATTACH(FastjsonUtil.getText(dataObject, "PP_ATTACH"));
  148. // jltStorageInBean.setPP_REMARK(FastjsonUtil.getText(dataObject, "PP_REMARK"));
  149. // jltStorageInBean.setPP_VERSIONNAME(FastjsonUtil.getText(dataObject, "PP_VERSIONNAME"));
  150. // list.add(jltStorageInBean);
  151. // mUpgradeButton.setVisibility(View.VISIBLE);
  152. // }else {
  153. // mUpgradeButton.setVisibility(View.GONE);
  154. // }
  155. //
  156. // }
  157. // }
  158. // }
  159. // if (list == null || list.size() == 0){
  160. // mUpgradeButton.setVisibility(View.GONE);
  161. // }else {
  162. // for (int i=0;i<list.size();i++){
  163. // VersionUpgradeBean versionUpgradeBean = list.get(0);
  164. // mNewVersionTv.setText(versionUpgradeBean.getPP_VERSIONNAME());
  165. // mMsgTextView.setText(versionUpgradeBean.getPP_REMARK());
  166. // mAttachId = versionUpgradeBean.getPP_ATTACH();
  167. // if (!TextUtils.isEmpty(mAttachId)) {
  168. // mUpgradeButton.setVisibility(View.VISIBLE);
  169. // } else {
  170. // mUpgradeButton.setVisibility(View.VISIBLE);
  171. // }
  172. // }
  173. // }
  174. String result = o.toString();
  175. JSONObject resultObject = JSON.parseObject(result);
  176. JSONObject dataObject = resultObject.getJSONObject("data");
  177. if (dataObject != null) {
  178. mNewVersionTv.setText(FastjsonUtil.getText(dataObject, "pp_versionname"));
  179. mMsgTextView.setText(FastjsonUtil.getText(dataObject, "pp_remark"));
  180. mAttachId = FastjsonUtil.getText(dataObject, "pp_attach");
  181. if (!TextUtils.isEmpty(mAttachId)) {
  182. mUpgradeButton.setVisibility(View.VISIBLE);
  183. } else {
  184. mUpgradeButton.setVisibility(View.GONE);
  185. }
  186. }
  187. }
  188. @Override
  189. public void onFail(int flag, String failStr) throws Exception {
  190. progressDialog.dismiss();
  191. CommonUtil.toastNoRepeat(mActivity, failStr);
  192. }
  193. });
  194. }
  195. @Override
  196. public void onPreExecute(long contentLength) {
  197. // 文件总长只需记录一次,要注意断点续传后的contentLength只是剩余部分的长度
  198. if (this.contentLength == 0L) {
  199. this.contentLength = contentLength;
  200. if (mProgressPop != null) {
  201. mProgressPop.setProgressMax((int) (contentLength / 1024));
  202. }
  203. }
  204. }
  205. @Override
  206. public void update(long totalBytes, boolean done) {
  207. // 注意加上断点的长度
  208. this.totalBytes = totalBytes + breakPoints;
  209. int progress = (int) (totalBytes + breakPoints) / 1024;
  210. Message message = Message.obtain();
  211. message.what = Constants.CONSTANT.DOWNLOAD_PROGRESS;
  212. message.obj = progress;
  213. mHandler.sendMessage(message);
  214. if (done) {
  215. mHandler.sendEmptyMessage(Constants.CONSTANT.DOWNLOAD_SUCCESS);
  216. }
  217. }
  218. private Handler mHandler = new Handler() {
  219. @Override
  220. public void handleMessage(Message msg) {
  221. switch (msg.what) {
  222. case Constants.CONSTANT.DOWNLOAD_PROGRESS:
  223. try {
  224. int progress = (int) msg.obj;
  225. mProgressPop.setProgress(progress);
  226. // long remain = 100 - (((progress * 1024 * 100) / contentLength));
  227. // if (remain < 0) {
  228. // remain = 0;
  229. // }
  230. } catch (Exception e) {
  231. }
  232. break;
  233. case Constants.CONSTANT.DOWNLOAD_SUCCESS:
  234. CommonUtil.toastNoRepeat(mActivity, "下载完成");
  235. mProgressPop.dismiss();
  236. try {
  237. File apk = new File(APK_FILE_PATH, APK_FILE_NAME);
  238. Intent intent = new Intent(Intent.ACTION_VIEW);
  239. if (Build.VERSION.SDK_INT >= 24) {
  240. Uri apkUri = FileProvider.getUriForFile(mActivity,
  241. "com.uas.pda_smart_sa.fileprovider", apk);
  242. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  243. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  244. intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  245. } else {
  246. intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
  247. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  248. }
  249. mActivity.startActivity(intent);
  250. } catch (Exception e) {
  251. e.printStackTrace();
  252. }
  253. break;
  254. case Constants.CONSTANT.DOWNLOAD_FAILED:
  255. mProgressPop.dismiss();
  256. CommonUtil.makeNotice();
  257. CommonUtil.toastNoRepeat(mActivity, "下载失败");
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. };
  264. @Override
  265. public boolean onKeyDown(int keyCode, KeyEvent event) {
  266. return false;
  267. }
  268. @Override
  269. public boolean onFragmentBackPressed() {
  270. return false;
  271. }
  272. }