/** 初始化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 url * @param listener */ public void addTask(String url, DownloadListener listener) { if (StringUtils.isEmpty(url)) { Logger.d("download url null"); return; } DownloadInfo downloadInfo = new DownloadInfo(); downloadInfo.setUrl(url); if (!hasTask(url)) { downloadInfo.setTargetFolder(mTargetFolder); try { mDbHelper.save(downloadInfo); } catch (DbException e) { Logger.e(e); } if (mDownloadingTasks.size() < MAX_DOWNLOAD_COUNT) { downloadInfo.setState(DownloadInfo.DOWNLOADING); mDownloadingTasks.add(downloadInfo); DownloadHttpTask task = new DownloadHttpTask(downloadInfo, mDownloadUIHandler, mDbHelper, this); mDownloadingTaskMap.put(url, task); task.start(); } else { // 加入等待队列 downloadInfo.setState(DownloadInfo.WAIT); boolean b = mWaitTasks.offer(downloadInfo); if (b) { addTaskListener(url, listener); } } mAllTasks.add(downloadInfo); } else { Logger.d("任务已存在"); } }
/** * 删除任务 * * @param url */ public void deleteTask(String url) { // 从等待队列和下载中 stopTask(url, true); // 从已暂停队列移除 Iterator<DownloadInfo> pausingIt = mPausingTasks.iterator(); synchronized (mIteratorLock) { while (pausingIt.hasNext()) { DownloadInfo bean = pausingIt.next(); if (TextUtils.equals(bean.getUrl(), url)) { bean.setState(DownloadInfo.PAUSE); pausingIt.remove(); break; } } } // 从完成队列移除 Iterator<DownloadInfo> completeIt = mCompleteTasks.iterator(); synchronized (mIteratorLock) { while (completeIt.hasNext()) { DownloadInfo bean = completeIt.next(); if (TextUtils.equals(bean.getUrl(), url)) { bean.setState(DownloadInfo.PAUSE); completeIt.remove(); break; } } } synchronized (mIteratorLock) { String filePath = ""; // 所有任务 Iterator<DownloadInfo> allTaskIt = mAllTasks.iterator(); while (allTaskIt.hasNext()) { DownloadInfo bean = allTaskIt.next(); if (TextUtils.equals(bean.getUrl(), url)) { bean.setState(DownloadInfo.PAUSE); filePath = bean.getTargetPath(); allTaskIt.remove(); break; } } if (!StringUtils.isEmpty(filePath)) { FileUtils.deleteFile(filePath); } } synchronized (mIteratorLock) { // 任务时间回调 for (Map.Entry<String, List<DownloadListener>> entry : mListenerListMap.entrySet()) { List<DownloadListener> listenerList = entry.getValue(); if (listenerList != null) { listenerList.clear(); listenerList = null; } } } // 清除数据库 try { mDbHelper.delete(DownloadInfo.class, WhereBuilder.b("url", "=", url)); } catch (DbException e) { Logger.e(e); } }