APKUpdateDownload.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.emato.ich.update;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.util.Log;
  5. import com.emato.ich.api.ICSPClient;
  6. import com.emato.ich.utils.BaseUtils;
  7. import org.jetbrains.annotations.NotNull;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Date;
  14. import okhttp3.Call;
  15. import okhttp3.Callback;
  16. import okhttp3.Response;
  17. public class APKUpdateDownload {
  18. private static final String TAG = APKUpdateDownload.class.getName();
  19. private static final String destFileDir = "res/apk/";
  20. private static String destFileName = "ich-android.apk";
  21. private static final APKUpdateDownload INSTANCE = APKUpdateDownloadInnerClass.apkUpdateDownload;
  22. private APKUpdateDownload(){}
  23. public static APKUpdateDownload getInstance(){
  24. return INSTANCE;
  25. }
  26. static class APKUpdateDownloadInnerClass{
  27. private static final APKUpdateDownload apkUpdateDownload = new APKUpdateDownload();
  28. }
  29. public void downloadAPK(Activity activity, Context context, String apk_url) {
  30. OnDownloadListener listener = new OnDownloadListener() {
  31. @Override
  32. public void onDownloadSuccess(File file) {
  33. Log.i(TAG, "onDownloadSuccess: 下载完成! ");
  34. // 文件路径
  35. String absolutePath = file.getAbsolutePath();
  36. // 需要自动安装并重启
  37. PackageManagerCompat.install(context, absolutePath, context.getPackageManager(), new OnInstallListener() {
  38. @Override
  39. public void onInstallException(Exception e) {
  40. Log.e(TAG, "onInstallException: 安装出现异常! " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), e);
  41. }
  42. @Override
  43. public void onInstallSuccess() {
  44. // TODO 重启应用 重启失败? 一直重启?
  45. // PackageManagerCompat.restartApp(context);
  46. }
  47. });
  48. }
  49. @Override
  50. public void onDownloading(int progress) {
  51. Log.i(TAG, "onDownloading: 下载进度========>" + progress + "%");
  52. }
  53. @Override
  54. public void onDownloadFailed(Exception e) {
  55. Log.e(TAG, "onDownloadFailed: 下载失败! 原因==> ", e);
  56. }
  57. };
  58. ICSPClient.download(apk_url, new Callback() {
  59. @Override
  60. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  61. listener.onDownloadFailed(e);
  62. }
  63. @Override
  64. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  65. InputStream is = null;
  66. byte[] buf = new byte[2048];
  67. int len = 0;
  68. FileOutputStream fos = null;
  69. //储存下载文件的目录
  70. File dir = new File(context.getFilesDir().getAbsolutePath() + "/" +destFileDir);
  71. if (!dir.exists()) {
  72. dir.mkdirs();
  73. }
  74. // destFileName = apk_url.substring(apk_url.lastIndexOf('/'), apk_url.length() - 1);
  75. File file = new File(dir, destFileName);
  76. try {
  77. is = response.body().byteStream();
  78. long total = response.body().contentLength();
  79. fos = new FileOutputStream(file);
  80. long sum = 0;
  81. while ((len = is.read(buf)) != -1) {
  82. fos.write(buf, 0, len);
  83. sum += len;
  84. int progress = (int) (sum * 1.0f / total * 100);
  85. //下载中更新进度条
  86. listener.onDownloading(progress);
  87. }
  88. fos.flush();
  89. //下载完成
  90. listener.onDownloadSuccess(file);
  91. } catch (Exception e) {
  92. listener.onDownloadFailed(e);
  93. }finally {
  94. try {
  95. if (is != null) {
  96. is.close();
  97. }
  98. if (fos != null) {
  99. fos.close();
  100. }
  101. } catch (IOException e) {
  102. }
  103. }
  104. }
  105. }, listener);
  106. }
  107. }