/** Pause all task */ public void pauseAll() { FileDownloadEventPool.getImpl().shutdownSendPool(); final BaseDownloadTask[] downloadList = FileDownloadList.getImpl().copy(); synchronized (pauseLock) { for (BaseDownloadTask baseDownloadTask : downloadList) { baseDownloadTask.pause(); } } // double check, for case: File Download progress alive but ui progress has died and relived, // so FileDownloadList not always contain all running task exactly. if (FileDownloadServiceUIGuard.getImpl().isConnected()) { FileDownloadServiceUIGuard.getImpl().pauseAllTasks(); } else { if (pauseAllRunnable == null) { pauseAllRunnable = new Runnable() { @Override public void run() { FileDownloadServiceUIGuard.getImpl().pauseAllTasks(); } }; } FileDownloadServiceUIGuard.getImpl() .bindStartByContext(FileDownloadHelper.getAppContext(), pauseAllRunnable); } }
/** * 开始下载任务 * * @param downloadId * @param callback */ public void startTask(int downloadId, FileDownloaderCallback callback) { FileDownloaderModel model = getFileDownloaderModelById(downloadId); if (model != null) { BridgeListener bridgeListener = mListenerManager.getBridgeListener(downloadId); bridgeListener.addDownloadListener(callback); if (mDownloadingList.size() >= mConfiguration.getMaxDownloadingCount()) { // 下载中队列已满 if (!mWaitQueue.contains(model)) { mWaitQueue.offer(model); } bridgeListener.wait(downloadId); } else { mDownloadingList.add(model); final BaseDownloadTask task = FileDownloader.getImpl() .create(model.getUrl()) .setPath(model.getPath()) .setCallbackProgressTimes(100) .setAutoRetryTimes(mAutoRetryTimes) .setListener(bridgeListener); for (int i = 0; i < mHeaders.size(); i++) { task.addHeader(mHeaders.name(i), mHeaders.value(i)); } bridgeListener.setDownloadTask(task); task.start(); } } else { ILogger.e("Task does not exist!"); } }
/** * Start the download queue by the same listener * * @param listener start download by same listener * @param isSerial is execute them linearly */ public void start(final FileDownloadListener listener, final boolean isSerial) { if (listener == null) { return; } final List<BaseDownloadTask> list = FileDownloadList.getImpl().copy(listener); if (FileDownloadMonitor.isValid()) { FileDownloadMonitor.getMonitor().onRequestStart(list.size(), isSerial, listener); } if (FileDownloadLog.NEED_LOG) { FileDownloadLog.v( this, "start list size[%d] listener[%s] isSerial[%B]", list.size(), listener, isSerial); } if (isSerial) { // serial final Handler serialHandler = createSerialHandler(list); Message msg = serialHandler.obtainMessage(); msg.what = WHAT_SERIAL_NEXT; msg.arg1 = 0; serialHandler.sendMessage(msg); } else { // parallel for (final BaseDownloadTask downloadTask : list) { downloadTask.start(); } } }
/** * @param downloadId Download Id * @return download status, if service has not connected, will be {@link * com.liulishuo.filedownloader.model.FileDownloadStatus#INVALID_STATUS} if already has * over(error,paused,completed,warn), will come from File Download Service if there are no * data in File Download Service , will be {@link * com.liulishuo.filedownloader.model.FileDownloadStatus#INVALID_STATUS} */ public int getStatus(final int downloadId) { BaseDownloadTask downloadTask = FileDownloadList.getImpl().get(downloadId); if (downloadTask == null) { return FileDownloadServiceUIGuard.getImpl().getStatus(downloadId); } return downloadTask.getStatus(); }
/** Get file total bytes by the downloadId */ public long getTotal(final int downloadId) { BaseDownloadTask downloadTask = FileDownloadList.getImpl().get(downloadId); if (downloadTask == null) { return FileDownloadServiceUIGuard.getImpl().getTotal(downloadId); } return downloadTask.getLargeFileTotalBytes(); }
/** * Pause the download task by the downloadId * * @param downloadId pause download by download id * @see #pause(FileDownloadListener) */ public void pause(final int downloadId) { BaseDownloadTask downloadTask = FileDownloadList.getImpl().get(downloadId); if (downloadTask == null) { FileDownloadLog.w(this, "request pause but not exist %d", downloadId); return; } downloadTask.pause(); }
/** * Pause the download queue by the same listener * * @param listener paused download by same listener * @see #pause(int) */ public void pause(final FileDownloadListener listener) { FileDownloadEventPool.getImpl().shutdownSendPool(listener); final List<BaseDownloadTask> downloadList = FileDownloadList.getImpl().copy(listener); synchronized (pauseLock) { for (BaseDownloadTask baseDownloadTask : downloadList) { baseDownloadTask.pause(); } } }