com.liulishuo.filedownloader.BaseDownloadTask Java Examples

The following examples show how to use com.liulishuo.filedownloader.BaseDownloadTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DownloadingFragment.java    From v9porn with MIT License 6 votes vote down vote up
@Override
public void complete(BaseDownloadTask task) {
    if (mV9PornItemList == null || mV9PornItemList.size() == 0) {
        return;
    }
    Logger.t(TAG).d("已经下载完成了");
    V9PornItem v9PornItem = presenter.findUnLimit91PornItemByDownloadId(task.getId());
    if (v9PornItem != null) {
        int position = mV9PornItemList.indexOf(v9PornItem);
        if (position >= 0 && position < mV9PornItemList.size()) {
            mV9PornItemList.remove(position);
            mDownloadAdapter.notifyItemRemoved(position);
        } else {
            presenter.loadDownloadingData();
        }
    } else {
        presenter.loadDownloadingData();
    }
}
 
Example #2
Source File: FileDownloadTask.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
@Override protected void completed(BaseDownloadTask baseDownloadTask) {
  new Thread(() -> {
    FileDownloadTaskStatus fileDownloadTaskStatus1 =
        new FileDownloadTaskStatus(AppDownloadStatus.AppDownloadState.VERIFYING_FILE_INTEGRITY,
            new FileDownloadProgressResult(baseDownloadTask.getLargeFileTotalBytes(),
                baseDownloadTask.getLargeFileTotalBytes()), md5);
    downloadStatus.onNext(fileDownloadTaskStatus1);

    FileDownloadTaskStatus fileDownloadTaskStatus;
    if (md5Comparator.compareMd5(md5, fileName)) {
      fileDownloadTaskStatus =
          new FileDownloadTaskStatus(AppDownloadStatus.AppDownloadState.COMPLETED,
              new FileDownloadProgressResult(baseDownloadTask.getLargeFileTotalBytes(),
                  baseDownloadTask.getLargeFileTotalBytes()), md5);
      Logger.getInstance()
          .d(TAG, " Download completed");
    } else {
      Logger.getInstance()
          .d(TAG, " Download error in md5");
      fileDownloadTaskStatus =
          new FileDownloadTaskStatus(AppDownloadStatus.AppDownloadState.ERROR_MD5_DOES_NOT_MATCH,
              md5, new Md5DownloadComparisonException("md5 does not match"));
    }
    downloadStatus.onNext(fileDownloadTaskStatus);
  }).start();
}
 
Example #3
Source File: TaskFileDownloadListener.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 状态: 连接中
 */
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
    super.connected(task, etag, isContinue, soFarBytes, totalBytes);
    final TaskViewHolderImp tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }
    AppLogUtils.e("taskDownloadListener----"+"connected--------");
    if(tag instanceof DialogListAdapter.ViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"connected--------DialogListAdapter");
        ((DialogListAdapter.ViewHolder)tag).updateDownloading(FileDownloadStatus.connected, soFarBytes, totalBytes);
    }
    if(tag instanceof CacheDownloadingAdapter.MyViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"connected--------CacheDownloadingAdapter");
        ((CacheDownloadingAdapter.MyViewHolder)tag).updateDownloading(FileDownloadStatus.connected, soFarBytes, totalBytes);

    }
}
 
Example #4
Source File: HybridTestActivity.java    From FileDownloader with Apache License 2.0 6 votes vote down vote up
/**
 * Start multiple download tasks parallel
 * <p>
 * 启动并行多任务下载
 *
 * @param view
 */
public void onClickMultiParallel(final View view) {
    updateDisplay(getString(R.string.hybrid_test_start_multiple_tasks_parallel, Constant.URLS.length));

    // 以相同的listener作为target,将不同的下载任务绑定起来
    final FileDownloadListener parallelTarget = createListener();
    final List<BaseDownloadTask> taskList = new ArrayList<>();
    int i = 0;
    for (String url : Constant.URLS) {
        taskList.add(FileDownloader.getImpl().create(url)
                .setTag(++i));
    }
    totalCounts += taskList.size();

    new FileDownloadQueueSet(parallelTarget)
            .setCallbackProgressTimes(1)
            .downloadTogether(taskList)
            .start();
}
 
