diff options
Diffstat (limited to 'src/de/danoeh/antennapod/service')
5 files changed, 484 insertions, 182 deletions
diff --git a/src/de/danoeh/antennapod/service/download/DownloadRequest.java b/src/de/danoeh/antennapod/service/download/DownloadRequest.java new file mode 100644 index 000000000..1f4e32e1b --- /dev/null +++ b/src/de/danoeh/antennapod/service/download/DownloadRequest.java @@ -0,0 +1,177 @@ +package de.danoeh.antennapod.service.download; + +import android.os.Parcel; +import android.os.Parcelable; + +public class DownloadRequest implements Parcelable { + + private final String destination; + private final String source; + private final String title; + private final long feedfileId; + private final int feedfileType; + + protected int progressPercent; + protected long soFar; + protected long size; + protected int statusMsg; + + public DownloadRequest(String destination, String source, String title, + long feedfileId, int feedfileType) { + if (destination == null) { + throw new IllegalArgumentException("Destination must not be null"); + } + if (source == null) { + throw new IllegalArgumentException("Source must not be null"); + } + if (title == null) { + throw new IllegalArgumentException("Title must not be null"); + } + + this.destination = destination; + this.source = source; + this.title = title; + this.feedfileId = feedfileId; + this.feedfileType = feedfileType; + } + + private DownloadRequest(Parcel in) { + destination = in.readString(); + source = in.readString(); + title = in.readString(); + feedfileId = in.readLong(); + feedfileType = in.readInt(); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeString(destination); + dest.writeString(source); + dest.writeString(title); + dest.writeLong(feedfileId); + dest.writeInt(feedfileType); + } + + public static final Parcelable.Creator<DownloadRequest> CREATOR = new Parcelable.Creator<DownloadRequest>() { + public DownloadRequest createFromParcel(Parcel in) { + return new DownloadRequest(in); + } + + public DownloadRequest[] newArray(int size) { + return new DownloadRequest[size]; + } + }; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((destination == null) ? 0 : destination.hashCode()); + result = prime * result + (int) (feedfileId ^ (feedfileId >>> 32)); + result = prime * result + feedfileType; + result = prime * result + progressPercent; + result = prime * result + (int) (size ^ (size >>> 32)); + result = prime * result + (int) (soFar ^ (soFar >>> 32)); + result = prime * result + ((source == null) ? 0 : source.hashCode()); + result = prime * result + statusMsg; + result = prime * result + ((title == null) ? 0 : title.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DownloadRequest other = (DownloadRequest) obj; + if (destination == null) { + if (other.destination != null) + return false; + } else if (!destination.equals(other.destination)) + return false; + if (feedfileId != other.feedfileId) + return false; + if (feedfileType != other.feedfileType) + return false; + if (progressPercent != other.progressPercent) + return false; + if (size != other.size) + return false; + if (soFar != other.soFar) + return false; + if (source == null) { + if (other.source != null) + return false; + } else if (!source.equals(other.source)) + return false; + if (statusMsg != other.statusMsg) + return false; + if (title == null) { + if (other.title != null) + return false; + } else if (!title.equals(other.title)) + return false; + return true; + } + + public String getDestination() { + return destination; + } + + public String getSource() { + return source; + } + + public String getTitle() { + return title; + } + + public long getFeedfileId() { + return feedfileId; + } + + public int getFeedfileType() { + return feedfileType; + } + + public int getProgressPercent() { + return progressPercent; + } + + public void setProgressPercent(int progressPercent) { + this.progressPercent = progressPercent; + } + + public long getSoFar() { + return soFar; + } + + public void setSoFar(long soFar) { + this.soFar = soFar; + } + + public long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public int getStatusMsg() { + return statusMsg; + } + + public void setStatusMsg(int statusMsg) { + this.statusMsg = statusMsg; + } +} diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index e1230e170..fe327db59 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -41,8 +41,6 @@ import android.os.AsyncTask; import android.os.Binder; import android.os.Handler; import android.os.IBinder; -import android.os.Parcel; -import android.os.Parcelable; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.webkit.URLUtil; @@ -50,10 +48,8 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.DownloadActivity; import de.danoeh.antennapod.activity.DownloadLogActivity; -import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.feed.EventDistributor; import de.danoeh.antennapod.feed.Feed; -import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedItem; import de.danoeh.antennapod.feed.FeedManager; @@ -257,30 +253,26 @@ public class DownloadService extends Service { StringBuilder bigText = new StringBuilder(""); for (int i = 0; i < downloads.size(); i++) { Downloader downloader = downloads.get(i); - if (downloader.getStatus() != null) { - FeedFile f = downloader.getStatus().getFeedFile(); - if (f.getClass() == Feed.class) { - Feed feed = (Feed) f; - if (feed.getTitle() != null) { - if (i > 0) { - bigText.append("\n"); - } - bigText.append("\u2022 " + feed.getTitle()); + final DownloadRequest request = downloader + .getDownloadRequest(); + if (request.getFeedfileType() == Feed.FEEDFILETYPE_FEED) { + if (request.getTitle() != null) { + if (i > 0) { + bigText.append("\n"); } - } else if (f.getClass() == FeedMedia.class) { - FeedMedia media = (FeedMedia) f; - if (media.getItem().getTitle() != null) { - if (i > 0) { - bigText.append("\n"); - } - bigText.append("\u2022 " - + media.getItem().getTitle() - + " (" - + downloader.getStatus() - .getProgressPercent() + "%)"); + 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); @@ -301,8 +293,7 @@ public class DownloadService extends Service { private Downloader getDownloader(String downloadUrl) { for (Downloader downloader : downloads) { - if (downloader.getStatus().getFeedFile().getDownload_url() - .equals(downloadUrl)) { + if (downloader.getDownloadRequest().getSource().equals(downloadUrl)) { return downloader; } } @@ -333,8 +324,7 @@ public class DownloadService extends Service { for (Downloader d : downloads) { d.cancel(); DownloadRequester.getInstance().removeDownload( - d.getStatus().getFeedFile()); - d.getStatus().getFeedFile().setFile_url(null); + d.getDownloadRequest()); if (AppConfig.DEBUG) Log.d(TAG, "Cancelled all downloads"); } @@ -350,7 +340,7 @@ public class DownloadService extends Service { private void onDownloadQueued(Intent intent) { if (AppConfig.DEBUG) Log.d(TAG, "Received enqueue request"); - Request request = intent.getParcelableExtra(EXTRA_REQUEST); + DownloadRequest request = intent.getParcelableExtra(EXTRA_REQUEST); if (request == null) { throw new IllegalArgumentException( "ACTION_ENQUEUE_DOWNLOAD intent needs request extra"); @@ -361,22 +351,13 @@ public class DownloadService extends Service { shutdownInitiated = false; } - DownloadRequester requester = DownloadRequester.getInstance(); - FeedFile feedfile = requester.getDownload(request.source); - if (feedfile != null) { - - DownloadStatus status = new DownloadStatus(feedfile, - feedfile.getHumanReadableIdentifier()); - Downloader downloader = getDownloader(status); - if (downloader != null) { - downloads.add(downloader); - downloadExecutor.submit(downloader); - sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); - } - } else { - Log.e(TAG, - "Could not find feedfile in download requester when trying to enqueue new download"); + Downloader downloader = getDownloader(request); + if (downloader != null) { + downloads.add(downloader); + downloadExecutor.submit(downloader); + sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); } + queryDownloads(); } @@ -389,8 +370,8 @@ public class DownloadService extends Service { }; - private Downloader getDownloader(DownloadStatus status) { - if (URLUtil.isHttpUrl(status.getFeedFile().getDownload_url())) { + private Downloader getDownloader(DownloadRequest request) { + if (URLUtil.isHttpUrl(request.getSource())) { return new HttpDownloader(new DownloaderCallback() { @Override @@ -404,10 +385,11 @@ public class DownloadService extends Service { } }); } - }, status); + }, request); } - Log.e(TAG, "Could not find appropriate downloader for " - + status.getFeedFile().getDownload_url()); + Log.e(TAG, + "Could not find appropriate downloader for " + + request.getSource()); return null; } @@ -435,31 +417,28 @@ public class DownloadService extends Service { if (AppConfig.DEBUG) Log.d(TAG, "Received 'Download Complete' - message."); downloadsBeingHandled += 1; - DownloadStatus status = downloader.getStatus(); - status.setCompletionDate(new Date()); + DownloadStatus status = downloader.getResult(); successful = status.isSuccessful(); - FeedFile download = status.getFeedFile(); - if (download != null) { - if (successful) { - if (download.getClass() == Feed.class) { - handleCompletedFeedDownload(status); - } else if (download.getClass() == FeedImage.class) { - handleCompletedImageDownload(status); - } else if (download.getClass() == FeedMedia.class) { - handleCompletedFeedMediaDownload(status); - } - } else { - download.setFile_url(null); - download.setDownloaded(false); - if (!successful && !status.isCancelled()) { - Log.e(TAG, "Download failed"); - saveDownloadStatus(status); - } - sendDownloadHandledIntent(); - downloadsBeingHandled -= 1; + 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 { + if (!successful && !status.isCancelled()) { + Log.e(TAG, "Download failed"); + saveDownloadStatus(status); + } + sendDownloadHandledIntent(); + downloadsBeingHandled -= 1; } + return null; } }; @@ -477,12 +456,11 @@ public class DownloadService extends Service { private void removeDownload(final Downloader d) { if (AppConfig.DEBUG) Log.d(TAG, "Removing downloader: " - + d.getStatus().getFeedFile().getDownload_url()); + + d.getDownloadRequest().getSource()); boolean rc = downloads.remove(d); if (AppConfig.DEBUG) Log.d(TAG, "Result of downloads.remove: " + rc); - DownloadRequester.getInstance().removeDownload( - d.getStatus().getFeedFile()); + DownloadRequester.getInstance().removeDownload(d.getDownloadRequest()); sendBroadcast(new Intent(ACTION_DOWNLOADS_CONTENT_CHANGED)); } @@ -521,7 +499,7 @@ public class DownloadService extends Service { if (status.isSuccessful()) { successfulDownloads++; } else if (!status.isCancelled()) { - if (status.getFeedFile().getClass() != FeedImage.class) { + if (status.getFeedfileType() != FeedImage.FEEDFILETYPE_FEEDIMAGE) { createReport = true; } failedDownloads++; @@ -581,25 +559,25 @@ public class DownloadService extends Service { } /** Is called whenever a Feed is downloaded */ - private void handleCompletedFeedDownload(DownloadStatus status) { + private void handleCompletedFeedDownload(DownloadRequest request) { if (AppConfig.DEBUG) Log.d(TAG, "Handling completed Feed Download"); - syncExecutor.execute(new FeedSyncThread(status)); + syncExecutor.execute(new FeedSyncThread(request)); } /** Is called whenever a Feed-Image is downloaded */ - private void handleCompletedImageDownload(DownloadStatus status) { + private void handleCompletedImageDownload(DownloadStatus status, DownloadRequest request) { if (AppConfig.DEBUG) Log.d(TAG, "Handling completed Image Download"); - syncExecutor.execute(new ImageHandlerThread(status)); + syncExecutor.execute(new ImageHandlerThread(status, request)); } /** Is called whenever a FeedMedia is downloaded. */ - private void handleCompletedFeedMediaDownload(DownloadStatus status) { + private void handleCompletedFeedMediaDownload(DownloadStatus status, DownloadRequest request) { if (AppConfig.DEBUG) Log.d(TAG, "Handling completed FeedMedia Download"); - syncExecutor.execute(new MediaHandlerThread(status)); + syncExecutor.execute(new MediaHandlerThread(status, request)); } /** @@ -609,25 +587,31 @@ public class DownloadService extends Service { class FeedSyncThread implements Runnable { private static final String TAG = "FeedSyncThread"; - private Feed feed; - private DownloadStatus status; + private DownloadRequest request; private int reason; private boolean successful; - public FeedSyncThread(DownloadStatus status) { - this.feed = (Feed) status.getFeedFile(); - this.status = status; + public FeedSyncThread(DownloadRequest request) { + if (request == null) { + throw new IllegalArgumentException("Request must not be null"); + } + + this.request = request; } public void run() { Feed savedFeed = null; + + Feed feed = new Feed(request.getSource(), new Date()); + feed.setFile_url(request.getDestination()); + feed.setDownloaded(true); + reason = 0; String reasonDetailed = null; successful = true; final FeedManager manager = FeedManager.getInstance(); FeedHandler feedHandler = new FeedHandler(); - feed.setDownloaded(true); try { feed = feedHandler.parseFeed(feed); @@ -751,7 +735,7 @@ public class DownloadService extends Service { } /** Delete files that aren't needed anymore */ - private void cleanup() { + private void cleanup(Feed feed) { if (feed.getFile_url() != null) { if (new File(feed.getFile_url()).delete()) if (AppConfig.DEBUG) @@ -768,16 +752,29 @@ public class DownloadService extends Service { /** Handles a completed image download. */ class ImageHandlerThread implements Runnable { - private FeedImage image; + + private DownloadRequest request; private DownloadStatus status; - public ImageHandlerThread(DownloadStatus status) { - this.image = (FeedImage) status.getFeedFile(); + public ImageHandlerThread(DownloadStatus status, DownloadRequest request) { + if (status == null) { + throw new IllegalArgumentException("Status must not be null"); + } + if (request == null) { + throw new IllegalArgumentException("Request must not be null"); + } this.status = status; + this.request = request; } @Override public void run() { + FeedImage image = FeedManager.getInstance().getFeedImage(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); @@ -803,20 +800,34 @@ public class DownloadService extends Service { /** Handles a completed media download. */ class MediaHandlerThread implements Runnable { - private FeedMedia media; - private DownloadStatus status; - public MediaHandlerThread(DownloadStatus status) { - super(); - this.media = (FeedMedia) status.getFeedFile(); + private DownloadRequest request; + private DownloadStatus status; + + public MediaHandlerThread(DownloadStatus status, DownloadRequest request) { + if (status == null) { + throw new IllegalArgumentException("Status must not be null"); + } + if (request == null) { + throw new IllegalArgumentException("Request must not be null"); + } + this.status = status; + this.request = request; } @Override public void run() { + FeedMedia media = FeedManager.getInstance().getFeedMedia( + 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 MediaPlayer mediaplayer = new MediaPlayer(); try { @@ -863,53 +874,6 @@ public class DownloadService extends Service { } } - /** Is used to request a new download. */ - public static class Request implements Parcelable { - private String destination; - private String source; - - public Request(String destination, String source) { - super(); - this.destination = destination; - this.source = source; - } - - private Request(Parcel in) { - destination = in.readString(); - source = in.readString(); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeString(destination); - dest.writeString(source); - } - - public static final Parcelable.Creator<Request> CREATOR = new Parcelable.Creator<Request>() { - public Request createFromParcel(Parcel in) { - return new Request(in); - } - - public Request[] newArray(int size) { - return new Request[size]; - } - }; - - public String getDestination() { - return destination; - } - - public String getSource() { - return source; - } - - } - /** Schedules the notification updater task if it hasn't been scheduled yet. */ private void setupNotificationUpdater() { if (AppConfig.DEBUG) diff --git a/src/de/danoeh/antennapod/service/download/DownloadStatus.java b/src/de/danoeh/antennapod/service/download/DownloadStatus.java new file mode 100644 index 000000000..76091ec67 --- /dev/null +++ b/src/de/danoeh/antennapod/service/download/DownloadStatus.java @@ -0,0 +1,158 @@ +package de.danoeh.antennapod.service.download; + +import java.util.Date; + +import de.danoeh.antennapod.feed.FeedFile; + +/** Contains status attributes for one download */ +public class DownloadStatus { + /** + * Downloaders should use this constant for the size attribute if necessary + * so that the listadapters etc. can react properly. + */ + public static final int SIZE_UNKNOWN = -1; + + // ----------------------------------- ATTRIBUTES STORED IN DB + /** Unique id for storing the object in database. */ + protected long id; + /** + * A human-readable string which is shown to the user so that he can + * identify the download. Should be the title of the item/feed/media or the + * URL if the download has no other title. + */ + protected String title; + protected int reason; + /** + * A message which can be presented to the user to give more information. + * Should be null if Download was successful. + */ + protected String reasonDetailed; + protected boolean successful; + protected Date completionDate; + protected long feedfileId; + /** + * Is used to determine the type of the feedfile even if the feedfile does + * not exist anymore. The value should be FEEDFILETYPE_FEED, + * FEEDFILETYPE_FEEDIMAGE or FEEDFILETYPE_FEEDMEDIA + */ + protected int feedfileType; + + // ------------------------------------ NOT STORED IN DB + protected boolean done; + protected boolean cancelled; + + /** Constructor for restoring Download status entries from DB. */ + public DownloadStatus(long id, String title, long feedfileId, + int feedfileType, boolean successful, int reason, + Date completionDate, String reasonDetailed) { + this.id = id; + this.title = title; + this.done = true; + this.feedfileId = feedfileId; + this.reason = reason; + this.successful = successful; + this.completionDate = completionDate; + this.reasonDetailed = reasonDetailed; + this.feedfileType = feedfileType; + } + + public DownloadStatus(DownloadRequest request, int reason, + boolean successful, boolean cancelled, String reasonDetailed) { + if (request == null) { + throw new IllegalArgumentException("request must not be null"); + } + this.title = request.getTitle(); + this.feedfileId = request.getFeedfileId(); + this.feedfileType = request.getFeedfileType(); + this.reason = reason; + this.successful = successful; + this.cancelled = cancelled; + this.reasonDetailed = reasonDetailed; + this.completionDate = new Date(); + } + + /** Constructor for creating new completed downloads. */ + public DownloadStatus(FeedFile feedfile, String title, int reason, + boolean successful, String reasonDetailed) { + if (feedfile == null) { + throw new IllegalArgumentException("feedfile must not be null"); + } + + this.title = title; + this.done = true; + this.feedfileId = feedfile.getId(); + this.feedfileType = feedfile.getTypeAsInt(); + this.reason = reason; + this.successful = successful; + this.completionDate = new Date(); + this.reasonDetailed = reasonDetailed; + } + + /** Constructor for creating new completed downloads. */ + public DownloadStatus(long feedfileId, int feedfileType, String title, + int reason, boolean successful, String reasonDetailed) { + this.title = title; + this.done = true; + this.feedfileId = feedfileId; + this.feedfileType = feedfileType; + this.reason = reason; + this.successful = successful; + this.completionDate = new Date(); + this.reasonDetailed = reasonDetailed; + } + + @Override + public String toString() { + return "DownloadStatus [id=" + id + ", title=" + title + ", reason=" + + reason + ", reasonDetailed=" + reasonDetailed + + ", successful=" + successful + ", completionDate=" + + completionDate + ", feedfileId=" + feedfileId + + ", feedfileType=" + feedfileType + ", done=" + done + + ", cancelled=" + cancelled + "]"; + } + + public long getId() { + return id; + } + + public String getTitle() { + return title; + } + + public int getReason() { + return reason; + } + + public String getReasonDetailed() { + return reasonDetailed; + } + + public boolean isSuccessful() { + return successful; + } + + public Date getCompletionDate() { + return completionDate; + } + + public long getFeedfileId() { + return feedfileId; + } + + public int getFeedfileType() { + return feedfileType; + } + + public boolean isDone() { + return done; + } + + public boolean isCancelled() { + return cancelled; + } + + public void setId(long id) { + this.id = id; + } + +}
\ No newline at end of file diff --git a/src/de/danoeh/antennapod/service/download/Downloader.java b/src/de/danoeh/antennapod/service/download/Downloader.java index 9ed9d9a76..8df2bf977 100644 --- a/src/de/danoeh/antennapod/service/download/Downloader.java +++ b/src/de/danoeh/antennapod/service/download/Downloader.java @@ -1,7 +1,6 @@ package de.danoeh.antennapod.service.download; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.asynctask.DownloadStatus; /** Downloads files */ public abstract class Downloader extends Thread { @@ -12,14 +11,15 @@ public abstract class Downloader extends Thread { protected volatile boolean cancelled; - protected volatile DownloadStatus status; + protected volatile DownloadRequest request; + protected volatile DownloadStatus result; public Downloader(DownloaderCallback downloaderCallback, - DownloadStatus status) { + DownloadRequest request) { super(); this.downloaderCallback = downloaderCallback; - this.status = status; - this.status.setStatusMsg(R.string.download_pending); + this.request = request; + this.request.setStatusMsg(R.string.download_pending); this.cancelled = false; } @@ -39,11 +39,23 @@ public abstract class Downloader extends Thread { @Override public final void run() { download(); + if (result == null) { + throw new IllegalStateException( + "Downloader hasn't created DownloadStatus object"); + } finish(); } - public DownloadStatus getStatus() { - return status; + public DownloadRequest getDownloadRequest() { + return request; + } + + public DownloadStatus getResult() { + return result; + } + + public boolean isFinished() { + return finished; } public void cancel() { diff --git a/src/de/danoeh/antennapod/service/download/HttpDownloader.java b/src/de/danoeh/antennapod/service/download/HttpDownloader.java index f8f26f6fd..77443956b 100644 --- a/src/de/danoeh/antennapod/service/download/HttpDownloader.java +++ b/src/de/danoeh/antennapod/service/download/HttpDownloader.java @@ -26,7 +26,6 @@ import android.util.Log; import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.StorageUtils; @@ -40,8 +39,8 @@ public class HttpDownloader extends Downloader { private static final int SOCKET_TIMEOUT = 30000; public HttpDownloader(DownloaderCallback downloaderCallback, - DownloadStatus status) { - super(downloaderCallback, status); + DownloadRequest request) { + super(downloaderCallback, request); } private DefaultHttpClient createHttpClient() { @@ -66,8 +65,7 @@ public class HttpDownloader extends Downloader { OutputStream out = null; InputStream connection = null; try { - HttpGet httpGet = new HttpGet(status.getFeedFile() - .getDownload_url()); + HttpGet httpGet = new HttpGet(request.getSource()); httpClient = createHttpClient(); HttpResponse response = httpClient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); @@ -76,8 +74,7 @@ public class HttpDownloader extends Downloader { Log.d(TAG, "Response code is " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK && httpEntity != null) { if (StorageUtils.storageAvailable(PodcastApp.getInstance())) { - File destination = new File(status.getFeedFile() - .getFile_url()); + File destination = new File(request.getDestination()); if (!destination.exists()) { connection = AndroidHttpClient .getUngzippedContent(httpEntity); @@ -86,29 +83,30 @@ public class HttpDownloader extends Downloader { destination)); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; - status.setStatusMsg(R.string.download_running); + request.setStatusMsg(R.string.download_running); if (AppConfig.DEBUG) Log.d(TAG, "Getting size of download"); - status.setSize(httpEntity.getContentLength()); + request.setSize(httpEntity.getContentLength()); if (AppConfig.DEBUG) - Log.d(TAG, "Size is " + status.getSize()); - if (status.getSize() < 0) { - status.setSize(DownloadStatus.SIZE_UNKNOWN); + Log.d(TAG, "Size is " + request.getSize()); + if (request.getSize() < 0) { + request.setSize(DownloadStatus.SIZE_UNKNOWN); } long freeSpace = StorageUtils.getFreeSpaceAvailable(); if (AppConfig.DEBUG) Log.d(TAG, "Free space is " + freeSpace); - if (status.getSize() == DownloadStatus.SIZE_UNKNOWN - || status.getSize() <= freeSpace) { + if (request.getSize() == DownloadStatus.SIZE_UNKNOWN + || request.getSize() <= freeSpace) { if (AppConfig.DEBUG) Log.d(TAG, "Starting download"); while (!cancelled && (count = in.read(buffer)) != -1) { out.write(buffer, 0, count); - status.setSoFar(status.getSoFar() + count); - status.setProgressPercent((int) (((double) status - .getSoFar() / (double) status.getSize()) * 100)); + request.setSoFar(request.getSoFar() + count); + request.setProgressPercent((int) (((double) request + .getSoFar() / (double) request + .getSize()) * 100)); } if (cancelled) { onCancelled(); @@ -144,8 +142,7 @@ public class HttpDownloader extends Downloader { } catch (NullPointerException e) { // might be thrown by connection.getInputStream() e.printStackTrace(); - onFail(DownloadError.ERROR_CONNECTION_ERROR, status.getFeedFile() - .getDownload_url()); + onFail(DownloadError.ERROR_CONNECTION_ERROR, request.getSource()); } finally { IOUtils.closeQuietly(connection); IOUtils.closeQuietly(out); @@ -158,36 +155,30 @@ public class HttpDownloader extends Downloader { private void onSuccess() { if (AppConfig.DEBUG) Log.d(TAG, "Download was successful"); - status.setSuccessful(true); - status.setDone(true); + result = new DownloadStatus(request, 0, true, false, null); } private void onFail(int reason, String reasonDetailed) { if (AppConfig.DEBUG) { Log.d(TAG, "Download failed"); } - status.setReason(reason); - status.setReasonDetailed(reasonDetailed); - status.setDone(true); - status.setSuccessful(false); + result = new DownloadStatus(request, reason, false, false, + reasonDetailed); cleanup(); } private void onCancelled() { if (AppConfig.DEBUG) Log.d(TAG, "Download was cancelled"); - status.setReason(DownloadError.ERROR_DOWNLOAD_CANCELLED); - status.setDone(true); - status.setSuccessful(false); - status.setCancelled(true); + result = new DownloadStatus(request, + DownloadError.ERROR_DOWNLOAD_CANCELLED, false, true, null); cleanup(); } /** Deletes unfinished downloads. */ private void cleanup() { - if (status != null && status.getFeedFile() != null - && status.getFeedFile().getFile_url() != null) { - File dest = new File(status.getFeedFile().getFile_url()); + if (request.getDestination() != null) { + File dest = new File(request.getDestination()); if (dest.exists()) { boolean rc = dest.delete(); if (AppConfig.DEBUG) |