Java Code Examples for com.liulishuo.filedownloader.util.FileDownloadUtils#generateId()

The following examples show how to use com.liulishuo.filedownloader.util.FileDownloadUtils#generateId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TasksManager.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 将某个链接资源添加到下载的队列中
 * @param url               资源url
 * @param path              路径
 * @return                  model
 */
public TasksManagerModel addTask(final String url, final String path) {
    if (TextUtils.isEmpty(url) || TextUtils.isEmpty(path)) {
        return null;
    }
    //根据链接和地址查找对应id
    final int id = FileDownloadUtils.generateId(url, path);
    TasksManagerModel model = getById(id);
    if (model != null) {
        return model;
    }
    //天机到数据库
    final TasksManagerModel newModel = dbController.addTask(url, path);
    if (newModel != null) {
        modelList.add(newModel);
    }
    return newModel;
}
 
Example 2
Source File: TasksManager.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 将文件音视频内容添加到数据库中
 * @param model                 model
 */
public void addDownloadedDb(DialogListBean model) {
    String url = model.getVideo();
    String path = TasksManager.getImpl().createPath(url);
    if (TextUtils.isEmpty(url) || TextUtils.isEmpty(path)) {
        return ;
    }
    final int id = FileDownloadUtils.generateId(url, path);
    TasksManagerModel bean = new TasksManagerModel();
    bean.setId(id);
    bean.setName(model.getName());
    bean.setUrl(url);
    bean.setUrl(path);
    modelListed.add(bean);
    dbController.addDownloadedDb(bean);
}
 
Example 3
Source File: TasksManagerDemoActivity.java    From FileDownloader with Apache License 2.0 6 votes vote down vote up
public TasksManagerModel addTask(final String url, final String path) {
    if (TextUtils.isEmpty(url) || TextUtils.isEmpty(path)) {
        return null;
    }

    final int id = FileDownloadUtils.generateId(url, path);
    TasksManagerModel model = getById(id);
    if (model != null) {
        return model;
    }
    final TasksManagerModel newModel = dbController.addTask(url, path);
    if (newModel != null) {
        modelList.add(newModel);
    }

    return newModel;
}
 
Example 4
Source File: TasksManagerDemoActivity.java    From FileDownloader with Apache License 2.0 6 votes vote down vote up
public TasksManagerModel addTask(final String url, final String path) {
    if (TextUtils.isEmpty(url) || TextUtils.isEmpty(path)) {
        return null;
    }

    // have to use FileDownloadUtils.generateId to associate TasksManagerModel with FileDownloader
    final int id = FileDownloadUtils.generateId(url, path);

    TasksManagerModel model = new TasksManagerModel();
    model.setId(id);
    model.setName(DemoApplication.CONTEXT.getString(R.string.tasks_manager_demo_name, id));
    model.setUrl(url);
    model.setPath(path);

    final boolean succeed = db.insert(TABLE_NAME, null, model.toContentValues()) != -1;
    return succeed ? model : null;
}
 
Example 5
Source File: TasksManager.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 获取下载文件的下载id
 * @param url                   下载链接
 * @return                      下载id
 */
public int getId(String url) {
    if (TextUtils.isEmpty(url)){
        return -1 ;
    }
    return FileDownloadUtils.generateId(url, createPath(url));
}
 
Example 6
Source File: TasksManagerDBController.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 添加到数据库
 * @param url                   下载链接
 * @param path                  下载路径
 * @return                      实体类model
 */
TasksManagerModel addTask(final String url, final String path) {
    if (TextUtils.isEmpty(url) || TextUtils.isEmpty(path)) {
        return null;
    }
    // 必须使用文件下载器,将TasksManagerModel与文件下载器关联
    final int id = FileDownloadUtils.generateId(url, path);
    TasksManagerModel model = new TasksManagerModel();
    model.setId(id);
    model.setName(url.substring(url.lastIndexOf("/")));
    model.setUrl(url);
    model.setPath(path);
    final boolean succeed = db.insert(TABLE_NAME, null, model.toContentValues()) != -1;
    return succeed ? model : null;
}
 
Example 7
Source File: FileDownloadTaskAtom.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
public int getId() {
    if (id != 0) {
        return id;
    }

    return id = FileDownloadUtils.generateId(getUrl(), getPath());
}
 
Example 8
Source File: DownloadTask.java    From FileDownloader with Apache License 2.0 5 votes vote down vote up
@Override
public int getId() {
    if (mId != 0) {
        return mId;
    }

    if (!TextUtils.isEmpty(mPath) && !TextUtils.isEmpty(mUrl)) {
        return mId = FileDownloadUtils.generateId(mUrl, mPath, mPathAsDirectory);
    }

    return 0;
}
 
Example 9
Source File: DownloadLaunchRunnable.java    From FileDownloader with Apache License 2.0 4 votes vote down vote up
private void checkupAfterGetFilename() throws RetryDirectly, DiscardSafely {
    final int id = model.getId();

    if (model.isPathAsDirectory()) {
        // this scope for caring about the case of there is another task is provided
        // the same path to store file and the same url.

        final String targetFilePath = model.getTargetFilePath();

        // get the ID after got the filename.
        final int fileCaseId = FileDownloadUtils.generateId(model.getUrl(),
                targetFilePath);

        // whether the file with the filename has been existed.
        if (FileDownloadHelper.inspectAndInflowDownloaded(id,
                targetFilePath, isForceReDownload, false)) {
            database.remove(id);
            database.removeConnections(id);
            throw new DiscardSafely();
        }

        final FileDownloadModel fileCaseModel = database.find(fileCaseId);

        if (fileCaseModel != null) {
            // the task with the same file name and url has been exist.

            // whether the another task with the same file and url is downloading.
            if (FileDownloadHelper.inspectAndInflowDownloading(id, fileCaseModel,
                    threadPoolMonitor, false)) {
                //it has been post to upper layer the 'warn' message, so the current
                // task no need to continue download.
                database.remove(id);
                database.removeConnections(id);
                throw new DiscardSafely();
            }

            final List<ConnectionModel> connectionModelList = database
                    .findConnectionModel(fileCaseId);

            // the another task with the same file name and url is paused
            database.remove(fileCaseId);
            database.removeConnections(fileCaseId);
            FileDownloadUtils.deleteTargetFile(model.getTargetFilePath());

            if (FileDownloadUtils.isBreakpointAvailable(fileCaseId, fileCaseModel)) {
                model.setSoFar(fileCaseModel.getSoFar());
                model.setTotal(fileCaseModel.getTotal());
                model.setETag(fileCaseModel.getETag());
                model.setConnectionCount(fileCaseModel.getConnectionCount());
                database.update(model);

                // re connect to resume from breakpoint.
                if (connectionModelList != null) {
                    for (ConnectionModel connectionModel : connectionModelList) {
                        connectionModel.setId(id);
                        database.insertConnectionModel(connectionModel);
                    }
                }

                // retry
                throw new RetryDirectly();
            }
        }

        // whether there is an another running task with the same target-file-path.
        if (FileDownloadHelper.inspectAndInflowConflictPath(id, model.getSoFar(),
                model.getTempFilePath(),
                targetFilePath,
                threadPoolMonitor)) {
            database.remove(id);
            database.removeConnections(id);

            throw new DiscardSafely();
        }
    }
}