Java Code Examples for com.liulishuo.filedownloader.BaseDownloadTask

The following examples show how to use com.liulishuo.filedownloader.BaseDownloadTask. These examples are extracted from open source projects. 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 Project: v9porn   Source File: DownloadingFragment.java    License: 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
@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 Project: YCAudioPlayer   Source File: TaskFileDownloadListener.java    License: 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 Project: YCAudioPlayer   Source File: TaskFileDownloadListener.java    License: 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 5
Source Project: YCAudioPlayer   Source File: TaskFileDownloadListener.java    License: 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 6
Source Project: v9porn   Source File: DownloadingFragment.java    License: 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 Project: Aurora   Source File: DownloadSerialQueue.java    License: 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 Project: FileDownloader   Source File: HybridTestActivity.java    License: 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 9
Source Project: okdownload   Source File: FileDownloadSerialQueueTest.java    License: 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 10
Source Project: FileDownloader   Source File: FileDownloadSerialQueue.java    License: 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 11
Source Project: FileDownloader   Source File: MessageSnapshotTaker.java    License: 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 12
Source Project: FileDownloader   Source File: NotificationSampleActivity.java    License: 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 13
Source Project: v9porn   Source File: DownloadManager.java    License: 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 14
Source Project: FileDownloader   Source File: NotificationSampleActivity.java    License: 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 15
public void addNotificationItem(int downloadId) {
    if (downloadId == 0) {
        return;
    }

    BaseDownloadTask.IRunningTask task = FileDownloadList.getImpl().get(downloadId);
    if (task != null) {
        addNotificationItem(task.getOrigin());
    }
}
 
Example 16
Source Project: FileDownloader   Source File: TasksManagerDemoActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void completed(BaseDownloadTask task) {
    super.completed(task);
    final TaskItemViewHolder tag = checkCurrentHolder(task);
    if (tag == null) {
        return;
    }

    tag.updateDownloaded();
    TasksManager.getImpl().removeTaskForViewHolder(task.getId());
}
 
Example 17
Source Project: FileDownloader   Source File: TasksManagerDemoActivity.java    License: Apache License 2.0 5 votes vote down vote up
public void updateViewHolder(final int id, final TaskItemViewHolder holder) {
    final BaseDownloadTask task = taskSparseArray.get(id);
    if (task == null) {
        return;
    }

    task.setTag(holder);
}
 
Example 18
Source Project: FileDownloader   Source File: NotificationSampleActivity.java    License: 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 19
Source Project: YCAudioPlayer   Source File: CacheDownloadingAdapter.java    License: 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 20
@Override
protected void completed(BaseDownloadTask task) {
    context.sendBroadcast(
            new Intent(AbstractDownloaderExecutor.ACTION_DOWNLOAD_COMPLETE)
                    .putExtra(AbstractDownloaderExecutor.EXTRA_DOWNLOAD_ID, this.task.taskId)
                    .setComponent(
                            new ComponentName(
                                    context,
                                    AndroidDownloaderExecutor.DownloadReceiver.class
                            )
                    )
    );
}
 
Example 21
public void showIndeterminate(BaseDownloadTask task) {
    if (disableNotification(task)) {
        return;
    }

    this.helper.showIndeterminate(task.getId(), task.getStatus());
}
 
Example 22
Source Project: FileDownloader   Source File: TasksManagerDemoActivity.java    License: 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 23
Source Project: FileDownloader   Source File: TasksManagerDemoActivity.java    License: 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 24
@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 25
Source Project: okdownload   Source File: FileDownloadSerialQueue.java    License: 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 26
Source Project: okdownload   Source File: FileDownloadSerialQueue.java    License: 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 27
Source Project: FileDownloader   Source File: NotificationSampleActivity.java    License: 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 28
public void addNotificationItem(BaseDownloadTask task) {
    if (disableNotification(task)) {
        return;
    }

    final BaseNotificationItem n = create(task);
    if (n != null) {
        //noinspection unchecked
        this.helper.add(n);
    }
}
 
Example 29
/**
 * 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
public void showProgress(BaseDownloadTask task, int soFarBytes,
                         int totalBytes) {
    if (disableNotification(task)) {
        return;
    }

    this.helper.showProgress(task.getId(), task.getSmallFileSoFarBytes(),
            task.getSmallFileTotalBytes());
}