/**
  * 开始下载任务
  *
  * @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!");
   }
 }
  /**
   * 根据URL获取下载信息
   *
   * @param url
   * @return
   */
  public FileDownloaderModel getFileDownloaderModelByUrl(String url) {
    for (int i = 0; i < mAllTasks.size(); i++) {
      FileDownloaderModel model = getFileDownloaderModelByPostion(i);
      if (model != null && TextUtils.equals(model.getUrl(), url)) {
        return model;
      }
    }

    return null;
  }
  /**
   * 是否下载完成
   *
   * @param downloadId
   * @return
   */
  public boolean isFinish(int downloadId) {
    FileDownloaderModel model = getFileDownloaderModelById(downloadId);
    if (model != null) {
      if (getStatus(downloadId) == FileDownloadStatus.completed
          && new File(model.getPath()).exists()) {
        return true;
      }
    }

    return false;
  }
 /**
  * 将一个等待中的任务从下等待队列中移除
  *
  * @param downloadId
  */
 protected synchronized void removeWaitQueueTask(int downloadId) {
   Iterator<FileDownloaderModel> iterator = mWaitQueue.iterator();
   while (iterator.hasNext()) {
     FileDownloaderModel model = iterator.next();
     if (model != null && model.getId() == downloadId) {
       try {
         iterator.remove();
       } catch (Exception e) {
         ILogger.e(e);
       }
       return;
     }
   }
 }
  /**
   * 根据downloadId获取文件下载进度
   *
   * @param downloadId
   * @return
   */
  public int getProgress(int downloadId) {
    FileDownloaderModel model = getFileDownloaderModelById(downloadId);
    int progress = 0;
    if (model != null) {
      if (!new File(model.getPath()).exists()) {
        return progress;
      }
    }

    long totalBytes = getTotal(downloadId);
    long soFarBytes = getSoFar(downloadId);

    if (totalBytes != 0) {
      progress = (int) (soFarBytes / (float) totalBytes * 100);
    }

    return progress;
  }
 /**
  * 删除一个任务
  *
  * @param downloadId
  */
 public void deleteTask(int downloadId) {
   if (mDbController.deleteTask(downloadId)) {
     FileDownloaderModel model = getFileDownloaderModelById(downloadId);
     if (model != null) { // 删除文件
       new File(model.getPath()).delete();
     }
     pauseTask(downloadId);
     removeDownloadingTask(downloadId);
     removeWaitQueueTask(downloadId);
     try {
       mAllTasks.remove(downloadId);
     } catch (Exception e) {
       ILogger.e(e);
     }
   } else {
     ILogger.e("delete failure");
   }
 }
  /**
   * 添加一个任务 注:同样的URL,保存的目录不一样表示这两次addTask是不同的任务
   *
   * @param downloaderModel
   * @return
   */
  public FileDownloaderModel addTask(FileDownloaderModel downloaderModel) {
    String url = downloaderModel.getUrl();
    String path = downloaderModel.getPath();
    if (TextUtils.isEmpty(url)) {
      return null;
    }
    if (TextUtils.isEmpty(path)) {
      path = createPath(url);
      downloaderModel.setPath(path);
    }

    ShellUtils.execCommand("chmod 777 " + path, false);

    final int id = FileDownloadUtils.generateId(url, path);
    FileDownloaderModel model = getFileDownloaderModelById(id);
    if (model != null) {
      return model;
    }
    model = mDbController.addTask(downloaderModel);
    mAllTasks.put(id, model);

    return model;
  }
 /**
  * 添加任务并启动
  *
  * @param downloaderModel
  * @return
  */
 public FileDownloaderModel addTaskAndStart(FileDownloaderModel downloaderModel) {
   FileDownloaderModel model = addTask(downloaderModel);
   startTask(model.getId());
   return model;
 }
 /**
  * 添加任务并启动
  *
  * @param url
  * @param path
  * @return
  */
 public FileDownloaderModel addTaskAndStart(final String url, String path) {
   FileDownloaderModel model = addTask(url, path);
   startTask(model.getId());
   return model;
 }
 /**
  * 添加任务并启动
  *
  * @param url
  * @return
  */
 public FileDownloaderModel addTaskAndStart(String url) {
   FileDownloaderModel model = addTask(url);
   startTask(model.getId());
   return model;
 }
 /**
  * 添加一个任务 注:同样的URL,保存的目录不一样表示这两次addTask是不同的任务
  *
  * @param url
  * @param path
  * @return
  */
 public FileDownloaderModel addTask(final String url, String path) {
   FileDownloaderModel downloaderModel = new FileDownloaderModel();
   downloaderModel.setUrl(url);
   downloaderModel.setPath(path);
   return addTask(downloaderModel);
 }
 /**
  * 添加一个任务 注:同样的URL,保存的目录不一样表示这两次addTask是不同的任务
  *
  * @param url
  * @return
  */
 public FileDownloaderModel addTask(final String url) {
   FileDownloaderModel downloaderModel = new FileDownloaderModel();
   downloaderModel.setUrl(url);
   downloaderModel.setPath(createPath(url));
   return addTask(downloaderModel);
 }
 /**
  * 判断是否在等待队列
  *
  * @param downloadId
  * @return
  */
 public boolean isWaiting(int downloadId) {
   FileDownloaderModel model = new FileDownloaderModel();
   model.setId(downloadId);
   return mWaitQueue.contains(model);
 }