Skip to content
huxq17 edited this page Jul 30, 2020 · 28 revisions

Configuration

Configuration is not necessary,can download normally without configuration.

  • setMaxRunningTaskNum(int): Set the maximum number of tasks to run,default 3, optionally.
  • setMinUsableStorageSpace(long): Set the minimum available storage space size for downloading to avoid insufficient storage space during downloading, default is 4kb, optionally.
  • setDownloadConnectionFactory(DownloadConnection.Factory): Set download connection factory,you can use yourself okhttpClient by use OkHttpDownloadConnection.Factory:
setDownloadConnectionFactory(new OkHttpDownloadConnection.Factory("yourself okhttpclient"))

If you want custom http connection,you can implement DownloadConnection.Factory.

  • addDownloadInterceptor(DownloadInterceptor): Add download interceptor that can use for unzip download file and verify download file MD5,and so on.

  • Usage

  Pump.newConfigBuilder()
        //Optionally.
        .setMaxRunningTaskNum(2)
        //Optionally.
        .setMinUsableStorageSpace(4 * 1024L)
        //Optionally.
        .setDownloadConnectionFactory(new OkHttpDownloadConnection.Factory(OKHttpUtil.get()))
        //Optionally.
        .addDownloadInterceptor(new DownloadInterceptor() {
                    @Override
                    public DownloadInfo intercept(DownloadChain chain) {
                        DownloadRequest downloadRequest = chain.request();
                        DownloadInfo downloadInfo = chain.proceed(downloadRequest);
                        if (downloadInfo.isFinished()) {
                            //verify md5.
                            File downloadFile = new File(downloadInfo.getFilePath());
                            String fileMD5 = Utils.getMD5ByBase64(downloadFile);
                            String serverMD5 = downloadInfo.getMD5();
                            LogUtil.e("fileMD5=" + fileMD5 + ";serverMD5=" + serverMD5);
                            if (!TextUtils.isEmpty(serverMD5) && !serverMD5.equals(fileMD5)) {
                                //setErrorCode will make download failed.
                                downloadInfo.setErrorCode(MD5_VERIFY_FAILED);
                                FileUtil.deleteFile(downloadFile);
                            }

                            //unzip file here if need.
                        }
                        return downloadInfo;
                    }
                })
        .build();

Submit Download Task

  • newRequest(url,storagePath): New request need download url,storage path is not necessary,if don't assign storage path, file will be download into cache directory.

  • listener(DownloadListener): Set download listener,optionally,listen download progress,DownloadListener can aware of page's lifecycle,if pass activity or fragment as a parameter in the constructor,DownloadListener will automatically removed when the page is destroyed.

  • setId(id): Set id for download task, id is the unique identifier for download task,default id is download url.

  • setRequestBuilder(okhttp's Request Builder): Set a OKHttp request builder, can customize download’s network request.

  • tag(tag): Tag download task, can use Pump#getDownloadListByTag(String) to get download list filter by tag,and use DownloadInfo#getTag() to get tag.

  • forceReDownload(force): Set whether to repeatedly download the downloaded file,default false.

  • setRetry(retryCount, delayMillis): Set retry count and retry interval

  • setDownloadTaskExecutor(DownloadTaskExecutor): Set download task executor, optionally,see more.

  • submit(): submit download task.

  • Usage

Pump.newRequest(url)
        //Set id,optionally
        .setId(id)
        //Set a OKHttp request builder,specify http method, head and params,optionally
        .setRequestBuilder(new Request.Builder())
        //Set tag,optionally
        .tag(tag)
        //Set DownloadTaskExecutor,optionally
        .setDownloadTaskExecutor(DemoApplication.getInstance().musicDownloadDispatcher)
        //Set listener,optionally
        .listener(new DownloadListener() {
            @Override
             public void onSuccess() {}

            @Override
            public void onFailed() {}

            @Override
            public void onProgress(int progress) {
              DownloadInfo downloadInfo = getDownloadInfo();
              //Get download file
              File downloadFile = new File(downloadInfo.getFilePath());
              //Get download url
              String url = downloadInfo.getUrl();
              //Get download speed
              String speed = downloadInfo.getSpeed();
            }
    })
    .submit();

Delete Download

Delete download will delete local cache files and database records,so it should to be performed on a different thread than UI thread. If you use RxJava, you can avoid ANR by using RxPump,or remove RxJava dependences from build.gradle and use Pump instead. In a word,it is recommended to delete download in an non-UI thread.

  • Pump
   //Delete downloads by Tag,to delete a group of task
   Pump.deleteByTag("tag");
   //Delete a download by special download id.
   Pump.deleteById("id");
  • RxPump
 //Delete downloads by Tag,to delete a group of task
 RxPump.deleteByTag("tag")
    .subscribeOn(Schedulers.io())
    .subscribe();
  //Delete a download by special download id.
 RxPump.deleteById("id")
    .subscribeOn(Schedulers.io())
    .subscribe();

Get Download List

Get download list will query local database.

  • Pump
//Get all download list
List<DownloadInfo> downloadInfoList = Pump.getAllDownloadList();
//Get completed download list
List<DownloadInfo> downloadInfoList = Pump.getDownloadedList();
//Get downloading list
List<DownloadInfo> downloadInfoList = Pump.getDownloadingList();
//Get download list by tag
List<DownloadInfo> downloadInfoList = Pump.getDownloadListByTag("tag");
//Get download list by id
DownloadInfo downloadInfoList = Pump.getDownloadInfoById("id");
  • RxPump
  RxPump.getAllDownloadList()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
         .subscribe(new Consumer<List<DownloadInfo>>() {
            @Override
            public void accept(List<DownloadInfo> downloadInfoList) {
                // The code that use download list.
            }
        });
    //Several methods for Pump have the same name as the RxPump method,I'm not post them all。

Download Success Or Not

  • Pump
    //Return true if download successful
    boolean succeed = Pump.hasDownloadSucceed(id);
    //Return download file if download successful,or return null
    File downloadFile= Pump.getFileIfSucceed(id);
  • RxPump
    RxPump.hasDownloadSucceed(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean isSucceed) {
                    //isSucceed is true if download successful
                }
            });

    RxPump.getFileIfSucceed(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<File>() {
                @Override
                public void accept(File file) throws Exception {
                    //file isn't null if download successful.
                }
            });

DownloadInfo

Method Annotation
setTag(Object obj) Set tag
getTag() Get tag
getUrl() Get download url
getFilePath() Get download file path
getName() Get download file's name
getProgress() Get downlaod progress
getContentLength() Get download file's length
getStatus() Get download status.For more,see DownloadInfo.Status
getSpeed() Get download Speed
getErrorCode() Get download error infomation,For more,see ErrorCode
Clone this wiki locally