コード例 #1
0
  /** 初始化DownloadManager */
  public synchronized void init(DownloaderManagerConfiguration configuration) {
    FileDownloader.init(configuration.getContext(), configuration.getOkHttpClientCustomMaker());
    FileDownloader.getImpl().bindService();

    ILogger.DEBUG = configuration.isDebug();
    FileDownloadLog.NEED_LOG = ILogger.DEBUG;

    this.mConfiguration = configuration;
    this.mExtFieldMap = configuration.getDbExtField();
    mDbController =
        new FileDownloaderDBController(
            configuration.getContext(),
            configuration.getDbVersion(),
            mExtFieldMap,
            configuration.getDbUpgradeListener());
    mAllTasks = mDbController.getAllTasks();
    mConnectListenerList = new ArrayList<>();
    mListenerManager = new ListenerManager();
    mAutoRetryTimes = configuration.getAutoRetryTimes();
    mHeaders = configuration.getHeaders();

    // 设置下载保存目录
    if (!StringUtils.isEmpty(configuration.getDownloadStorePath())) {
      FileDownloadUtils.setDefaultSaveRootPath(configuration.getDownloadStorePath());
    }

    mWaitQueue = new LinkedList<>();
    mDownloadingList = Collections.synchronizedList(new ArrayList<FileDownloaderModel>());
    mDownloadManager = this;

    ShellUtils.execCommand("chmod 777 " + configuration.getDownloadStorePath(), false);
  }
コード例 #2
0
 /**
  * 开始下载任务
  *
  * @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!");
   }
 }
コード例 #3
0
 /** 释放下载管理器 */
 public void onDestroy() {
   try {
     mConnectListenerList.clear();
     pauseAllTask();
     FileDownloader.getImpl().unBindServiceIfIdle();
   } catch (Exception e) {
   }
 }
コード例 #4
0
 /**
  * 暂停任务
  *
  * @param downloadId
  */
 public synchronized void pauseTask(int downloadId) {
   if (isWaiting(downloadId)) {
     BridgeListener bridgeListener = mListenerManager.getBridgeListener(downloadId);
     removeWaitQueueTask(downloadId);
     bridgeListener.stop(downloadId, getSoFar(downloadId), getTotal(downloadId));
   } else {
     FileDownloader.getImpl().pause(downloadId);
   }
 }
コード例 #5
0
  @Override
  public void onCreate() {
    super.onCreate();

    // 下面这句为了测试,正常使用的时候不用添加
    FileDownloadLog.NEED_LOG = BuildConfig.DEBUG;

    // 不耗时,做一些简单初始化准备工作,不会启动下载进程
    FileDownloader.init(this);

    //        FileDownloader.getImpl().bindService();
  }
コード例 #6
0
 /**
  * 根据downloadId获取文件已下载大小
  *
  * @param downloadId
  * @return
  */
 public long getSoFar(final int downloadId) {
   return FileDownloader.getImpl().getSoFar(downloadId);
 }
コード例 #7
0
 /**
  * 根据downloadId获取文件总大小
  *
  * @param downloadId
  * @return
  */
 public long getTotal(final int downloadId) {
   return FileDownloader.getImpl().getTotal(downloadId);
 }
コード例 #8
0
 /**
  * 获取FileDownloader FileDownloadStatus下载状态
  *
  * @param downloadId
  * @return
  */
 private int getStatus(final int downloadId) {
   return FileDownloader.getImpl().getStatus(downloadId);
 }
コード例 #9
0
 /**
  * 获取service是否已连接
  *
  * @return
  */
 public boolean isReady() {
   return FileDownloader.getImpl().isServiceConnected();
 }
コード例 #10
0
 /**
  * 移除sevice连接监听
  *
  * @param listener
  */
 public void removeServiceConnectListener(FileDownloadConnectListener listener) {
   FileDownloader.getImpl().removeServiceConnectListener(listener);
 }
コード例 #11
0
 /**
  * 添加service连接监听
  *
  * @param listener
  */
 public void addServiceConnectListener(FileDownloadConnectListener listener) {
   FileDownloader.getImpl().addServiceConnectListener(listener);
 }
コード例 #12
0
 /** 停止所有任务 */
 public void pauseAllTask() {
   FileDownloader.getImpl().pauseAll();
 }