コード例 #1
0
  public byte getStatus(final int id) {
    final FileDownloadModel model = mHelper.find(id);
    if (model == null) {
      return FileDownloadStatus.INVALID_STATUS;
    }

    return model.getStatus();
  }
コード例 #2
0
  public long getTotal(final int id) {
    final FileDownloadModel model = mHelper.find(id);
    if (model == null) {
      return 0;
    }

    return model.getTotal();
  }
コード例 #3
0
  public static boolean isBreakpointAvailable(
      final int id, final FileDownloadModel model, final String path) {
    boolean result = false;

    do {
      if (path == null) {
        if (FileDownloadLog.NEED_LOG) {
          FileDownloadLog.d(FileDownloadMgr.class, "can't continue %d path = null", id);
        }
        break;
      }

      File file = new File(path);
      final boolean isExists = file.exists();
      final boolean isDirectory = file.isDirectory();

      if (!isExists || isDirectory) {
        if (FileDownloadLog.NEED_LOG) {
          FileDownloadLog.d(
              FileDownloadMgr.class,
              "can't continue %d file not suit, exists[%B], directory[%B]",
              id,
              isExists,
              isDirectory);
        }
        break;
      }

      final long fileLength = file.length();

      if (model.getSoFar() == 0) {
        if (FileDownloadLog.NEED_LOG) {
          FileDownloadLog.d(
              FileDownloadMgr.class, "can't continue %d the downloaded-record is zero.", id);
        }
        break;
      }

      if (fileLength < model.getSoFar()
          || (model.getTotal() != -1 // not chunk transfer encoding data
              && (fileLength > model.getTotal() || model.getSoFar() >= model.getTotal()))) {
        // dirty data.
        if (FileDownloadLog.NEED_LOG) {
          FileDownloadLog.d(
              FileDownloadMgr.class,
              "can't continue %d dirty data" + " fileLength[%d] sofar[%d] total[%d]",
              id,
              fileLength,
              model.getSoFar(),
              model.getTotal());
        }
        break;
      }

      result = true;
    } while (false);

    return result;
  }
コード例 #4
0
  /** @return can resume by break point */
  public static boolean isBreakpointAvailable(final int id, final FileDownloadModel model) {
    if (model == null) {
      if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(FileDownloadMgr.class, "can't continue %d model == null", id);
      }
      return false;
    }

    if (model.getTempFilePath() == null) {
      if (FileDownloadLog.NEED_LOG) {
        FileDownloadLog.d(FileDownloadMgr.class, "can't continue %d temp path == null", id);
      }
      return false;
    }

    return isBreakpointAvailable(id, model, model.getTempFilePath());
  }
コード例 #5
0
  @Override
  public boolean isDownloading(FileDownloadModel model) {
    if (model == null) {
      return false;
    }

    final boolean isInPool = mThreadPool.isInThreadPool(model.getId());
    boolean isDownloading;

    do {
      if (FileDownloadStatus.isOver(model.getStatus())) {

        //noinspection RedundantIfStatement
        if (isInPool) {
          // already finished, but still in the pool.
          // handle as downloading.
          isDownloading = true;
        } else {
          // already finished, and not in the pool.
          // make sense.
          isDownloading = false;
        }
      } else {
        if (isInPool) {
          // not finish, in the pool.
          // make sense.
          isDownloading = true;
        } else {
          // not finish, but not in the pool.
          // beyond expectation.
          FileDownloadLog.e(
              this,
              "%d status is[%s](not finish) & but not in the pool",
              model.getId(),
              model.getStatus());
          // handle as not in downloading, going to re-downloading.
          isDownloading = false;
        }
      }
    } while (false);

    return isDownloading;
  }
コード例 #6
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));
  }