Filedownloader: Start download and show on taskmanager

Created on 13 Apr 2016  ·  10Comments  ·  Source: lingochamp/FileDownloader

Hi! I want to start a download with an URL and show all files downloaded on another activity like the taskmanager. Is that possible?

question

Most helpful comment

@YuriHeupa In addition, you also can make all methods in FileDownloadListener be invoked in other thread than main by set BaseDownloadTask#setSyncCallback(true) for the task.

/**
 * @param syncCallback if true will invoke callbacks of {@link FileDownloadListener} directly
 *                     on the download thread(do not post the message to the ui thread
 *                     by {@link android.os.Handler#post(Runnable)}
 */
public BaseDownloadTask setSyncCallback(final boolean syncCallback) {
  this.syncCallback = syncCallback;
  return this;
}

All 10 comments

Yes, FileDownloader is not bound to Activity, isn't it?

For example, you can wrap your own manager, such as:

public class DownloadManager {

    private final static class HolderClass{
        private final static DownloadManager INSTANCE = new DownloadManager();
    }

    public static DownloadManager getImpl(){
        return HolderClass.INSTANCE;
    }

    private ArrayList<DownloadStatusUpdater> updaterList = new ArrayList<>();

    public void startDownload(final String url, final String path){
        FileDownloader.getImpl().create(url)
                .setPath(path)
                .setListener(lis)
                .start();
    }

    public void addUpdater(final DownloadStatusUpdater updater) {
        if (!updaterList.contains(updater)) {
            updaterList.add(updater);
        }
    }

    public boolean removeUpdater(final DownloadStatusUpdater updater) {
        return updaterList.remove(updater);
    }


    private FileDownloadListener lis = new FileDownloadListener() {
        @Override
        protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            update(task);
        }

         @Override
        protected void started(BaseDownloadTask task) {
            super.started(task);
            update(task);
        }

        @Override
        protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
            super.connected(task, etag, isContinue, soFarBytes, totalBytes);
            update(task);
        }

        @Override
        protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            update(task);
        }

        @Override
        protected void blockComplete(BaseDownloadTask task) {
            blockComplete(task);
        }

        @Override
        protected void completed(BaseDownloadTask task) {
            update(task);
        }

        @Override
        protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
            update(task);
        }

        @Override
        protected void error(BaseDownloadTask task, Throwable e) {
            update(task);
        }

        @Override
        protected void warn(BaseDownloadTask task) {
            update(task);
        }
    };

    private void blockComplete(final BaseDownloadTask task){
        final List<DownloadStatusUpdater> updaterListCopy = (List<DownloadStatusUpdater>) updaterList.clone();
        for (DownloadStatusUpdater downloadStatusUpdater : updaterListCopy) {
            downloadStatusUpdater.blockComplete(task);
        }
    }

    private void update(final BaseDownloadTask task){
        final List<DownloadStatusUpdater> updaterListCopy = (List<DownloadStatusUpdater>) updaterList.clone();
        for (DownloadStatusUpdater downloadStatusUpdater : updaterListCopy) {
            downloadStatusUpdater.update(task);
        }
    }

    public interface DownloadStatusUpdater{
        void blockComplete(BaseDownloadTask task);
        void update(BaseDownloadTask task);
    }

}

Then you can start download in any Activity you want:

DownloadManager.getImpl().startDownload(URL, PATH);

A Activity:

final DownloadStatusUpdater aActivityUpdater = new DownloadManager.DownloadStatusUpdater{...}

DownloadManager.getImpl().addUpdater(aActivityUpdater);

//In order to prevent the memory leak, don't forget to remove updater in `onDestory` or Task finished.
DownloadManager.getImpl().removeUpdater(aActivityUpdater);

B Activity:

final DownloadStatusUpdater bActivityUpdater = new DownloadManager.DownloadStatusUpdater{...}

DownloadManager.getImpl().addUpdater(bActivityUpdater);

//In order to prevent the memory leak, don't forget to remove updater in `onDestory` or Task finished.
DownloadManager.getImpl().removeUpdater(bActivityUpdater);

wooow, my friend, your a genius!
I just dont understand what is A/B Activity, and how do I create the manager and select a layout...
Thanks a lot for your answer! :dancers:

Hi, Then how to make an activity like the demo "TasksManagerActivityDemo" ?
I want to start downloads all of them on another activity...
@Jacksgong

Recommend follow the Wiki, FileDownloader provided flexible interfaces, and you just try to use it by your thought. And if you think there have any of the interfaces is required but you couldn't find in FileDownloader, just print out and take a communicate :)

Is the blockComplete executed in other thread aside from the main? Because I'm using your download manager implementation and right after calling update from this part:

@Override
protected void blockComplete(BaseDownloadTask task) {
    update(task);
}

..the listener error method is called with the following error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

@YuriHeupa Yes, unlike other methods in FileDownloadListener, blockComplete is executed in other thread than main, when you receive FileDownloadeListener#blockComplete, it means has already completed downloading, but just block the execution of FileDownloadListener#completed in other thread. therefore, you can unzip or do some ending operation before complete.

@YuriHeupa In addition, you also can make all methods in FileDownloadListener be invoked in other thread than main by set BaseDownloadTask#setSyncCallback(true) for the task.

/**
 * @param syncCallback if true will invoke callbacks of {@link FileDownloadListener} directly
 *                     on the download thread(do not post the message to the ui thread
 *                     by {@link android.os.Handler#post(Runnable)}
 */
public BaseDownloadTask setSyncCallback(final boolean syncCallback) {
  this.syncCallback = syncCallback;
  return this;
}

@Jacksgong 没有太明白逻辑,请问DownloadStatusUpdater 这个主要是完成什么任务的?

May be identify by id or url??这个标识思路是怎样的呢?能提供个demo吗?

@chenycu 就是说,你可以做一个map来存储DownloadStatusUpdater,而非用List,key可以是id或者url之类的。

Was this page helpful?
0 / 5 - 0 ratings