Example #5
Source File: FileDownloadSerialQueue.java    From FileDownloader with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to stop the working task, halts the processing of waiting tasks, and returns a list
 * of the tasks that were awaiting execution. These tasks are drained (removed) from the task
 * queue upon return from this method.
 */
public List<BaseDownloadTask> shutdown() {
    synchronized (finishCallback) {
        if (workingTask != null) {
            pause();
        }

        final List<BaseDownloadTask> unDealTaskList = new ArrayList<>(pausedList);
        pausedList.clear();
        mHandler.removeMessages(WHAT_NEXT);
        mHandlerThread.interrupt();
        mHandlerThread.quit();

        return unDealTaskList;
    }
}
 
Example #6
Source File: DownloadingFragment.java    From v9porn with MIT License 6 votes vote down vote up
@Override
public void complete(BaseDownloadTask task) {
    if (mV9PornItemList == null || mV9PornItemList.size() == 0) {
        return;
    }
    Logger.t(TAG).d("已经下载完成了");
    V9PornItem v9PornItem = presenter.findUnLimit91PornItemByDownloadId(task.getId());
    if (v9PornItem != null) {
        int position = mV9PornItemList.indexOf(v9PornItem);
        if (position >= 0 && position < mV9PornItemList.size()) {
            mV9PornItemList.remove(position);
            mDownloadAdapter.notifyItemRemoved(position);
        } else {
            presenter.loadDownloadingData();
        }
    } else {
        presenter.loadDownloadingData();
    }
}
 
Example #7
Source File: DownloadSerialQueue.java    From Aurora with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void over(BaseDownloadTask task) {
    task.removeFinishListener(this);

    if (mQueueWeakReference == null) {
        return;
    }

    final DownloadSerialQueue queue = mQueueWeakReference.get();
    if (queue == null) {
        return;
    }

    queue.workingTask = null;
    if (queue.paused) {
        return;
    }
    queue.sendNext();
}
 
Example #8
Source File: FileDownloadSerialQueueTest.java    From okdownload with Apache License 2.0 6 votes vote down vote up
@Test
public void shutDown() {
    final DownloadTask downloadTask1 = mock(DownloadTask.class);
    final DownloadTask downloadTask2 = mock(DownloadTask.class);
    final DownloadTask downloadTask3 = mock(DownloadTask.class);
    final DownloadTaskAdapter downloadTaskAdapter1 = mock(DownloadTaskAdapter.class);
    final DownloadTaskAdapter downloadTaskAdapter2 = mock(DownloadTaskAdapter.class);
    final DownloadTaskAdapter downloadTaskAdapter3 = mock(DownloadTaskAdapter.class);
    when(downloadTask1.getTag(DownloadTaskAdapter.KEY_TASK_ADAPTER))
            .thenReturn(downloadTaskAdapter1);
    when(downloadTask2.getTag(DownloadTaskAdapter.KEY_TASK_ADAPTER))
            .thenReturn(downloadTaskAdapter2);
    when(downloadTask3.getTag(DownloadTaskAdapter.KEY_TASK_ADAPTER))
            .thenReturn(downloadTaskAdapter3);
    final DownloadTask[] downloadTasks =
            new DownloadTask[]{downloadTask1, downloadTask2, downloadTask3};
    when(serialQueue.shutdown()).thenReturn(downloadTasks);

    final List<BaseDownloadTask> result = fileDownloadSerialQueue.shutdown();

    assertThat(result).hasSize(downloadTasks.length);
    verify(fileDownloadList).remove(downloadTaskAdapter1);
    verify(fileDownloadList).remove(downloadTaskAdapter2);
    verify(fileDownloadList).remove(downloadTaskAdapter3);
}
 
Example #9
Source File: TaskFileDownloadListener.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 状态: 错误
 */
@Override
protected void error(BaseDownloadTask task, Throwable e) {
    super.error(task, e);
    final TaskViewHolderImp tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }
    AppLogUtils.e("taskDownloadListener----"+"error--------");
    if(tag instanceof DialogListAdapter.ViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"error--------DialogListAdapter");
        ((DialogListAdapter.ViewHolder)tag).updateNotDownloaded(FileDownloadStatus.error, task.getLargeFileSoFarBytes()
                , task.getLargeFileTotalBytes());
    }
    if(tag instanceof CacheDownloadingAdapter.MyViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"error--------CacheDownloadingAdapter");
        ((CacheDownloadingAdapter.MyViewHolder)tag).updateNotDownloaded(FileDownloadStatus.error, task.getLargeFileSoFarBytes()
                , task.getLargeFileTotalBytes());
    }
    TasksManager.getImpl().removeTaskForViewHolder(task.getId());
}
 
