예제 #1
0
  /**
   * 下载一个任务
   *
   * @param url
   * @param listener
   */
  public void addTask(String url, DownloadListener listener) {
    if (StringUtils.isEmpty(url)) {
      Logger.d("download url null");
      return;
    }

    DownloadInfo downloadInfo = new DownloadInfo();
    downloadInfo.setUrl(url);
    if (!hasTask(url)) {
      downloadInfo.setTargetFolder(mTargetFolder);
      try {
        mDbHelper.save(downloadInfo);
      } catch (DbException e) {
        Logger.e(e);
      }

      if (mDownloadingTasks.size() < MAX_DOWNLOAD_COUNT) {
        downloadInfo.setState(DownloadInfo.DOWNLOADING);
        mDownloadingTasks.add(downloadInfo);
        DownloadHttpTask task =
            new DownloadHttpTask(downloadInfo, mDownloadUIHandler, mDbHelper, this);
        mDownloadingTaskMap.put(url, task);
        task.start();
      } else { // 加入等待队列
        downloadInfo.setState(DownloadInfo.WAIT);
        boolean b = mWaitTasks.offer(downloadInfo);
        if (b) {
          addTaskListener(url, listener);
        }
      }
      mAllTasks.add(downloadInfo);
    } else {
      Logger.d("任务已存在");
    }
  }
예제 #2
0
  private DbHelper createDownloadDb(Context context) {
    try {
      DbHelper.DaoConfig config = new DbHelper.DaoConfig(context);
      config.setDbName("download.db");
      config.setDbVersion(1);
      DbHelper dbHelper = DbHelper.create(config);
      return dbHelper;
    } catch (Exception e) {
      Logger.e(e);
    }

    return null;
  }
예제 #3
0
  public DownloadManager(Context context) {

    mAllTasks = Collections.synchronizedList(new ArrayList());
    mDownloadingTasks = Collections.synchronizedList(new ArrayList());
    mPausingTasks = Collections.synchronizedList(new ArrayList());
    mCompleteTasks = Collections.synchronizedList(new ArrayList());
    mWaitTasks = new ConcurrentLinkedQueue<>();
    mDownloadingTaskMap = Collections.synchronizedMap(new HashMap<String, DownloadHttpTask>());
    mListenerListMap = Collections.synchronizedMap(new HashMap<String, List<DownloadListener>>());
    mDownloadUIHandler = new DownloadUIHandler(mListenerListMap);

    // 初始化目标Download保存目录
    String folder =
        StorageUtils.getCacheDirectory(context).getAbsolutePath() + Constants.DM_TARGET_FOLDER;
    if (!new File(folder).exists()) {
      FileUtils.makeFolders(folder);
    }
    mTargetFolder = folder;

    // 构建下载Downlaod DbHelper
    mDbHelper = createDownloadDb(context);
    // 获取所有任务
    if (mDbHelper != null) {
      try {
        List<DownloadInfo> list = mDbHelper.findAll(DownloadInfo.class);
        if (list != null && list.size() > 0) {

          for (DownloadInfo info : list) {
            if (info.getProgress() == 100) {
              info.setState(DownloadInfo.COMPLETE);
              mCompleteTasks.add(info);
            } else {
              info.setState(DownloadInfo.PAUSE);
              mPausingTasks.add(info);
            }
          }

          mAllTasks.addAll(mPausingTasks);
          mAllTasks.addAll(mCompleteTasks);
        }
      } catch (DbException e) {
        Logger.e(e);
      }
    }
  }
예제 #4
0
  /**
   * 删除任务
   *
   * @param url
   */
  public void deleteTask(String url) {
    // 从等待队列和下载中
    stopTask(url, true);
    // 从已暂停队列移除
    Iterator<DownloadInfo> pausingIt = mPausingTasks.iterator();
    synchronized (mIteratorLock) {
      while (pausingIt.hasNext()) {
        DownloadInfo bean = pausingIt.next();
        if (TextUtils.equals(bean.getUrl(), url)) {
          bean.setState(DownloadInfo.PAUSE);
          pausingIt.remove();
          break;
        }
      }
    }

    // 从完成队列移除
    Iterator<DownloadInfo> completeIt = mCompleteTasks.iterator();
    synchronized (mIteratorLock) {
      while (completeIt.hasNext()) {
        DownloadInfo bean = completeIt.next();
        if (TextUtils.equals(bean.getUrl(), url)) {
          bean.setState(DownloadInfo.PAUSE);
          completeIt.remove();
          break;
        }
      }
    }

    synchronized (mIteratorLock) {
      String filePath = "";
      // 所有任务
      Iterator<DownloadInfo> allTaskIt = mAllTasks.iterator();
      while (allTaskIt.hasNext()) {
        DownloadInfo bean = allTaskIt.next();
        if (TextUtils.equals(bean.getUrl(), url)) {
          bean.setState(DownloadInfo.PAUSE);
          filePath = bean.getTargetPath();
          allTaskIt.remove();
          break;
        }
      }

      if (!StringUtils.isEmpty(filePath)) {
        FileUtils.deleteFile(filePath);
      }
    }

    synchronized (mIteratorLock) {
      // 任务时间回调
      for (Map.Entry<String, List<DownloadListener>> entry : mListenerListMap.entrySet()) {
        List<DownloadListener> listenerList = entry.getValue();
        if (listenerList != null) {
          listenerList.clear();
          listenerList = null;
        }
      }
    }

    // 清除数据库
    try {
      mDbHelper.delete(DownloadInfo.class, WhereBuilder.b("url", "=", url));
    } catch (DbException e) {
      Logger.e(e);
    }
  }