PageRenderTime 90ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/gzsll/hupu/util/UpdateAgent.java

https://gitlab.com/gzsll/TLint
Java | 175 lines | 145 code | 25 blank | 5 comment | 10 complexity | a8f061c62a49af7289194f9cdbd01b3d MD5 | raw file
  1. package com.gzsll.hupu.util;
  2. import android.app.Activity;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Environment;
  9. import android.support.v4.app.NotificationCompat;
  10. import android.text.Html;
  11. import android.widget.Toast;
  12. import com.afollestad.materialdialogs.MaterialDialog;
  13. import com.alibaba.fastjson.JSON;
  14. import com.gzsll.hupu.BuildConfig;
  15. import com.gzsll.hupu.Constants;
  16. import com.gzsll.hupu.R;
  17. import com.gzsll.hupu.bean.UpdateInfo;
  18. import com.gzsll.hupu.components.okhttp.OkHttpHelper;
  19. import com.gzsll.hupu.ui.main.MainActivity;
  20. import com.liulishuo.filedownloader.BaseDownloadTask;
  21. import com.liulishuo.filedownloader.FileDownloadListener;
  22. import com.liulishuo.filedownloader.FileDownloader;
  23. import com.yalantis.phoenix.util.Logger;
  24. import java.io.File;
  25. import rx.Observable;
  26. import rx.android.schedulers.AndroidSchedulers;
  27. import rx.functions.Action1;
  28. import rx.functions.Func1;
  29. import rx.schedulers.Schedulers;
  30. /**
  31. * Created by sll on 2015/10/8.
  32. */
  33. public class UpdateAgent {
  34. private OkHttpHelper mOkHttpHelper;
  35. private Activity mActivity;
  36. private NotificationManager mNotifyManager;
  37. private NotificationCompat.Builder mBuilder;
  38. private Uri destinationUri;
  39. public static final String SDCARD_ROOT =
  40. Environment.getExternalStorageDirectory().getAbsolutePath() + "/gzsll/hupu";
  41. public UpdateAgent(OkHttpHelper mOkHttpHelper, Activity mActivity) {
  42. this.mOkHttpHelper = mOkHttpHelper;
  43. this.mActivity = mActivity;
  44. }
  45. public void checkUpdate() {
  46. this.checkUpdate(true);
  47. }
  48. public void checkUpdate(final boolean show) {
  49. mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
  50. mBuilder = new NotificationCompat.Builder(mActivity);
  51. Observable.just(Constants.UPDATE_URL)
  52. .subscribeOn(Schedulers.io())
  53. .map(new Func1<String, UpdateInfo>() {
  54. @Override public UpdateInfo call(String s) {
  55. try {
  56. String result = mOkHttpHelper.getStringFromServer(Constants.UPDATE_URL);
  57. return JSON.parseObject(result, UpdateInfo.class);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<UpdateInfo>() {
  64. @Override public void call(UpdateInfo updateInfo) {
  65. if (updateInfo != null) {
  66. checkUpdateFinished(updateInfo, show);
  67. if (updateInfo.extra != null) {
  68. SettingPrefUtils.setNeedExam(mActivity, updateInfo.extra.needExam == 1);
  69. }
  70. SettingPrefUtils.setHuPuSign(mActivity, updateInfo.hupuSign);
  71. }
  72. }
  73. }, new Action1<Throwable>() {
  74. @Override public void call(Throwable throwable) {
  75. throwable.printStackTrace();
  76. }
  77. });
  78. }
  79. private void checkUpdateFinished(UpdateInfo updateInfo, boolean show) {
  80. if (updateInfo.versionCode > BuildConfig.VERSION_CODE && SettingPrefUtils.getAutoUpdate(
  81. mActivity) && show) {
  82. showUpdateDialog(updateInfo);
  83. }
  84. }
  85. private void showUpdateDialog(final UpdateInfo updateInfo) {
  86. if (updateInfo != null) {
  87. MaterialDialog.Builder builder = new MaterialDialog.Builder(mActivity).title("升级新版本");
  88. builder.positiveText("立刻升级").negativeText("取消").content(Html.fromHtml(updateInfo.updateInfo));
  89. builder.callback(new MaterialDialog.ButtonCallback() {
  90. @Override public void onPositive(MaterialDialog dialog) {
  91. try {
  92. String url = updateInfo.updateUrl;
  93. mBuilder.setContentTitle(mActivity.getString(R.string.app_name) + "正在更新")
  94. .setAutoCancel(true)
  95. .setSmallIcon(mActivity.getPackageManager()
  96. .getPackageInfo(mActivity.getPackageName(), 0).applicationInfo.icon);
  97. destinationUri =
  98. Uri.parse(SDCARD_ROOT + File.separator + FormatUtils.getFileNameFromUrl(url));
  99. FileDownloader.getImpl()
  100. .create(url)
  101. .setPath(SDCARD_ROOT + File.separator + FormatUtils.getFileNameFromUrl(url))
  102. .setListener(listener)
  103. .start();
  104. Toast.makeText(mActivity, "开始下载新版本,稍后会开始安装", Toast.LENGTH_SHORT).show();
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }).show();
  110. }
  111. }
  112. private FileDownloadListener listener = new FileDownloadListener() {
  113. @Override protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  114. }
  115. @Override protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  116. int progress = soFarBytes * 100 / totalBytes;
  117. String content = String.format("正在下载:%1$d%%", progress);
  118. mBuilder.setContentText(content).setProgress(totalBytes, soFarBytes, false);
  119. PendingIntent pendingintent =
  120. PendingIntent.getActivity(mActivity, 0, new Intent(mActivity, MainActivity.class),
  121. PendingIntent.FLAG_CANCEL_CURRENT);
  122. mBuilder.setContentIntent(pendingintent);
  123. mNotifyManager.notify(0, mBuilder.build());
  124. }
  125. @Override protected void blockComplete(BaseDownloadTask task) {
  126. }
  127. @Override protected void completed(BaseDownloadTask task) {
  128. Intent installAPKIntent = new Intent(Intent.ACTION_VIEW);
  129. //如果没有设置SDCard写权限,或者没有sdcard,apk文件保存在内存中,需要授予权限才能安装
  130. FileUtils.chmod("777", destinationUri.getPath());
  131. installAPKIntent.setDataAndType(Uri.parse("file://" + destinationUri.getPath()),
  132. "application/vnd.android.package-archive");
  133. PendingIntent pendingIntent = PendingIntent.getActivity(mActivity, 0, installAPKIntent,
  134. PendingIntent.FLAG_UPDATE_CURRENT);
  135. mBuilder.setContentIntent(pendingIntent);
  136. installAPKIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  137. mActivity.startActivity(installAPKIntent);
  138. // 下载完成
  139. mBuilder.setContentText("下载成功(点击安装)").setProgress(0, 0, false);
  140. mNotifyManager.notify(0, mBuilder.build());
  141. }
  142. @Override protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  143. }
  144. @Override protected void error(BaseDownloadTask task, Throwable e) {
  145. Logger.d("error:" + e.getMessage());
  146. }
  147. @Override protected void warn(BaseDownloadTask task) {
  148. }
  149. };
  150. }