/** * 开始下载任务 * * @param downloadId * @param callback */ public void startTask(int downloadId, FileDownloaderCallback callback) { FileDownloaderModel model = getFileDownloaderModelById(downloadId); if (model != null) { BridgeListener bridgeListener = mListenerManager.getBridgeListener(downloadId); bridgeListener.addDownloadListener(callback); if (mDownloadingList.size() >= mConfiguration.getMaxDownloadingCount()) { // 下载中队列已满 if (!mWaitQueue.contains(model)) { mWaitQueue.offer(model); } bridgeListener.wait(downloadId); } else { mDownloadingList.add(model); final BaseDownloadTask task = FileDownloader.getImpl() .create(model.getUrl()) .setPath(model.getPath()) .setCallbackProgressTimes(100) .setAutoRetryTimes(mAutoRetryTimes) .setListener(bridgeListener); for (int i = 0; i < mHeaders.size(); i++) { task.addHeader(mHeaders.name(i), mHeaders.value(i)); } bridgeListener.setDownloadTask(task); task.start(); } } else { ILogger.e("Task does not exist!"); } }
/** 初始化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); }
/** 释放下载管理器 */ public void onDestroy() { try { mConnectListenerList.clear(); pauseAllTask(); FileDownloader.getImpl().unBindServiceIfIdle(); } catch (Exception e) { } }
/** * 暂停任务 * * @param downloadId */ public synchronized void pauseTask(int downloadId) { if (isWaiting(downloadId)) { BridgeListener bridgeListener = mListenerManager.getBridgeListener(downloadId); removeWaitQueueTask(downloadId); bridgeListener.stop(downloadId, getSoFar(downloadId), getTotal(downloadId)); } else { FileDownloader.getImpl().pause(downloadId); } }
/** * 根据downloadId获取文件已下载大小 * * @param downloadId * @return */ public long getSoFar(final int downloadId) { return FileDownloader.getImpl().getSoFar(downloadId); }
/** * 根据downloadId获取文件总大小 * * @param downloadId * @return */ public long getTotal(final int downloadId) { return FileDownloader.getImpl().getTotal(downloadId); }
/** * 获取FileDownloader FileDownloadStatus下载状态 * * @param downloadId * @return */ private int getStatus(final int downloadId) { return FileDownloader.getImpl().getStatus(downloadId); }
/** * 获取service是否已连接 * * @return */ public boolean isReady() { return FileDownloader.getImpl().isServiceConnected(); }
/** * 移除sevice连接监听 * * @param listener */ public void removeServiceConnectListener(FileDownloadConnectListener listener) { FileDownloader.getImpl().removeServiceConnectListener(listener); }
/** * 添加service连接监听 * * @param listener */ public void addServiceConnectListener(FileDownloadConnectListener listener) { FileDownloader.getImpl().addServiceConnectListener(listener); }
/** 停止所有任务 */ public void pauseAllTask() { FileDownloader.getImpl().pauseAll(); }