/** 初始化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); }
/** * 删除一个任务 * * @param downloadId */ public void deleteTask(int downloadId) { if (mDbController.deleteTask(downloadId)) { FileDownloaderModel model = getFileDownloaderModelById(downloadId); if (model != null) { // 删除文件 new File(model.getPath()).delete(); } pauseTask(downloadId); removeDownloadingTask(downloadId); removeWaitQueueTask(downloadId); try { mAllTasks.remove(downloadId); } catch (Exception e) { ILogger.e(e); } } else { ILogger.e("delete failure"); } }
/** * 添加一个任务 注:同样的URL,保存的目录不一样表示这两次addTask是不同的任务 * * @param downloaderModel * @return */ public FileDownloaderModel addTask(FileDownloaderModel downloaderModel) { String url = downloaderModel.getUrl(); String path = downloaderModel.getPath(); if (TextUtils.isEmpty(url)) { return null; } if (TextUtils.isEmpty(path)) { path = createPath(url); downloaderModel.setPath(path); } ShellUtils.execCommand("chmod 777 " + path, false); final int id = FileDownloadUtils.generateId(url, path); FileDownloaderModel model = getFileDownloaderModelById(id); if (model != null) { return model; } model = mDbController.addTask(downloaderModel); mAllTasks.put(id, model); return model; }