/// <summary> /// Cancels any in-progress or future download of the DownloadBatchRequest by the specified // identifier. /// </summary> /// <param name="downloadBatchRequestId"></param> public void cancel(UUID downloadBatchRequestId) { // Locals DownloadBatchRequest batchRequest; DownloadBatchInformation downloadBatchInformation; // Get batch request batchRequest = _downloadQueue.getDownloadBatchRequest(downloadBatchRequestId); if (batchRequest == null) { return; } // Cancel each item in batch for (DownloadRequest request : batchRequest.getDownloadRequests()) { cancel(request.getUrl()); } // Remove batch from queue _downloadQueue.remove(downloadBatchRequestId); // Synchronize synchronized (_downloadBatchInformationLock) { // Remove batch's download information (if any) downloadBatchInformation = _downloadBatchInformationById.get(downloadBatchRequestId); if (downloadBatchInformation != null) { _downloadBatchInformationById.remove(downloadBatchRequestId); } } // Fire batch cancelled event this.DownloadBatchCancelled.fireEvent(new DownloadEvent(this, null, downloadBatchInformation)); }
/// <summary> /// Cancels any in-progress or future download of the DownloadRequest by the specified URL. /// </summary> /// <param name="url"></param> public void cancel(String url) { // Locals DownloadRequest request; // Cancel any in-progress download _downloadRequestUrlsThatMayProceed.remove(url); // Remove download from queue DownloadInformation di = getDownloadInformation(url); request = _downloadQueue.remove(url); if (request == null) { return; } // Was the request partially completed? if (request.getAvailableLength() > 0) { // Delete any portion of the item from storage String filePath = request.getFilePath(); if (filePath.startsWith("file://")) { filePath = filePath.substring(7); } File file = new File(filePath); if (file.exists() == true) { file.delete(); } // Delete item from the completed catalog (if it exists) _completedDownloadCatalog.deleteCompletedDownload(url); } // Fire event this.DownloadCancelled.fireEvent(new DownloadEvent(this, di, null)); }
/** Cancel all downloads requests and shutdown this download queue */ public void cancelAll() { for (DownloadRequest request : current) { request.setCancelled(true); } executor.shutdownNow(); }
private DownloadResult processRequest(ManagedDownloadRequest mRequest) { DownloadResult result = null; DownloadRequest request = null; if (!stop) { try { request = mRequest.getOriginalRequest(); result = new DownloadResult(); if (request == null) { throw new Exception("Request is null"); } if (!request.getSource().isWellFormed()) { throw new Exception("Datasource in not WellFormed"); } String pageContent = HttpTools.getPageContent(request); if (pageContent != null) { // try loading Marker data List<Marker> markers = DataConvertor.getInstance() .load(request.getSource().getUrl(), pageContent, request.getSource()); result.setAccomplish(mRequest.getUniqueKey(), markers, request.getSource()); // Log.d("test", request.getSource().getType().name()); } } catch (Exception ex) { result.setError(ex, request); Log.w(MixContext.TAG, "ERROR ON DOWNLOAD REQUEST", ex); } } return result; }
private Downloader getDownloader(DownloadRequest request) { if (URLUtil.isHttpUrl(request.getSource()) || URLUtil.isHttpsUrl(request.getSource())) { return new HttpDownloader(request); } Log.e(TAG, "Could not find appropriate downloader for " + request.getSource()); return null; }
/// <summary> /// Pauses any in-progress or future download of the DownloadBatchRequest by the specified // identifier. /// </summary> /// <param name="downloadBatchRequestId"></param> public void pause(UUID downloadBatchRequestId) { // Locals DownloadBatchRequest batchRequest; DownloadBatchInformation downloadBatchInformation; // Get batch downloadInformation batchRequest = _downloadQueue.getDownloadBatchRequest(downloadBatchRequestId); if (batchRequest == null) { return; } // Pause each item in batch for (DownloadRequest request : batchRequest.getDownloadRequests()) { pause(request.getUrl()); } // Fire batch paused event if (this.DownloadBatchPaused != null) { // Synchronize synchronized (_downloadBatchInformationLock) { downloadBatchInformation = _downloadBatchInformationById.get(downloadBatchRequestId); } this.DownloadBatchPaused.fireEvent(new DownloadEvent(this, null, downloadBatchInformation)); } }
/// <summary> /// Resumes any in-progress or future download of the DownloadRequest by the specified URL. /// </summary> /// <param name="url"></param> public void resume(String url) { // Locals DownloadRequest request; // Reset status of download request in queue so that downloading may resume request = _downloadQueue.getDownloadRequest(url); if (request == null) { return; } request.setDownloadStatus(DownloadStatus.None); }
/* * (non-Javadoc) * * @see * org.mixare.mgr.downloader.DownloadManager#submitJob(org.mixare.mgr.downloader * .DownloadRequest) */ public String submitJob(DownloadRequest job) { String jobId = null; if (job != null && job.getSource().isWellFormed()) { ManagedDownloadRequest mJob; if (!todoList.contains(job)) { mJob = new ManagedDownloadRequest(job); todoList.add(mJob); Log.i(MixView.TAG, "Submitted " + job.toString()); jobId = mJob.getUniqueKey(); } } return jobId; }
/// <summary> /// Resumes any in-progress or future download of the DownloadBatchRequest by the specified // identifier. /// </summary> /// <param name="downloadBatchRequestId"></param> public void resume(UUID downloadBatchRequestId) { // Locals DownloadBatchRequest batchRequest; // Get batch downloadInformation batchRequest = _downloadQueue.getDownloadBatchRequest(downloadBatchRequestId); if (batchRequest == null) { return; } // Resume each item in batch for (DownloadRequest request : batchRequest.getDownloadRequests()) { resume(request.getUrl()); } }
@Override public void run() { FeedImage image = DBReader.getFeedImage(DownloadService.this, request.getFeedfileId()); if (image == null) { throw new IllegalStateException("Could not find downloaded image in database"); } image.setFile_url(request.getDestination()); image.setDownloaded(true); saveDownloadStatus(status); sendDownloadHandledIntent(); DBWriter.setFeedImage(DownloadService.this, image); numberOfDownloads.decrementAndGet(); queryDownloadsAsync(); }
/** This method is called when a user presses a button (see res/layout/activity_download.xml) */ public void runService(View view) { Uri uri = Uri.parse(getUrlString()); hideKeyboard(); switch (view.getId()) { case R.id.bound_sync_button: // TODO - You fill in here to use mDownloadCall to // download the image & then display it. try { displayBitmap(mDownloadCall.downloadImage(uri)); } catch (RemoteException e) { } break; case R.id.bound_async_button: // TODO - You fill in here to call downloadImage() on // mDownloadRequest, passing in the appropriate Uri and // callback. try { mDownloadRequest.downloadImage(uri, mDownloadCallback); } catch (RemoteException e) { } break; } }
// 检查缓存目录中是否已经下载过文件 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; }
/// <summary> /// Pauses any in-progress or future download of the DownloadRequest by the specified URL. /// </summary> /// <param name="url"></param> public void pause(String url) { // Locals DownloadRequest request; // Cancel any in-progress download _downloadRequestUrlsThatMayProceed.remove(url); // Set status of download request in queue to paused request = _downloadQueue.getDownloadRequest(url); if (request == null) { return; } request.setDownloadStatus(DownloadStatus.Paused); // Fire event this.DownloadPaused.fireEvent(new DownloadEvent(this, getDownloadInformation(url), null)); }
@Override public void run() { if (request.isDeleteOnFailure()) { if (BuildConfig.DEBUG) Log.d(TAG, "Ignoring failed download, deleteOnFailure=true"); } else { File dest = new File(request.getDestination()); if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { Log.d(TAG, "File has been partially downloaded. Writing file url"); FeedMedia media = DBReader.getFeedMedia(DownloadService.this, request.getFeedfileId()); media.setFile_url(request.getDestination()); try { DBWriter.setFeedMedia(DownloadService.this, media).get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }
/** * Updates the contents of the service's notifications. Should be called before * setupNotificationBuilders. */ @SuppressLint("NewApi") private Notification updateNotifications() { String contentTitle = getString(R.string.download_notification_title); int numDownloads = requester.getNumberOfDownloads(); String downloadsLeft; if (numDownloads > 0) { downloadsLeft = requester.getNumberOfDownloads() + getString(R.string.downloads_left); } else { downloadsLeft = getString(R.string.downloads_processing); } if (android.os.Build.VERSION.SDK_INT >= 16) { if (notificationBuilder != null) { StringBuilder bigText = new StringBuilder(""); for (int i = 0; i < downloads.size(); i++) { Downloader downloader = downloads.get(i); final DownloadRequest request = downloader.getDownloadRequest(); if (request.getFeedfileType() == Feed.FEEDFILETYPE_FEED) { if (request.getTitle() != null) { if (i > 0) { bigText.append("\n"); } bigText.append("\u2022 " + request.getTitle()); } } else if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { if (request.getTitle() != null) { if (i > 0) { bigText.append("\n"); } bigText.append( "\u2022 " + request.getTitle() + " (" + request.getProgressPercent() + "%)"); } } } notificationBuilder.setSummaryText(downloadsLeft); notificationBuilder.setBigContentTitle(contentTitle); if (bigText != null) { notificationBuilder.bigText(bigText.toString()); } return notificationBuilder.build(); } } else { if (notificationCompatBuilder != null) { notificationCompatBuilder.setContentTitle(contentTitle); notificationCompatBuilder.setContentText(downloadsLeft); return notificationCompatBuilder.build(); } } return null; }
private Feed parseFeed(DownloadRequest request) { Feed savedFeed = null; Feed feed = new Feed(request.getSource(), new Date()); feed.setFile_url(request.getDestination()); feed.setId(request.getFeedfileId()); feed.setDownloaded(true); feed.setPreferences( new FeedPreferences(0, true, request.getUsername(), request.getPassword())); DownloadError reason = null; String reasonDetailed = null; boolean successful = true; FeedHandler feedHandler = new FeedHandler(); try { feed = feedHandler.parseFeed(feed).feed; if (BuildConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed"); if (checkFeedData(feed) == false) { throw new InvalidFeedException(); } } catch (SAXException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (IOException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (ParserConfigurationException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (UnsupportedFeedtypeException e) { e.printStackTrace(); successful = false; reason = DownloadError.ERROR_UNSUPPORTED_TYPE; reasonDetailed = e.getMessage(); } catch (InvalidFeedException e) { e.printStackTrace(); successful = false; reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } // cleanup(); if (savedFeed == null) { savedFeed = feed; } if (successful) { return savedFeed; } else { saveDownloadStatus( new DownloadStatus( savedFeed, savedFeed.getHumanReadableIdentifier(), reason, successful, reasonDetailed)); return null; } }
public Builder(String downloadUrl) { mRequest = new DownloadRequest(downloadUrl); mRequest.mStatus = STATUS_NORMAL; mRequest.mUrlHashCode = downloadUrl == null ? 0 : downloadUrl.hashCode(); }
public Builder setExtension(String ext) { mRequest.mFileExtension = ext; return this; }
public Builder setDownloadType(DOWNLOAD_TYPE type) { mRequest.mType = type; return this; }
public void run() { // Locals Thread downloadThread; DownloadRequest request; // Initialize _status = ProgressiveDownloaderStatus.Started; // Perform initial wait try { Thread.sleep(_waitMsUponStart); Log.d(LCAT, "Download Queue Count: " + _downloadQueue.getDownloadRequestCount()); // Loop while (_timeToStopControllerThread == false) { // Has the maximum simultaneous downloads been reached? if (_downloadQueue.getDownloadRequestCount() != 0 && _downloadRequestUrlsThatMayProceed.size() < _maximumSimultaneousDownloads) { ArrayList<String> removeUrls = new ArrayList<String>(); EnumSet<NetworkTypes> network = getNetworkTypes(); for (Entry<String, EnumSet<NetworkTypes>> entry : _downloadRequestUrlsThatMayProceed.entrySet()) { if (entry.getValue().containsAll(network) == false) { removeUrls.add(entry.getKey()); } } for (String url : removeUrls) { _downloadRequestUrlsThatMayProceed.remove(url); request = _downloadQueue.getDownloadRequest(url); if (request != null) { request.setDownloadStatus(DownloadStatus.None); } } // Log.d(LCAT, "Need to download item?"); // Anything to download? request = _downloadQueue.getNextDownloadCandidate(network); if (request != null && _downloadRequestUrlsThatMayProceed.containsKey(request.getUrl()) == false) { // Log.d(LCAT, "Found item to download start download thread?"); // Initialize download thread downloadThread = new DownloadThread(request); if (request.getDownloadPriority() == DownloadPriority.Low) { downloadThread.setPriority(DOWNLOAD_THREAD_PRIORITY_LOW); } else if (request.getDownloadPriority() == DownloadPriority.High) { downloadThread.setPriority(DOWNLOAD_THREAD_PRIORITY_HIGH); } else { downloadThread.setPriority(DOWNLOAD_THREAD_PRIORITY_NORMAL); } downloadThread.setName(DOWNLOAD_THREAD_NAME_PREFACE + request.getName()); _downloadRequestUrlsThatMayProceed.put( request.getUrl(), request.getFinalPermittedNetworkTypes()); // Update status of download request if (request.getDownloadStatus() == DownloadStatus.None) { request.setDownloadStatus(DownloadStatus.InProgress); } // Begin download thread Log.d(LCAT, "Controller thread starting download thread"); downloadThread.start(); } } // Wait between loops Thread.sleep(WAIT_MS_BETWEEN_QUEUED_REQUEST_CHECKS); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void run() { Log.d(LCAT, "Download thread started"); // Locals HttpClient webRequest1; DownloadRequest downloadRequest; DownloadInformation downloadInformation; DownloadBatchRequest downloadBatchRequest; DownloadBatchInformation downloadBatchInformation; DownloadPriority priority; // Initialize downloadInformation = null; downloadBatchInformation = null; downloadRequest = this.request; downloadBatchRequest = downloadRequest.getDownloadBatchRequestId() == null ? null : _downloadQueue.getDownloadBatchRequest(downloadRequest.getDownloadBatchRequestId()); priority = downloadRequest.getDownloadPriority(); try { // Initialize download information to report progress downloadInformation = new DownloadInformation(); downloadInformation.setUrl(downloadRequest.getUrl()); downloadInformation.setName(downloadRequest.getName()); downloadInformation.setLocale(downloadRequest.getLocale()); downloadInformation.setFilePath(downloadRequest.getFilePath()); downloadInformation.setLength(downloadRequest.getLength()); downloadInformation.setMediaBitsPerSecond(downloadRequest.getMediaBitsPerSecond()); downloadInformation.setAvailableLength(downloadRequest.getAvailableLength()); downloadInformation.setCreationUtc(downloadRequest.getCreationUtc()); downloadInformation.setLastWriteUtc(downloadRequest.getLastWriteUtc()); downloadInformation.setLastDownloadBitsPerSecond( downloadRequest.getLastDownloadBitsPerSecond()); downloadInformation.setDownloadPriority(downloadRequest.getDownloadPriority()); downloadInformation.setIsReadyForPlayback(downloadRequest.getIsReadyForPlayback()); downloadInformation.setPermittedNetworkTypes( downloadRequest.getFinalPermittedNetworkTypes()); downloadInformation.setStorageLocation(downloadRequest.getFinalStorageLocation()); if (downloadBatchRequest != null) { // Get or create batch download information synchronized (_downloadBatchInformationLock) { downloadBatchInformation = _downloadBatchInformationById.get(downloadBatchRequest.getDownloadBatchRequestId()); if (downloadBatchInformation == null) { downloadBatchInformation = new DownloadBatchInformation(); downloadBatchInformation.setDownloadBatchRequestId( downloadBatchRequest.getDownloadBatchRequestId()); downloadBatchInformation.setName(downloadBatchRequest.getName()); downloadBatchInformation.setLocale(downloadBatchRequest.getLocale()); downloadBatchInformation.setOverrideStorageLocation( downloadBatchRequest.getOverrideStorageLocation()); downloadBatchInformation.setDownloadPriority( downloadBatchRequest.getDownloadPriority()); _downloadBatchInformationById.put( downloadBatchRequest.downloadBatchRequestId, downloadBatchInformation); } // Add this download's information to the batch's collection of download informations downloadBatchInformation.getDownloadInformations().add(downloadInformation); } } // Locals int byteCount; byte[] buffer; FileOutputStream fileStream; long bpsTrackingSpan; Date bpsTrackingStart; int trackBpsIterationCount; int bytesForBps; int fireProgressEventCount; boolean downloadBatchRequestIsComplete; // Initialize fileStream = null; fireProgressEventCount = 0; trackBpsIterationCount = 0; bpsTrackingSpan = 0; bytesForBps = 0; InputStream responseStream = null; try { // Open output file and seek to next write position String filePath = downloadRequest.getFilePath(); if (filePath.startsWith("file://")) { filePath = filePath.substring(7); } String message = "resume"; Log.d(LCAT, "Creating file for writing: " + filePath); File file = new File(filePath); if (file.exists() == false) { message = "start"; boolean createRet = file.createNewFile(); Log.d(LCAT, "file.createNewFile returned " + createRet); } downloadInformation.setMessage(message); DownloadStarted.fireEvent(new DownloadEvent(this, downloadInformation, null)); Log.d( LCAT, "File length: " + file.length() + " available length: " + downloadRequest.getAvailableLength()); if (file.length() != downloadRequest.getAvailableLength()) { Log.d(LCAT, "File length and available length were different so using file length"); downloadRequest.setAvailableLength(file.length()); } webRequest1 = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(downloadRequest.getUrl())); if (downloadRequest.getAvailableLength() > OFFSET_ZERO) { request.setHeader( "Range", "bytes=" + downloadRequest.getAvailableLength() + '-' + downloadRequest.getLength()); // Start downloading after already-downloaded bytes // webRequest1.AddRange(downloadRequest.getAvailableLength()); } // webRequest1.AutomaticDecompression = DecompressionMethods.GZip | // DecompressionMethods.Deflate; HttpResponse response = webRequest1.execute(request); responseStream = response.getEntity().getContent(); FileOutputStream outputStream; // Get response if (response.containsHeader(HTTP_RESPONSE_HEADER_ACCEPT_RANGES) == true && response.getFirstHeader(HTTP_RESPONSE_HEADER_ACCEPT_RANGES).getValue() == HTTP_RESPONSE_HEADER_ACCEPT_RANGES_NONE) { // Response is for entire file, or server does not support byte ranges, so reset // available length to 0 Log.d(LCAT, "Resetting available length"); downloadRequest.setAvailableLength(0); downloadInformation.setAvailableLength(0); outputStream = new FileOutputStream(file, false); } else { outputStream = new FileOutputStream(file, true); } // On first download downloadInformation for file, set length of downloadRequest if (downloadRequest.getLength() == 0 || downloadRequest.getAvailableLength() == 0) { long contentLength = Long.parseLong(response.getFirstHeader("content-length").getValue()); Log.d(LCAT, "Setting length to " + contentLength); downloadRequest.setLength(contentLength); downloadInformation.setLength(contentLength); } // FileOutputStream outputStream = new FileOutputStream(file, true); outputStream.flush(); // Receive and store bytes long loopCount = 0; buffer = new byte[DOWNLOAD_BUFFER_SIZE]; bpsTrackingStart = new Date(); while (_downloadRequestUrlsThatMayProceed.containsKey(downloadRequest.getUrl()) == true && (byteCount = responseStream.read(buffer, 0, DOWNLOAD_BUFFER_SIZE)) > 0) { if (priority == DownloadPriority.Low) { Thread.yield(); } else if (priority == DownloadPriority.Normal && (loopCount % 4) == 0) { Thread.yield(); } ++loopCount; bytesForBps += byteCount; // Update status of downloadRequest if (downloadRequest.getDownloadStatus() == DownloadStatus.None) { downloadRequest.setDownloadStatus(DownloadStatus.InProgress); } if (downloadBatchRequest != null && downloadBatchRequest.getDownloadStatus() == DownloadStatus.None) { downloadBatchRequest.setDownloadStatus(DownloadStatus.InProgress); } // Store bytes outputStream.write(buffer, 0, byteCount); outputStream.flush(); // Update statistics downloadRequest.setAvailableLength(downloadRequest.getAvailableLength() + byteCount); downloadRequest.setLastWriteUtc(new Date()); downloadInformation.setLastWriteUtc(downloadRequest.getLastWriteUtc()); downloadInformation.setAvailableLength(downloadRequest.getAvailableLength()); // Track bits/second (every n iterations through loop) trackBpsIterationCount++; if (trackBpsIterationCount == TRACK_BPS_ON_ITERATION_NUMBER) { Date now = new Date(); bpsTrackingSpan = now.getTime() - bpsTrackingStart.getTime(); if (bpsTrackingSpan != 0) { downloadRequest.setLastDownloadBitsPerSecond( (int) ((8.0 * // bits per byte bytesForBps) / // bytes downloaded in timespan (bpsTrackingSpan / 1000.0))); // Seconds in timespan downloadInformation.setLastDownloadBitsPerSecond( downloadRequest.getLastDownloadBitsPerSecond()); } } // Fire progress event (every n iterations through loop) fireProgressEventCount++; if (fireProgressEventCount == FIRE_PROGRESS_EVENT_ON_ITERATION_NUMBER) { fireProgressEventCount = 0; DownloadProgress.fireEvent( new DownloadEvent(this, downloadInformation, downloadBatchInformation)); } // Reset tracking of bits/second (every n iterations through loop) if (trackBpsIterationCount == TRACK_BPS_ON_ITERATION_NUMBER) { trackBpsIterationCount = 0; if (bpsTrackingSpan != 0) { bytesForBps = 0; bpsTrackingStart = new Date(); } } } // Did the download complete? if (_downloadRequestUrlsThatMayProceed.containsKey(downloadRequest.getUrl()) == true) { // The download succeeded // Close file if (outputStream != null) { outputStream.close(); outputStream.flush(); outputStream = null; } // Remove download request from queue _downloadQueue.remove(downloadRequest.getUrl()); downloadRequest.setDownloadStatus( DownloadStatus .Complete); // This must come after removing the request from the queue to avoid // re-downloading // Update batch download status if (downloadBatchRequest != null) { boolean isInProgress = false; for (DownloadRequest dr : downloadBatchRequest.getDownloadRequests()) { if (dr.getDownloadStatus() == DownloadStatus.InProgress) { isInProgress = true; break; } } downloadBatchRequest.setDownloadStatus( isInProgress ? DownloadStatus.InProgress : DownloadStatus.None); } // Close response if (responseStream != null) { responseStream.close(); responseStream = null; } // Remove download tracking _downloadRequestUrlsThatMayProceed.remove(downloadRequest.getUrl()); // Cleanup batch request? if (downloadBatchRequest == null) { downloadBatchRequestIsComplete = false; } else { downloadBatchRequestIsComplete = _downloadQueue.downloadBatchRequestIsComplete( downloadBatchRequest.getDownloadBatchRequestId()); if (downloadBatchRequestIsComplete == true) { // Remove batch request from queue _downloadQueue.remove(downloadBatchRequest.getDownloadBatchRequestId()); // Synchronize synchronized (_downloadBatchInformationLock) { // Remove batch's download information (if any) if (_downloadBatchInformationById.containsKey( downloadBatchRequest.getDownloadBatchRequestId()) == true) { _downloadBatchInformationById.remove( downloadBatchRequest.getDownloadBatchRequestId()); } } } } // Create record for completed item if (downloadRequest.getAvailableLength() >= downloadRequest.getLength()) { _completedDownloadCatalog.addCompletedDownload(downloadInformation); } // Fire downloadInformation completed event DownloadCompleted.fireEvent(new DownloadEvent(this, downloadInformation, null)); // Batch completed? if (downloadBatchInformation != null && downloadBatchRequestIsComplete == true) { // Create record for completed batch _completedDownloadCatalog.addCompletedBatchDownload(downloadBatchInformation); // Fire batch completed event? DownloadBatchCompleted.fireEvent( new DownloadEvent(this, null, downloadBatchInformation)); } } else { // The download was cancelled or paused // Reset status of download request in download queue if (downloadRequest.getDownloadStatus() == DownloadStatus.InProgress) { downloadRequest.setDownloadStatus(DownloadStatus.None); } if (downloadBatchRequest != null) { boolean isInProgress = false; for (DownloadRequest dr : downloadBatchRequest.getDownloadRequests()) { if (dr.getDownloadStatus() == DownloadStatus.InProgress) { isInProgress = true; break; } } downloadBatchRequest.setDownloadStatus( isInProgress ? DownloadStatus.InProgress : DownloadStatus.None); } } } catch (Exception e) { Log.d(LCAT, "Download thread exception " + e.toString()); // Cancel the entire batch? if (downloadBatchRequest != null) { cancel(downloadBatchRequest.getDownloadBatchRequestId()); } // Fire download failed events DownloadFailed.fireEvent(new DownloadEvent(this, downloadInformation, null)); if (downloadBatchRequest != null) { DownloadBatchFailed.fireEvent(new DownloadEvent(this, null, downloadBatchInformation)); } } finally { // Close file if necessary if (fileStream != null) { fileStream.flush(); fileStream.close(); fileStream = null; } // Close response if necessary if (responseStream != null) { responseStream.close(); responseStream = null; } } } catch (Exception e) { // Cleanup Log.d(LCAT, "Download thread exception " + e.toString()); downloadRequest.setDownloadStatus(DownloadStatus.None); if (downloadBatchRequest != null) { boolean isInProgress = false; for (DownloadRequest dr : downloadBatchRequest.getDownloadRequests()) { if (dr.getDownloadStatus() == DownloadStatus.InProgress) { isInProgress = true; break; } } downloadBatchRequest.setDownloadStatus( isInProgress ? DownloadStatus.InProgress : DownloadStatus.None); } _downloadRequestUrlsThatMayProceed.remove(downloadRequest.getUrl()); // Cancel the entire batch? if (downloadBatchRequest != null) { cancel(downloadBatchRequest.getDownloadBatchRequestId()); } // Fire download failed events DownloadFailed.fireEvent(new DownloadEvent(this, downloadInformation, null)); if (downloadBatchRequest != null) { DownloadBatchFailed.fireEvent(new DownloadEvent(this, null, downloadBatchInformation)); } } }
public Builder addHeader(String name, String value) { if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) { mRequest.addHeader(name, value); } return this; }
/** * 新提交的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; }
@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(); }
private void resetAndValidateRequest(DownloadRequest request) { // Reset download request request.setLength(0); request.setAvailableLength(0); request.setDownloadStatus(DownloadStatus.None); // Validate download request if (request.getDownloadPriority() == DownloadPriority.None) { throw new IllegalArgumentException("request.DownloadPriority"); } if (request.getLocale() == null || request.getLocale().length() == 0) { throw new IllegalArgumentException("request.Locale"); } if (request.getName() == null || request.getName().length() == 0) { throw new IllegalArgumentException("request.Name"); } if (request.getOverridePermittedNetworkTypes() != null && request.getOverridePermittedNetworkTypes().contains(NetworkTypes.None)) { throw new IllegalArgumentException("request.OverridePermittedNetworkTypes"); } if (request.getUrl() == null || request.getUrl().length() == 0) { throw new IllegalArgumentException("request.Url"); } if (request.getFileName() == null || request.getFileName().length() == 0) { throw new IllegalArgumentException("request.FileName"); } if (_downloadQueue.getDownloadRequest(request.getUrl()) != null) { throw new IllegalArgumentException( "A download request already exists with the specified request's URL."); } }
/// <summary> /// Gets download progress information for the DownloadRequest by the specified URL. /// </summary> /// <param name="url"></param> /// <returns></returns> public DownloadInformation getDownloadInformation(String url) { Log.d(LCAT, "getDownloadInformation " + url); // Get download information DownloadInformation downloadInformation = _completedDownloadCatalog.getDownloadInformation(url); if (downloadInformation == null) { Log.d(LCAT, "NOT in completed catalog check queue"); // If no download information is available for a completed download, // create it from any matching queued download request DownloadRequest request = _downloadQueue.getDownloadRequest(url); if (request != null) { Log.d(LCAT, "Found in queue"); downloadInformation = new DownloadInformation(); downloadInformation.setUrl(request.getUrl()); downloadInformation.setName(request.getName()); downloadInformation.setLocale(request.getLocale()); downloadInformation.setFilePath(request.getFilePath()); downloadInformation.setLength(request.getLength()); downloadInformation.setMediaBitsPerSecond(request.getMediaBitsPerSecond()); downloadInformation.setAvailableLength(request.getAvailableLength()); downloadInformation.setCreationUtc(request.getCreationUtc()); downloadInformation.setLastWriteUtc(request.getLastWriteUtc()); downloadInformation.setLastDownloadBitsPerSecond(request.getLastDownloadBitsPerSecond()); downloadInformation.setDownloadPriority(request.getDownloadPriority()); downloadInformation.setIsReadyForPlayback(request.getIsReadyForPlayback()); downloadInformation.setPermittedNetworkTypes(request.getFinalPermittedNetworkTypes()); downloadInformation.setStorageLocation(request.getFinalStorageLocation()); } } // Return result return downloadInformation; }
public void run() { Feed savedFeed = null; Feed feed = new Feed(request.getSource(), new Date()); feed.setFile_url(request.getDestination()); feed.setDownloaded(true); reason = null; String reasonDetailed = null; successful = true; FeedHandler feedHandler = new FeedHandler(); try { feed = feedHandler.parseFeed(feed); if (AppConfig.DEBUG) Log.d(TAG, feed.getTitle() + " parsed"); if (checkFeedData(feed) == false) { throw new InvalidFeedException(); } // Save information of feed in DB savedFeed = DBTasks.updateFeed(DownloadService.this, feed); // Download Feed Image if provided and not downloaded if (savedFeed.getImage() != null && savedFeed.getImage().isDownloaded() == false) { if (AppConfig.DEBUG) Log.d(TAG, "Feed has image; Downloading...."); savedFeed.getImage().setFeed(savedFeed); final Feed savedFeedRef = savedFeed; try { requester.downloadImage(DownloadService.this, savedFeedRef.getImage()); } catch (DownloadRequestException e) { e.printStackTrace(); DBWriter.addDownloadStatus( DownloadService.this, new DownloadStatus( savedFeedRef.getImage(), savedFeedRef.getImage().getHumanReadableIdentifier(), DownloadError.ERROR_REQUEST_ERROR, false, e.getMessage())); } } } catch (SAXException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (IOException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (ParserConfigurationException e) { successful = false; e.printStackTrace(); reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } catch (UnsupportedFeedtypeException e) { e.printStackTrace(); successful = false; reason = DownloadError.ERROR_UNSUPPORTED_TYPE; reasonDetailed = e.getMessage(); } catch (InvalidFeedException e) { e.printStackTrace(); successful = false; reason = DownloadError.ERROR_PARSER_EXCEPTION; reasonDetailed = e.getMessage(); } // cleanup(); if (savedFeed == null) { savedFeed = feed; } saveDownloadStatus( new DownloadStatus( savedFeed, savedFeed.getHumanReadableIdentifier(), reason, successful, reasonDetailed)); sendDownloadHandledIntent(); numberOfDownloads.decrementAndGet(); queryDownloadsAsync(); }
/// <summary> /// Restarts download of the in-progress or completed item with the specified URL, beginning at // the specified byte offset. /// </summary> /// <param name="url"></param> /// <param name="byteOffset"></param> public void restart(String url, long byteOffset) { // Locals DownloadRequest downloadRequest; DownloadInformation downloadInformation; // Get download information downloadRequest = _downloadQueue.getDownloadRequest(url); if (downloadRequest == null) { downloadInformation = _completedDownloadCatalog.getDownloadInformation(url); if (downloadInformation == null) { return; } downloadRequest = new DownloadRequest(); downloadRequest.setUrl(downloadInformation.getUrl()); downloadRequest.setName(downloadInformation.getName()); downloadRequest.setLocale(downloadInformation.getLocale()); downloadRequest.setOverrideStorageLocation(downloadInformation.getStorageLocation()); downloadRequest.setDownloadPriority(downloadInformation.getDownloadPriority()); downloadRequest.setOverridePermittedNetworkTypes( downloadInformation.getPermittedNetworkTypes()); if (byteOffset == OFFSET_ZERO) { delete(downloadInformation.getUrl()); } else { downloadRequest.setAvailableLength(byteOffset); } } else { downloadRequest = new DownloadRequest(); downloadRequest.setUrl(downloadRequest.getUrl()); downloadRequest.setName(downloadRequest.getName()); downloadRequest.setLocale(downloadRequest.getLocale()); downloadRequest.setOverrideStorageLocation(downloadRequest.getOverrideStorageLocation()); downloadRequest.setDownloadPriority(downloadRequest.getDownloadPriority()); downloadRequest.setOverridePermittedNetworkTypes( downloadRequest.getOverridePermittedNetworkTypes()); } // Restart download information _downloadQueue.add(downloadRequest, _defaultStorageLocation, _permittedNetworkTypes); }
@Override public void run() { FeedMedia media = DBReader.getFeedMedia(DownloadService.this, request.getFeedfileId()); if (media == null) { throw new IllegalStateException("Could not find downloaded media object in database"); } boolean chaptersRead = false; media.setDownloaded(true); media.setFile_url(request.getDestination()); // Get duration MediaMetadataRetriever mmr = null; try { mmr = new MediaMetadataRetriever(); mmr.setDataSource(media.getFile_url()); String durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); media.setDuration(Integer.parseInt(durationStr)); if (AppConfig.DEBUG) Log.d(TAG, "Duration of file is " + media.getDuration()); } catch (NumberFormatException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } finally { if (mmr != null) { mmr.release(); } } if (media.getItem().getChapters() == null) { ChapterUtils.loadChaptersFromFileUrl(media); if (media.getItem().getChapters() != null) { chaptersRead = true; } } try { if (chaptersRead) { DBWriter.setFeedItem(DownloadService.this, media.getItem()).get(); } DBWriter.setFeedMedia(DownloadService.this, media).get(); if (!DBTasks.isInQueue(DownloadService.this, media.getItem().getId())) { DBWriter.addQueueItem(DownloadService.this, media.getItem().getId()).get(); } } catch (ExecutionException e) { e.printStackTrace(); status = new DownloadStatus( media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage()); } catch (InterruptedException e) { e.printStackTrace(); status = new DownloadStatus( media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage()); } saveDownloadStatus(status); sendDownloadHandledIntent(); numberOfDownloads.decrementAndGet(); queryDownloadsAsync(); }
/* Main method to lookup an entry */ public synchronized DownloadResponse getJnlpFile(JnlpResource jnlpres, DownloadRequest dreq) throws IOException { String path = jnlpres.getPath(); URL resource = jnlpres.getResource(); long lastModified = jnlpres.getLastModified(); _log.addDebug("lastModified: " + lastModified + " " + new Date(lastModified)); if (lastModified == 0) { _log.addWarning("servlet.log.warning.nolastmodified", path); } // fix for 4474854: use the request URL as key to look up jnlp file // in hash map String reqUrl = HttpUtils.getRequestURL(dreq.getHttpRequest()).toString(); // Check if entry already exist in HashMap JnlpFileEntry jnlpFile = (JnlpFileEntry) _jnlpFiles.get(reqUrl); if (jnlpFile != null && jnlpFile.getLastModified() == lastModified) { // Entry found in cache, so return it return jnlpFile.getResponse(); } // Read information from WAR file long timeStamp = lastModified; String mimeType = _servletContext.getMimeType(path); if (mimeType == null) mimeType = JNLP_MIME_TYPE; StringBuilder jnlpFileTemplate = new StringBuilder(); URLConnection conn = resource.openConnection(); final InputStream is = conn.getInputStream(); final InputStreamReader reader = new InputStreamReader(is); char[] chars = new char[1024]; int read; while ((read = reader.read(chars)) != -1) { jnlpFileTemplate.append(chars, 0, read); } try { is.close(); } catch (IOException e) { // should never happen and if it does, we're not handling this. It should be save to ignore } String jnlpFileContent = specializeJnlpTemplate(dreq.getHttpRequest(), path, jnlpFileTemplate.toString()); // Convert to bytes as a UTF-8 encoding byte[] byteContent = jnlpFileContent.getBytes("UTF-8"); // Create entry DownloadResponse resp = DownloadResponse.getFileDownloadResponse( byteContent, mimeType, timeStamp, jnlpres.getReturnVersionId()); jnlpFile = new JnlpFileEntry(resp, lastModified); _jnlpFiles.put(reqUrl, jnlpFile); return resp; }