Example #10
Source File: TaskFileDownloadListener.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 状态: 完成
 */
@Override
protected void completed(BaseDownloadTask task) {
    super.completed(task);
    final TaskViewHolderImp tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }
    AppLogUtils.e("taskDownloadListener----"+"completed--------");
    if(tag instanceof DialogListAdapter.ViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"completed--------DialogListAdapter");
        ((DialogListAdapter.ViewHolder)tag).updateDownloaded();
    }
    if(tag instanceof CacheDownloadingAdapter.MyViewHolder){
        AppLogUtils.e("taskDownloadListener----"+"completed--------CacheDownloadingAdapter");
        ((CacheDownloadingAdapter.MyViewHolder)tag).updateDownloaded();
    }
    TasksManager.getImpl().removeTaskForViewHolder(task.getId());
}
 
Example #11
Source File: CacheDownloadingAdapter.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(View v) {
    if (v.getTag() == null) {
        return;
    }
    AppLogUtils.e("listener----"+"--------");
    int imgResId = (int) holder.ivDownload.getTag();
    TasksManagerModel model = list.get(holder.position);
    String path = model.getPath();
    String url = model.getUrl();
    AppLogUtils.e("listener----"+url+ "--------");
    switch (imgResId) {
        case R.drawable.ic_note_btn_play_white:
            AppLogUtils.e("listener----"+"------开始下载--");
            // 开始下载
            final BaseDownloadTask task = FileDownloader.getImpl().create(url)
                    .setPath(path)
                    .setCallbackProgressTimes(500)
                    .setListener(new TaskFileDownloadListener());
            TasksManager.getImpl().addTaskForViewHolder(task);
            TasksManager.getImpl().updateViewHolder(holder.id, holder);
            task.start();
            break;
        case R.drawable.ic_note_btn_pause_white:
            AppLogUtils.e("listener----"+"------暂停下载--");
            // 暂停下载
            FileDownloader.getImpl().pause(holder.id);
            holder.ivDownload.setBackgroundResource(R.drawable.ic_note_btn_play_white);
            holder.ivDownload.setTag(R.drawable.ic_note_btn_play_white);
            break;
        default:
            break;
    }
}
 
Example #12
Source File: NotificationSampleActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
protected BaseNotificationItem create(BaseDownloadTask task) {
    return new NotificationItem(
            task.getId(),
            "Task-" + task.getId(),
            "",
            channelId
    );
}
 
Example #13
Source File: NotificationSampleActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean disableNotification(BaseDownloadTask task) {
    if (wActivity.get() != null) {
        return !wActivity.get().showNotificationCb.isChecked();
    }

    return super.disableNotification(task);
}
 
Example #14
Source File: CacheDownLoadService.java    From Aurora with Apache License 2.0 5 votes vote down vote up
@Override
protected void completed(BaseDownloadTask task) {
    getNotificationManager().cancel(1);
    VideoDownLoadInfo video = videos.get((String) task.getTag());
    video.setFinish(true);
    video.setContentLength(task.getLargeFileTotalBytes());
    DaoMaster master = GreenDaoHelper.getInstance().create(video.getDbName()).getMaster();
    master.newSession().update(video);
    EventBus.getDefault().post("", EventBusTags.CACHE_DOWNLOAD_FINISH);
    shouldStop();
}
 
Example #15
Source File: CacheDownLoadService.java    From Aurora with Apache License 2.0 5 votes vote down vote up
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
    getNotificationManager().notify(1, getNotification("正在下载视频"+task.getTag(), totalBytes > 0 && soFarBytes > 0?(int)(100L * soFarBytes / totalBytes):0,false));
    DownloadProgressInfo info = new DownloadProgressInfo((String)task.getTag());
    info.setCurrentBytes(soFarBytes);
    info.setContentLength(totalBytes);
    EventBus.getDefault().post(info, EventBusTags.CACHE_DOWNLOAD_PROGRESS);
}
 
