1
0

APKUpdateDownload.java 4.4 KB

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