/** * 下载一个文件 * * @param dirType * @param subPath * @param url * @param isOverite 复盖原有文件,删除原来的链接 * @return */ public long downLoadFile( String dirType, String subPath, final String url, boolean isExternal, boolean isOverite) { if (isOverite) { // 如果存在就先删除 final File dirtype = context.getExternalFilesDir(dirType); Uri.Builder builder = Uri.fromFile(dirtype).buildUpon(); builder = builder.appendEncodedPath(subPath); File file = new File(builder.build().getPath()); if (file.exists()) { file.delete(); } // 判断是否在下载列表中,有在下载列表中就先删除 List<DownloadStatus> downloadStatusList = getAllDownloadList(); for (DownloadStatus downloadStatus : downloadStatusList) { if (downloadStatus.getUrl().equals(url)) { deleteDownload(downloadStatus.getDownloadId()); } } return downLoadFile(dirType, subPath, url, isExternal); } else { return downLoadFile(dirType, subPath, url, isExternal); } }
private void setMessageStatus(Cursor cursor, Context context, StringBuilder messageDetails) { int ind = cursor.getColumnIndex(Msg.MSG_STATUS); if (ind >= 0) { DownloadStatus status = DownloadStatus.load(cursor.getLong(ind)); if (status != DownloadStatus.LOADED) { messageDetails.append(" (").append(status.getTitle(context)).append(")"); } } }
private void scheduleMoreBlockRequests() { DownloadStatus status = downloadStatus.get(); while (status.queueSize < MAX_BLOCK_QUEUE_SIZE && status.lastRequestedByte < status.piece.getLength() - 1) { requestBlock( status.piece.getIndex(), status.lastRequestedByte + 1, Math.min(BLOCK_SIZE, status.piece.getLength() - (status.lastRequestedByte + 1))); status.lastRequestedByte += BLOCK_SIZE; status.queueSize++; } }
/** * Creates a notification at the end of the service lifecycle to notify the user about the number * of completed downloads. A report will only be created if the number of successfully downloaded * feeds is bigger than 1 or if there is at least one failed download which is not an image or if * there is at least one downloaded media file. */ private void updateReport() { // check if report should be created boolean createReport = false; int successfulDownloads = 0; int failedDownloads = 0; // a download report is created if at least one download has failed // (excluding failed image downloads) for (DownloadStatus status : completedDownloads) { if (status.isSuccessful()) { successfulDownloads++; } else if (!status.isCancelled()) { if (status.getFeedfileType() != FeedImage.FEEDFILETYPE_FEEDIMAGE) { createReport = true; } failedDownloads++; } } if (createReport) { if (BuildConfig.DEBUG) Log.d(TAG, "Creating report"); Intent intent = new Intent(this, MainActivity.class); intent.putExtra(MainActivity.EXTRA_NAV_TYPE, NavListAdapter.VIEW_TYPE_NAV); intent.putExtra(MainActivity.EXTRA_NAV_INDEX, MainActivity.POS_DOWNLOADS); Bundle args = new Bundle(); args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG); intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args); // create notification object Notification notification = new NotificationCompat.Builder(this) .setTicker(getString(de.danoeh.antennapod.R.string.download_report_title)) .setContentTitle(getString(de.danoeh.antennapod.R.string.download_report_title)) .setContentText( String.format( getString(R.string.download_report_content), successfulDownloads, failedDownloads)) .setSmallIcon(R.drawable.stat_notify_sync) .setLargeIcon( BitmapFactory.decodeResource(getResources(), R.drawable.stat_notify_sync)) .setContentIntent( PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)) .setAutoCancel(true) .build(); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(REPORT_ID, notification); } else { if (BuildConfig.DEBUG) Log.d(TAG, "No report is created"); } completedDownloads.clear(); }
@Override public void read(org.apache.thrift.protocol.TProtocol prot, OnlineStatus struct) throws org.apache.thrift.TException { TTupleProtocol iprot = (TTupleProtocol) prot; BitSet incoming = iprot.readBitSet(5); if (incoming.get(0)) { struct.name = iprot.readString(); struct.setNameIsSet(true); } if (incoming.get(1)) { struct.plugin = iprot.readString(); struct.setPluginIsSet(true); } if (incoming.get(2)) { struct.packagename = iprot.readString(); struct.setPackagenameIsSet(true); } if (incoming.get(3)) { struct.status = DownloadStatus.findByValue(iprot.readI32()); struct.setStatusIsSet(true); } if (incoming.get(4)) { struct.size = iprot.readI64(); struct.setSizeIsSet(true); } }
public void read(org.apache.thrift.protocol.TProtocol iprot, OnlineStatus struct) throws org.apache.thrift.TException { org.apache.thrift.protocol.TField schemeField; iprot.readStructBegin(); while (true) { schemeField = iprot.readFieldBegin(); if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { break; } switch (schemeField.id) { case 1: // NAME if (schemeField.type == org.apache.thrift.protocol.TType.STRING) { struct.name = iprot.readString(); struct.setNameIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; case 2: // PLUGIN if (schemeField.type == org.apache.thrift.protocol.TType.STRING) { struct.plugin = iprot.readString(); struct.setPluginIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; case 3: // PACKAGENAME if (schemeField.type == org.apache.thrift.protocol.TType.STRING) { struct.packagename = iprot.readString(); struct.setPackagenameIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; case 4: // STATUS if (schemeField.type == org.apache.thrift.protocol.TType.I32) { struct.status = DownloadStatus.findByValue(iprot.readI32()); struct.setStatusIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; case 5: // SIZE if (schemeField.type == org.apache.thrift.protocol.TType.I64) { struct.size = iprot.readI64(); struct.setSizeIsSet(true); } else { org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } break; default: org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method struct.validate(); }
/** * Creates a notification at the end of the service lifecycle to notify the user about the number * of completed downloads. A report will only be created if the number of successfully downloaded * feeds is bigger than 1 or if there is at least one failed download which is not an image or if * there is at least one downloaded media file. */ private void updateReport() { // check if report should be created boolean createReport = false; int successfulDownloads = 0; int failedDownloads = 0; // a download report is created if at least one download has failed // (excluding failed image downloads) for (DownloadStatus status : completedDownloads) { if (status.isSuccessful()) { successfulDownloads++; } else if (!status.isCancelled()) { if (status.getFeedfileType() != FeedImage.FEEDFILETYPE_FEEDIMAGE) { createReport = true; } failedDownloads++; } } if (createReport) { if (AppConfig.DEBUG) Log.d(TAG, "Creating report"); // create notification object Notification notification = new NotificationCompat.Builder(this) .setTicker(getString(de.danoeh.antennapod.R.string.download_report_title)) .setContentTitle(getString(de.danoeh.antennapod.R.string.download_report_title)) .setContentText( String.format( getString(R.string.download_report_content), successfulDownloads, failedDownloads)) .setSmallIcon(R.drawable.stat_notify_sync) .setLargeIcon( BitmapFactory.decodeResource(getResources(), R.drawable.stat_notify_sync)) .setContentIntent( PendingIntent.getActivity( this, 0, new Intent(this, DownloadLogActivity.class), 0)) .setAutoCancel(true) .getNotification(); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(REPORT_ID, notification); } else { if (AppConfig.DEBUG) Log.d(TAG, "No report is created"); } completedDownloads.clear(); }
@Override public void run() { if (AppConfig.DEBUG) Log.d(TAG, "downloadCompletionThread was started"); while (!isInterrupted()) { try { Downloader downloader = downloadExecutor.take().get(); if (AppConfig.DEBUG) Log.d(TAG, "Received 'Download Complete' - message."); removeDownload(downloader); DownloadStatus status = downloader.getResult(); boolean successful = status.isSuccessful(); final int type = status.getFeedfileType(); if (successful) { if (type == Feed.FEEDFILETYPE_FEED) { handleCompletedFeedDownload(downloader.getDownloadRequest()); } else if (type == FeedImage.FEEDFILETYPE_FEEDIMAGE) { handleCompletedImageDownload(status, downloader.getDownloadRequest()); } else if (type == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { handleCompletedFeedMediaDownload(status, downloader.getDownloadRequest()); } } else { numberOfDownloads.decrementAndGet(); if (!successful && !status.isCancelled()) { Log.e(TAG, "Download failed"); saveDownloadStatus(status); } sendDownloadHandledIntent(); queryDownloadsAsync(); } } catch (InterruptedException e) { if (AppConfig.DEBUG) Log.d(TAG, "DownloadCompletionThread was interrupted"); } catch (ExecutionException e) { e.printStackTrace(); numberOfDownloads.decrementAndGet(); } } if (AppConfig.DEBUG) Log.d(TAG, "End of downloadCompletionThread"); }
/** * 获得全部的下载列表 * * @return */ public List<DownloadStatus> getAllDownloadList() { List<DownloadStatus> downloadStatusList = new ArrayList<>(); DownloadManager.Query query = new DownloadManager.Query(); Cursor cursor = downloadManager.query(query); while (cursor.moveToNext()) { long time = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)); // Log.d(TAG, "time:" + System.currentTimeMillis()); // Log.d(TAG, "time:" + time); int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON)); int downId = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_ID)); int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)); String url = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI)); // 下载的url int progress = (int) ((float) bytes_downloaded / (float) bytes_total * 100); Log.d( TAG, bytes_downloaded + "/" + bytes_total + "--" + progress + "uri" + downloadManager.getUriForDownloadedFile(downId)); DownloadStatus downloadStatus = new DownloadStatus(); downloadStatus.setDownloadId(downId); downloadStatus.setDownloadedbyte(bytes_downloaded); downloadStatus.setStatus(status); downloadStatus.setReason(reason); downloadStatus.setDownloadtotalbyte(bytes_total); downloadStatus.setFileUri(downloadManager.getUriForDownloadedFile(downId)); downloadStatus.setProgress(progress); downloadStatus.setUrl(url); downloadStatusList.add(downloadStatus); } cursor.close(); return downloadStatusList; }
@Override public void run() { if (BuildConfig.DEBUG) Log.d(TAG, "downloadCompletionThread was started"); while (!isInterrupted()) { try { Downloader downloader = downloadExecutor.take().get(); if (BuildConfig.DEBUG) Log.d(TAG, "Received 'Download Complete' - message."); removeDownload(downloader); DownloadStatus status = downloader.getResult(); boolean successful = status.isSuccessful(); final int type = status.getFeedfileType(); if (successful) { if (type == Feed.FEEDFILETYPE_FEED) { handleCompletedFeedDownload(downloader.getDownloadRequest()); } else if (type == FeedImage.FEEDFILETYPE_FEEDIMAGE) { handleCompletedImageDownload(status, downloader.getDownloadRequest()); } else if (type == FeedMedia.FEEDFILETYPE_FEEDMEDIA) { handleCompletedFeedMediaDownload(status, downloader.getDownloadRequest()); } } else { numberOfDownloads.decrementAndGet(); if (!status.isCancelled()) { if (status.getReason() == DownloadError.ERROR_UNAUTHORIZED) { postAuthenticationNotification(downloader.getDownloadRequest()); } else if (status.getReason() == DownloadError.ERROR_HTTP_DATA_ERROR && Integer.valueOf(status.getReasonDetailed()) == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) { Log.d(TAG, "Requested invalid range, restarting download from the beginning"); FileUtils.deleteQuietly( new File(downloader.getDownloadRequest().getDestination())); DownloadRequester.getInstance() .download(DownloadService.this, downloader.getDownloadRequest()); } else { Log.e(TAG, "Download failed"); saveDownloadStatus(status); handleFailedDownload(status, downloader.getDownloadRequest()); } } sendDownloadHandledIntent(); queryDownloadsAsync(); } } catch (InterruptedException e) { if (BuildConfig.DEBUG) Log.d(TAG, "DownloadCompletionThread was interrupted"); } catch (ExecutionException e) { e.printStackTrace(); numberOfDownloads.decrementAndGet(); } } if (BuildConfig.DEBUG) Log.d(TAG, "End of downloadCompletionThread"); }