// 检查缓存目录中是否已经下载过文件 private String checkFromCache(DownloadRequest request) { if (request != null && !TextUtils.isEmpty(request.mDownloadUrl)) { String saveUrl = mDownloadFilenameCreateListener != null ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl( request.mDownloadUrl) : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl( request.mDownloadUrl); if (TextUtils.isEmpty(DOWNLOADED_FILE_DIR)) { return null; } File dir = new File(DOWNLOADED_FILE_DIR); if (!dir.exists() || dir.isFile()) { return null; } String extension = TextUtils.isEmpty(request.mFileExtension) ? "" : "." + request.mFileExtension; File cachedFile = new File(DOWNLOADED_FILE_DIR + saveUrl + extension); if (cachedFile.exists()) { if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME( "<<<<< [[find in cache]] >>>>> ::::::::: " + cachedFile.getAbsolutePath()); } return cachedFile.getAbsolutePath(); } } if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME( "<<<<< [[can not find in cache]] >>>>> ::::::::: " + request.toString()); } return null; }
private DownloadRequest findRequestCanOperate(ArrayList<DownloadRequest> requestList) { if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[findRequestCanOperate]] >>>>>"); } synchronized (requestList) { for (DownloadRequest r : requestList) { if (!r.requestIsOperating.get()) { r.requestIsOperating.set(true); if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME( "<<<<< [[findRequestCanOperate]] end findRequestCanOperate >>>>>"); } return r; } } return null; } }
@Override public void run() { while (!bIsStop) { waitforUrl(); if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[run]] >>>>>"); } synchronized (mRequestList) { if (mRequestList.size() == 0) { // bIsRunning = false; bIsStop = true; break; } } if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[run]] end synchronized (mRequestList) >>>>>"); } DownloadRequest request = null; try { request = findRequestCanOperate(mRequestList); if (request == null) { bIsStop = true; } if (request != null && request.mStatus != DownloadRequest.STATUS_CANCEL) { if (DEBUG) { CommonUtilsConfig.LOGD( "================ <<" + Thread.currentThread().getName() + ">> working on : "); CommonUtilsConfig.LOGD("begin operate one request : " + request.toString()); CommonUtilsConfig.LOGD("============================================"); } String cacheFile = InternetUtils.requestBigResourceWithCache( mContext, request.mDownloadUrl, request.getHeaders()); if (DEBUG) { CommonUtilsConfig.LOGD("----- after get the cache file : " + cacheFile + " ======="); } if (!TextUtils.isEmpty(cacheFile)) { // 将文件移动到下载完成的页面 String filePath = mvFileToDownloadedDir(cacheFile, request.mFileExtension); if (!TextUtils.isEmpty(filePath)) { // notify success // 将文件移动到下载完成的页面 DownloadResponse response = tryToHandleDownloadFile(filePath, request); if (response != null) { mSuccessHandler.notifyAll(-1, -1, response); handleResponseByListener(DOWNLOAD_SUCCESS, request.mDownloadUrl, response, false); removeRequest(request); continue; } else { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false); mFailedHandler.notifyAll(-1, -1, request); continue; } } else { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false); mFailedHandler.notifyAll(-1, -1, request); continue; } } if (request.getmStatus() != DownloadRequest.STATUS_CANCEL) { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false); mFailedHandler.notifyAll(-1, -1, request); } else { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, true); } if (DEBUG) { CommonUtilsConfig.LOGD("success end operate one request : " + request); } } } catch (Exception e) { e.printStackTrace(); if (DEBUG) { CommonUtilsConfig.LOGD("Exception : ", e); CommonUtilsConfig.LOGD("exception end operate one request : " + request); CommonUtilsConfig.LOGD(e.getStackTrace().toString()); } if (request.getmStatus() != DownloadRequest.STATUS_CANCEL) { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, false); mFailedHandler.notifyAll(-1, -1, request); } else { handleResponseByListener(DOWNLOAD_FAILED, request.mDownloadUrl, request, true); } } removeRequest(request); } System.gc(); }
/** * 新提交的request会默认 * * @param request * @return */ public boolean postRequest(DownloadRequest request) { if (mRequestList == null || request == null || TextUtils.isEmpty(request.mDownloadUrl)) { return false; } if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME( "<<<<< [[postRequest]] >>>>> ::::::::: " + request.toString()); } // 检查是否已经下载过此request对应的文件 String cachedFile = checkFromCache(request); if (!TextUtils.isEmpty(cachedFile)) { File file = new File(cachedFile); if (file.exists()) { DownloadResponse response = tryToHandleDownloadFile(cachedFile, request); if (response != null) { mSuccessHandler.notifyAll(-1, -1, response); handleProcess(request.mDownloadUrl, (int) file.length(), (int) file.length()); } return true; } } synchronized (mRequestList) { boolean contain = false; for (DownloadRequest r : mRequestList) { if (r.mUrlHashCode == request.mUrlHashCode) { contain = true; break; } } if (!contain) { // mRequestList.add(request); // 将最新添加的任务放在下载队列的最前面 if (mLastInFirstDownload) { mRequestList.add(0, request); } else { mRequestList.add(request); } if (DEBUG) { CommonUtilsConfig.LOGD( "postRequest, add request : " + request.toString() + " into download list"); } } bIsStop = false; ThreadPoolSnapShot tss = CustomThreadPool.getInstance() .getSpecialThreadSnapShot(FileDownloader.class.getSimpleName()); if (tss == null) { return false; } else { if (tss.taskCount < tss.ALLOWED_MAX_TAKS) { if (DEBUG) { CommonUtilsConfig.LOGD("entry into [[postRequest]] to start process "); } processWorks(); } } } if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME( "<<<<< [[postRequest]] end synchronized (mRequestList) >>>>>"); } synchronized (objLock) { if (bIsWaiting) { bIsWaiting = false; if (DEBUG) { CommonUtilsConfig.LOGD("try to notify download process begin"); } objLock.notify(); } } if (DEBUG) { CommonUtilsConfig.LOGD_WITH_TIME("<<<<< [[postRequest]] end synchronized (objLock) >>>>>"); } return true; }