diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-08-17 20:47:35 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-08-17 20:47:35 +0200 |
commit | 026efe29c3c26a5429c09c4bb6421dcde9c8ff78 (patch) | |
tree | 87a17622643572e8348e36db6ad7ae27e7c414b0 | |
parent | f2d72739f9121a62610f2c579fe6ec3f3ac896fc (diff) | |
download | AntennaPod-026efe29c3c26a5429c09c4bb6421dcde9c8ff78.zip |
HttpDownloader now checks if enough storage is available
6 files changed, 66 insertions, 30 deletions
diff --git a/res/values/strings.xml b/res/values/strings.xml index 425152192..2670a78ed 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -176,6 +176,9 @@ <string name="user_interface_label">User Interface</string> <string name="feed_delete_confirmation_msg">Please confirm that you want to delete this feed and ALL episodes of this feed that you have downloaded.</string> <string name="image_of_prefix">Image of:\u0020</string> + <string name="download_error_malformed_url">Malformed URL</string> + <string name="download_error_io_error">IO Error</string> + <string name="download_error_device_not_found">External storage unavailable</string> </resources>
\ No newline at end of file diff --git a/src/de/danoeh/antennapod/service/download/DownloadService.java b/src/de/danoeh/antennapod/service/download/DownloadService.java index 6a8a3cb2f..67094133a 100644 --- a/src/de/danoeh/antennapod/service/download/DownloadService.java +++ b/src/de/danoeh/antennapod/service/download/DownloadService.java @@ -661,7 +661,6 @@ public class DownloadService extends Service { class MediaHandlerThread implements Runnable { private FeedMedia media; private DownloadStatus status; - private DownloadService service; public MediaHandlerThread(DownloadStatus status) { super(); @@ -686,7 +685,7 @@ public class DownloadService extends Service { saveDownloadStatus(status); sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA); - manager.setFeedMedia(service, media); + manager.setFeedMedia(DownloadService.this, media); boolean autoQueue = PreferenceManager.getDefaultSharedPreferences( getApplicationContext()).getBoolean( PodcastApp.PREF_AUTO_QUEUE, true); diff --git a/src/de/danoeh/antennapod/service/download/Downloader.java b/src/de/danoeh/antennapod/service/download/Downloader.java index 83cdeb921..13ee8896c 100644 --- a/src/de/danoeh/antennapod/service/download/Downloader.java +++ b/src/de/danoeh/antennapod/service/download/Downloader.java @@ -1,7 +1,9 @@ package de.danoeh.antennapod.service.download; import de.danoeh.antennapod.asynctask.DownloadStatus; +import android.os.Environment; import android.os.Handler; +import android.os.StatFs; /** Downloads files */ public abstract class Downloader extends Thread { diff --git a/src/de/danoeh/antennapod/service/download/HttpDownloader.java b/src/de/danoeh/antennapod/service/download/HttpDownloader.java index 41db8c980..fb9d0c6ec 100644 --- a/src/de/danoeh/antennapod/service/download/HttpDownloader.java +++ b/src/de/danoeh/antennapod/service/download/HttpDownloader.java @@ -17,6 +17,7 @@ import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.R; import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.util.DownloadError; +import de.danoeh.antennapod.util.StorageUtils; public class HttpDownloader extends Downloader { private static final String TAG = "HttpDownloader"; @@ -41,36 +42,49 @@ public class HttpDownloader extends Downloader { 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); - if (AppConfig.DEBUG) - Log.d(TAG, "Getting size of download"); - status.setSize(connection.getContentLength()); - if (AppConfig.DEBUG) - Log.d(TAG, "Size is " + status.getSize()); - publishProgress(); - if (AppConfig.DEBUG) - Log.d(TAG, "Starting download"); - while ((count = in.read(buffer)) != -1 && !isInterrupted()) { - out.write(buffer, 0, count); - status.setSoFar(status.getSoFar() + count); - status.setProgressPercent((int) (((double) status - .getSoFar() / (double) status.getSize()) * 100)); - } - if (isInterrupted()) { - onCancelled(); + if (StorageUtils.externalStorageMounted()) { + 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); + if (AppConfig.DEBUG) + Log.d(TAG, "Getting size of download"); + status.setSize(connection.getContentLength()); + if (AppConfig.DEBUG) + Log.d(TAG, "Size is " + status.getSize()); + if (status.getSize() == -1 + || status.getSize() <= StorageUtils + .getFreeSpaceAvailable()) { + if (AppConfig.DEBUG) + Log.d(TAG, "Size is " + status.getSize()); + publishProgress(); + if (AppConfig.DEBUG) + Log.d(TAG, "Starting download"); + while ((count = in.read(buffer)) != -1 + && !isInterrupted()) { + out.write(buffer, 0, count); + status.setSoFar(status.getSoFar() + count); + status.setProgressPercent((int) (((double) status + .getSoFar() / (double) status.getSize()) * 100)); + } + if (isInterrupted()) { + onCancelled(); + } else { + onSuccess(); + } + } else { + onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE); + } } else { - onSuccess(); + onFail(DownloadError.ERROR_FILE_EXISTS); } } else { - onFail(DownloadError.ERROR_FILE_EXISTS); + onFail(DownloadError.ERROR_DEVICE_NOT_FOUND); } } catch (MalformedURLException e) { e.printStackTrace(); diff --git a/src/de/danoeh/antennapod/util/DownloadError.java b/src/de/danoeh/antennapod/util/DownloadError.java index 2967711e6..c3f44672f 100644 --- a/src/de/danoeh/antennapod/util/DownloadError.java +++ b/src/de/danoeh/antennapod/util/DownloadError.java @@ -14,14 +14,21 @@ public class DownloadError { public static final int ERROR_DOWNLOAD_CANCELLED = 7; public static final int ERROR_DEVICE_NOT_FOUND = 8; public static final int ERROR_HTTP_DATA_ERROR = 9; + public static final int ERROR_NOT_ENOUGH_SPACE = 10; /** Get a human-readable string for a specific error code. */ public static String getErrorString(Context context, int code) { int resId; switch(code) { - case ERROR_DEVICE_NOT_FOUND: + case ERROR_NOT_ENOUGH_SPACE: resId = R.string.download_error_insufficient_space; break; + case ERROR_DEVICE_NOT_FOUND: + resId = R.string.download_error_device_not_found; + break; + case ERROR_IO_ERROR: + resId = R.string.download_error_io_error; + break; case ERROR_HTTP_DATA_ERROR: resId = R.string.download_error_http_data_error; break; diff --git a/src/de/danoeh/antennapod/util/StorageUtils.java b/src/de/danoeh/antennapod/util/StorageUtils.java index 942c333fb..eb3b5d3a4 100644 --- a/src/de/danoeh/antennapod/util/StorageUtils.java +++ b/src/de/danoeh/antennapod/util/StorageUtils.java @@ -4,6 +4,7 @@ import de.danoeh.antennapod.activity.StorageErrorActivity; import android.app.Activity; import android.content.Intent; import android.os.Environment; +import android.os.StatFs; /** Utility functions for handling storage errors */ public class StorageUtils { @@ -25,4 +26,14 @@ public class StorageUtils { } return storageAvailable; } + + /** Get the number of free bytes that are available on the external storage. */ + public static int getFreeSpaceAvailable() { + StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); + return stat.getAvailableBlocks() * stat.getBlockSize(); + } + + public static boolean externalStorageMounted() { + return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); + } } |