Exemplo n.º 1
0
  public FileDownloadMgr() {

    final DownloadMgrInitialParams params = FileDownloadHelper.getDownloadMgrInitialParams();
    mHelper = new FileDownloadDBHelper();

    final OkHttpClient client;
    final int maxNetworkThreadCount;
    if (params != null) {
      client = params.makeCustomOkHttpClient();
      maxNetworkThreadCount = params.getMaxNetworkThreadCount();
    } else {
      client = null;
      maxNetworkThreadCount = 0;
    }

    if (FileDownloadLog.NEED_LOG) {
      FileDownloadLog.d(
          this,
          "init the download manager with initialParams: "
              + "okhttpClient[is customize: %B], maxNetworkThreadCount[%d]",
          client != null,
          maxNetworkThreadCount);
    }

    // init client
    if (this.client != client) {
      this.client = client;
    } else {
      // in this case, the client must be null, see #41
      this.client = new OkHttpClient();
    }

    mThreadPool = new FileDownloadThreadPool(maxNetworkThreadCount);
  }
Exemplo n.º 2
0
 /** 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);
   }
 }
Exemplo n.º 3
0
  // synchronize for safe: check downloading, check resume, update data, execute runnable
  public synchronized void start(
      final String url,
      final String path,
      final boolean pathAsDirectory,
      final int callbackProgressTimes,
      final int callbackProgressMinIntervalMillis,
      final int autoRetryTimes,
      final boolean forceReDownload,
      final FileDownloadHeader header) {
    final int id = FileDownloadUtils.generateId(url, path, pathAsDirectory);
    FileDownloadModel model = mHelper.find(id);

    if (!pathAsDirectory && model == null) {
      // try dir data.
      final int dirCaseId =
          FileDownloadUtils.generateId(url, FileDownloadUtils.getParent(path), true);
      model = mHelper.find(dirCaseId);
      if (model != null && path.equals(model.getTargetFilePath())) {
        if (FileDownloadLog.NEED_LOG) {
          FileDownloadLog.d(this, "task[%d] find model by dirCaseId[%d]", id, dirCaseId);
        }
      }
    }

    if (FileDownloadHelper.inspectAndInflowDownloading(id, model, this)) {
      if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(this, "has already started download %d", id);
      }
      return;
    }

    final String targetFilePath =
        model != null
            ? model.getTargetFilePath()
            : FileDownloadUtils.getTargetFilePath(path, pathAsDirectory, null);

    if (FileDownloadHelper.inspectAndInflowDownloaded(id, targetFilePath, forceReDownload)) {
      if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(this, "has already completed downloading %d", id);
      }
      return;
    }

    // real start
    // - create model
    boolean needUpdate2DB;
    if (model != null
        && (model.getStatus() == FileDownloadStatus.paused
            || model.getStatus() == FileDownloadStatus.error) // FileDownloadRunnable invoke
    // #isBreakpointAvailable to determine whether it is really invalid.
    ) {
      if (model.getId() != id) {
        // in try dir case.
        mHelper.remove(model.getId());
        model.setId(id);
        model.setPath(path, pathAsDirectory);

        needUpdate2DB = true;
      } else {
        needUpdate2DB = false;
      }
    } else {
      if (model == null) {
        model = new FileDownloadModel();
      }
      model.setUrl(url);
      model.setPath(path, pathAsDirectory);

      model.setId(id);
      model.setSoFar(0);
      model.setTotal(0);
      model.setStatus(FileDownloadStatus.pending);
      needUpdate2DB = true;
    }

    // - update model to db
    if (needUpdate2DB) {
      mHelper.update(model);
    }

    // - execute
    mThreadPool.execute(
        new FileDownloadRunnable(
            client,
            this,
            model,
            mHelper,
            autoRetryTimes,
            header,
            callbackProgressMinIntervalMillis,
            callbackProgressTimes,
            forceReDownload));
  }
Exemplo n.º 4
0
 /**
  * Just cache ApplicationContext
  *
  * <p>Proposed call at{@link Application#onCreate()}
  */
 public static void init(final Application application) {
   if (FileDownloadLog.NEED_LOG) {
     FileDownloadLog.d(FileDownloader.class, "init Downloader");
   }
   FileDownloadHelper.initAppContext(application);
 }
Exemplo n.º 5
0
 /**
  * Unbind & stop ':filedownloader' process manually(Do not need, will unbind & stop automatically
  * by System if leave unused period)
  */
 public void unBindService() {
   if (isServiceConnected()) {
     FileDownloadServiceUIGuard.getImpl().unbindByContext(FileDownloadHelper.getAppContext());
   }
 }
Exemplo n.º 6
0
 /**
  * Bind & start ':filedownloader' process manually(Do not need, will bind & start automatically by
  * Download Engine if real need)
  */
 public void bindService() {
   if (!isServiceConnected()) {
     FileDownloadServiceUIGuard.getImpl().bindStartByContext(FileDownloadHelper.getAppContext());
   }
 }