summaryrefslogtreecommitdiff
path: root/src/de/danoeh
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2012-08-17 20:47:35 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2012-08-17 20:47:35 +0200
commit026efe29c3c26a5429c09c4bb6421dcde9c8ff78 (patch)
tree87a17622643572e8348e36db6ad7ae27e7c414b0 /src/de/danoeh
parentf2d72739f9121a62610f2c579fe6ec3f3ac896fc (diff)
downloadAntennaPod-026efe29c3c26a5429c09c4bb6421dcde9c8ff78.zip
HttpDownloader now checks if enough storage is available
Diffstat (limited to 'src/de/danoeh')
-rw-r--r--src/de/danoeh/antennapod/service/download/DownloadService.java3
-rw-r--r--src/de/danoeh/antennapod/service/download/Downloader.java2
-rw-r--r--src/de/danoeh/antennapod/service/download/HttpDownloader.java68
-rw-r--r--src/de/danoeh/antennapod/util/DownloadError.java9
-rw-r--r--src/de/danoeh/antennapod/util/StorageUtils.java11
5 files changed, 63 insertions, 30 deletions
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);
+ }
}