DownloadInstall.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.hc.webapp.web;
  2. import android.app.DownloadManager;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.Environment;
  8. import android.support.v4.content.FileProvider;
  9. import android.util.Log;
  10. import android.webkit.URLUtil;
  11. import com.hc.webapp.BuildConfig;
  12. import com.liulishuo.filedownloader.BaseDownloadTask;
  13. import com.liulishuo.filedownloader.FileDownloadListener;
  14. import com.liulishuo.filedownloader.FileDownloader;
  15. import com.liulishuo.filedownloader.util.FileDownloadUtils;
  16. import java.io.File;
  17. public class DownloadInstall {
  18. private String TAG = "DownloadInstall";
  19. private Context context;
  20. public DownloadInstall(Context context){
  21. this.context = context;
  22. }
  23. public void start(String url,String md5){
  24. downloadApk(url,md5);
  25. }
  26. private void downloadApk(final String url,final String md5){
  27. String filePath = FileDownloadUtils.getDefaultSaveRootPath();
  28. FileDownloader.getImpl().create(url)
  29. .setPath(filePath,true)
  30. .setCallbackProgressTimes(300)
  31. .setMinIntervalUpdateSpeed(400)
  32. .setListener(new FileDownloadListener() {
  33. @Override
  34. protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  35. }
  36. @Override
  37. protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  38. }
  39. @Override
  40. protected void completed(BaseDownloadTask task) {
  41. File file = new File( FileDownloadUtils.getDefaultSaveFilePath(url));
  42. String apkMD5 = MD5.getFileMD5(file).toUpperCase();
  43. String updateD5 = md5.toUpperCase();
  44. if(!apkMD5.equals(updateD5)){
  45. file.delete();
  46. downloadApk(url,md5);
  47. }else{
  48. apkInstall(file);
  49. }
  50. }
  51. @Override
  52. protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
  53. }
  54. @Override
  55. protected void error(BaseDownloadTask task, Throwable e) {
  56. e.printStackTrace();
  57. }
  58. @Override
  59. protected void warn(BaseDownloadTask task) {
  60. }
  61. })
  62. .start();
  63. }
  64. private void apkInstall(File apkFile){
  65. Intent intent = new Intent(Intent.ACTION_VIEW);
  66. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
  67. intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  68. Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
  69. intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
  70. }else{
  71. intent.setDataAndType(Uri.fromFile(apkFile),
  72. "application/vnd.android.package-archive");
  73. }
  74. context.startActivity(intent);
  75. }
  76. }