diff options
Diffstat (limited to 'src/de')
4 files changed, 175 insertions, 22 deletions
diff --git a/src/de/danoeh/antennapod/asynctask/DownloadStatus.java b/src/de/danoeh/antennapod/asynctask/DownloadStatus.java index 67cf4a6d8..e5b2bcf5c 100644 --- a/src/de/danoeh/antennapod/asynctask/DownloadStatus.java +++ b/src/de/danoeh/antennapod/asynctask/DownloadStatus.java @@ -14,6 +14,9 @@ public class DownloadStatus { /** Unique id for storing the object in database. */ protected long id; + /** Used by DownloadService to check if the status has been updated. */ + protected volatile boolean updateAvailable; + protected FeedFile feedfile; protected int progressPercent; protected long soFar; @@ -29,8 +32,8 @@ public class DownloadStatus { } /** Constructor for restoring Download status entries from DB. */ - public DownloadStatus(long id, FeedFile feedfile, boolean successful, int reason, - Date completionDate) { + public DownloadStatus(long id, FeedFile feedfile, boolean successful, + int reason, Date completionDate) { this.id = id; this.feedfile = feedfile; progressPercent = 100; @@ -41,11 +44,9 @@ public class DownloadStatus { this.done = true; this.completionDate = completionDate; } - - + /** Constructor for creating new completed downloads. */ - public DownloadStatus(FeedFile feedfile, int reason, - boolean successful) { + public DownloadStatus(FeedFile feedfile, int reason, boolean successful) { this(0, feedfile, successful, reason, new Date()); } @@ -88,8 +89,49 @@ public class DownloadStatus { public boolean isDone() { return done; } - - - + + public void setFeedfile(FeedFile feedfile) { + this.feedfile = feedfile; + } + + public void setProgressPercent(int progressPercent) { + this.progressPercent = progressPercent; + } + + public void setSoFar(long soFar) { + this.soFar = soFar; + } + + public void setSize(long size) { + this.size = size; + } + + public void setStatusMsg(int statusMsg) { + this.statusMsg = statusMsg; + } + + public void setReason(int reason) { + this.reason = reason; + } + + public void setSuccessful(boolean successful) { + this.successful = successful; + } + + public void setDone(boolean done) { + this.done = done; + } + + public void setCompletionDate(Date completionDate) { + this.completionDate = completionDate; + } + + public boolean isUpdateAvailable() { + return updateAvailable; + } + + public void setUpdateAvailable(boolean updateAvailable) { + this.updateAvailable = updateAvailable; + } }
\ 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 6eb2562f3..5871c3ef1 100644 --- a/src/de/danoeh/antennapod/service/download/Downloader.java +++ b/src/de/danoeh/antennapod/service/download/Downloader.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.service.download; +import de.danoeh.antennapod.asynctask.DownloadStatus; import android.os.Handler; /** Downloads files */ @@ -7,25 +8,21 @@ public abstract class Downloader extends Thread { private static final String TAG = "Downloader"; private Handler handler; private DownloadService downloadService; - + protected boolean finished; - - protected String destination; - protected String source; - - - public Downloader(DownloadService downloadService, String destination, String source) { + protected DownloadStatus status; + + public Downloader(DownloadService downloadService, DownloadStatus status) { super(); this.downloadService = downloadService; - this.destination = destination; - this.source = source; + this.status = status; handler = new Handler(); } /** - * This method must be called when the download was completed, failed, - * or was cancelled + * This method must be called when the download was completed, failed, or + * was cancelled */ protected void finish() { if (!finished) { @@ -40,9 +37,13 @@ public abstract class Downloader extends Thread { }); } } + + protected void publishProgress() { + status.setUpdateAvailable(true); + } protected abstract void download(); - + @Override public final void run() { download(); diff --git a/src/de/danoeh/antennapod/service/download/HttpDownloader.java b/src/de/danoeh/antennapod/service/download/HttpDownloader.java new file mode 100644 index 000000000..32ab7f59d --- /dev/null +++ b/src/de/danoeh/antennapod/service/download/HttpDownloader.java @@ -0,0 +1,107 @@ +package de.danoeh.antennapod.service.download; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import android.util.Log; +import de.danoeh.antennapod.AppConfig; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.asynctask.DownloadStatus; +import de.danoeh.antennapod.util.DownloadError; + +public class HttpDownloader extends Downloader { + private static final String TAG = "HttpDownloader"; + + private static final int BUFFER_SIZE = 8 * 1024; + + public HttpDownloader(DownloadService downloadService, DownloadStatus status) { + super(downloadService, status); + } + + @Override + protected void download() { + HttpURLConnection connection = null; + OutputStream out = null; + try { + status.setStatusMsg(R.string.download_pending); + publishProgress(); + URL url = new URL(status.getFeedFile().getDownload_url()); + connection = (HttpURLConnection) url.openConnection(); + if (AppConfig.DEBUG) { + Log.d(TAG, "Connected to resource"); + } + File destination = new File(status.getFeedFile().getFile_url()); + if (!destination.exists()) { + InputStream in = new BufferedInputStream( + connection.getInputStream()); + out = new BufferedOutputStream( + new FileOutputStream(destination)); + byte[] buffer = new byte[BUFFER_SIZE]; + int count = 0; + status.setStatusMsg(R.string.download_running); + status.setSize(connection.getContentLength()); + publishProgress(); + while ((count = in.read(buffer)) != -1 || !isInterrupted()) { + out.write(buffer, 0, count); + status.setSoFar(status.getSoFar() + count); + } + if (isInterrupted()) { + onCancelled(); + } else { + onSuccess(); + } + } else { + onFail(DownloadError.ERROR_FILE_EXISTS); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + onFail(DownloadError.ERROR_MALFORMED_URL); + } catch (IOException e) { + e.printStackTrace(); + onFail(DownloadError.ERROR_IO_ERROR); + } finally { + if (connection != null) { + connection.disconnect(); + } + if (out != null) { + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private void onSuccess() { + if (AppConfig.DEBUG) + Log.d(TAG, "Download was successful"); + status.setSuccessful(true); + status.setDone(true); + } + + private void onFail(int reason) { + if (AppConfig.DEBUG) { + Log.d(TAG, "Download failed"); + } + status.setReason(reason); + status.setDone(true); + status.setSuccessful(false); + } + + private void onCancelled() { + if (AppConfig.DEBUG) Log.d(TAG, "Download was cancelled"); + status.setReason(DownloadError.ERROR_DOWNLOAD_CANCELLED); + status.setDone(true); + status.setSuccessful(false); + } + +} diff --git a/src/de/danoeh/antennapod/util/DownloadError.java b/src/de/danoeh/antennapod/util/DownloadError.java index b2f43a8dd..2e76cc53d 100644 --- a/src/de/danoeh/antennapod/util/DownloadError.java +++ b/src/de/danoeh/antennapod/util/DownloadError.java @@ -9,7 +9,10 @@ public class DownloadError { public static final int ERROR_PARSER_EXCEPTION = 1; public static final int ERROR_UNSUPPORTED_TYPE = 2; public static final int ERROR_CONNECTION_ERROR = 3; - + public static final int ERROR_MALFORMED_URL = 4; + public static final int ERROR_IO_ERROR = 5; + public static final int ERROR_FILE_EXISTS = 6; + public static final int ERROR_DOWNLOAD_CANCELLED = 7; /** Get a human-readable string for a specific error code. */ public static String getErrorString(Context context, int code) { |