Example #16
Source File: UpdateFragment.java    From YCUpdateApp with Apache License 2.0 5 votes vote down vote up
private BaseDownloadTask downApk(String apkUrl, String saveApkPath, FileDownloadListener listener) {
    BaseDownloadTask baseDownloadTask = FileDownloader
            .getImpl()
            .create(apkUrl)
            .setPath(saveApkPath)
            .setListener(listener);
    baseDownloadTask.start();
    return baseDownloadTask;
}
 
Example #17
Source File: NotificationSampleActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
public void onClickStart(final View view) {
    startDownload.setEnabled(false);
    final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(listener);
    final List<BaseDownloadTask> tasks = new ArrayList<>();
    final int taskCount = 5;
    for (int i = 0; i < taskCount; i++) {
        tasks.add(FileDownloader.getImpl()
                .create(Constant.URLS[i])
                .setTag(i + 1)
                .setPath(savePath, true)
        );
    }
    queueSet.downloadTogether(tasks)
            .addTaskFinishListener(new BaseDownloadTask.FinishListener() {
                final AtomicInteger counter = new AtomicInteger();

                @Override
                public void over(BaseDownloadTask task) {
                    final int finishCount = counter.addAndGet(1);
                    if (finishCount == taskCount) startDownload.post(new Runnable() {
                        @Override
                        public void run() {
                            startDownload.setEnabled(true);
                            progressBar.setIndeterminate(false);
                            progressBar.setProgress(1);
                            progressBar.setMax(1);
                        }
                    });
                }
            })
            .start();
    progressBar.setIndeterminate(true);
}
 
Example #18
Source File: DownloadManager.java    From v9porn with MIT License 5 votes vote down vote up
/**
 * 实时保存下载信息
 *
 * @param task 任务信息
 */
private void saveDownloadInfo(BaseDownloadTask task) {
    V9PornItem v9PornItem = dataManager.findV9PornItemByDownloadId(task.getId());
    if (v9PornItem == null) {
        //不存在的任务清除掉
        FileDownloader.getImpl().clear(task.getId(), task.getPath());
        if (!BuildConfig.DEBUG) {
          //  Bugsnag.notify(new Throwable(TAG + "::save download info failure:" + task.getUrl()), Severity.WARNING);
        }
        return;
    }
    int soFarBytes = task.getSmallFileSoFarBytes();
    int totalBytes = task.getSmallFileTotalBytes();
    if (soFarBytes > 0) {
        v9PornItem.setSoFarBytes(soFarBytes);
    }

    if (totalBytes > 0) {
        v9PornItem.setTotalFarBytes(totalBytes);
    }
    if (totalBytes > 0) {
        int p = (int) (((float) soFarBytes / totalBytes) * 100);
        v9PornItem.setProgress(p);
    }
    if (task.getStatus() == FileDownloadStatus.completed) {
        v9PornItem.setFinishedDownloadDate(new Date());
    }
    v9PornItem.setSpeed(task.getSpeed());
    v9PornItem.setStatus(task.getStatus());
    dataManager.updateV9PornItem(v9PornItem);
    if (task.getStatus() == FileDownloadStatus.completed) {
        complete(task);
    } else {
        update(task);
    }
}
 
Example #19
Source File: NotificationSampleActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyNotification(BaseDownloadTask task) {
    super.destroyNotification(task);
    if (wActivity.get() != null) {
        wActivity.get().showNotificationCb.setEnabled(true);
    }
}
 
Example #20
Source File: NotificationSampleActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean interceptCancel(BaseDownloadTask task,
                                  BaseNotificationItem n) {
    // in this demo, I don't want to cancel the notification, just show for the test
    // so return true
    return true;
}
 
Example #21
Source File: DownloadHelper.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
    Notification notification = NotificationHelper.getInstance(context)
            .sendDownloadProgressNotification(title, soFarBytes - soFar, totalBytes - total, true, false);
    DownloadHelper.getInstance().setForeground(notification);
    soFar = soFarBytes;
    total = totalBytes;
}
 
Example #22
Source File: TasksManagerDemoActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
protected void error(BaseDownloadTask task, Throwable e) {
    super.error(task, e);
    final TaskItemViewHolder tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }

    tag.updateNotDownloaded(FileDownloadStatus.error, task.getLargeFileSoFarBytes()
            , task.getLargeFileTotalBytes());
    TasksManager.getImpl().removeTaskForViewHolder(task.getId());
}
 
