/**
  * 开始下载任务
  *
  * @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!");
   }
 }
예제 #2
0
  /**
   * 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();
      }
    }
  }