Example #23
Source File: FileDownloadSerialQueue.java    From okdownload with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueues the given task sometime in the serial queue. If the {@code task} is in the head of
 * the serial queue, the {@code task} will be started automatically.
 */
public void enqueue(BaseDownloadTask task) {
    final DownloadTaskAdapter downloadTaskAdapter = (DownloadTaskAdapter) task;
    downloadTaskAdapter.insureAssembleDownloadTask();
    FileDownloadList.getImpl().addIndependentTask(downloadTaskAdapter);
    serialQueue.enqueue(downloadTaskAdapter.getDownloadTask());
    listenerManager.addAutoRemoveListenersWhenTaskEnd(downloadTaskAdapter.getId());
    listenerManager.attachListener(downloadTaskAdapter.getDownloadTask(),
            downloadTaskAdapter.getCompatListener());
}
 
Example #24
Source File: FileDownloadNotificationListener.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
public void showIndeterminate(BaseDownloadTask task) {
    if (disableNotification(task)) {
        return;
    }

    this.helper.showIndeterminate(task.getId(), task.getStatus());
}
 
Example #25
Source File: MessageSnapshotTaker.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
public static MessageSnapshot catchPause(BaseDownloadTask task) {
    if (task.isLargeFile()) {
        return new LargeMessageSnapshot.PausedSnapshot(task.getId(),
                task.getLargeFileSoFarBytes(), task.getLargeFileTotalBytes());
    } else {
        return new SmallMessageSnapshot.PausedSnapshot(task.getId(),
                task.getSmallFileSoFarBytes(), task.getSmallFileTotalBytes());
    }
}
 
Example #26
Source File: FileDownloadNotificationListener.java    From okdownload with Apache License 2.0 5 votes vote down vote up
public void showProgress(BaseDownloadTask task, int soFarBytes,
                         int totalBytes) {
    if (disableNotification(task)) {
        return;
    }

    this.helper.showProgress(task.getId(), task.getSmallFileSoFarBytes(),
            task.getSmallFileTotalBytes());
}
 
Example #27
Source File: TasksManagerDemoActivity.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
protected void started(BaseDownloadTask task) {
    super.started(task);
    final TaskItemViewHolder tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }

    tag.taskStatusTv.setText(R.string.tasks_manager_demo_status_started);
}
 
Example #28
Source File: FileDownloadSerialQueue.java    From okdownload with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to stop the working task, halts the processing of waiting tasks, and returns a list
 * of the tasks that were awaiting execution. These tasks are drained (removed) from the task
 * queue upon return from this method.
 */
public List<BaseDownloadTask> shutdown() {
    DownloadTask[] tasks = serialQueue.shutdown();
    List<BaseDownloadTask> notRunningTasks = new ArrayList<>();
    for (DownloadTask task : tasks) {
        final DownloadTaskAdapter notRunningTask =
                FileDownloadUtils.findDownloadTaskAdapter(task);
        if (notRunningTask != null) {
            notRunningTasks.add(notRunningTask);
            FileDownloadList.getImpl().remove(notRunningTask);
        }
    }
    return notRunningTasks;
}
 
Example #29
Source File: FileDownloadNotificationListener.java    From okdownload with Apache License 2.0 5 votes vote down vote up
/**
 * The notification item with the {@code task} is told to destroy.
 *
 * @param task The task used to identify the will be destroyed notification item.
 */
public void destroyNotification(BaseDownloadTask task) {
    if (disableNotification(task)) {
        return;
    }

    this.helper.showIndeterminate(task.getId(), task.getStatus());

    final BaseNotificationItem n = this.helper.
            remove(task.getId());
    if (!interceptCancel(task, n) && n != null) {
        n.cancel();
    }
}
 
Example #30
Source File: FileDownloadNotificationListener.java    From okdownload with Apache License 2.0 5 votes vote down vote up
public void addNotificationItem(BaseDownloadTask task) {
    if (disableNotification(task)) {
        return;
    }

    final BaseNotificationItem n = create(task);
    if (n != null) {
        //noinspection unchecked
        this.helper.add(n);
    }
}