HttpDownloader now checks if enough storage is available

This commit is contained in:
daniel oeh 2012-08-17 20:47:35 +02:00
parent f2d72739f9
commit 026efe29c3
6 changed files with 66 additions and 30 deletions

View File

@ -176,6 +176,9 @@
<string name="user_interface_label">User Interface</string> <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="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="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> </resources>

View File

@ -661,7 +661,6 @@ public class DownloadService extends Service {
class MediaHandlerThread implements Runnable { class MediaHandlerThread implements Runnable {
private FeedMedia media; private FeedMedia media;
private DownloadStatus status; private DownloadStatus status;
private DownloadService service;
public MediaHandlerThread(DownloadStatus status) { public MediaHandlerThread(DownloadStatus status) {
super(); super();
@ -686,7 +685,7 @@ public class DownloadService extends Service {
saveDownloadStatus(status); saveDownloadStatus(status);
sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA); sendDownloadHandledIntent(DOWNLOAD_TYPE_MEDIA);
manager.setFeedMedia(service, media); manager.setFeedMedia(DownloadService.this, media);
boolean autoQueue = PreferenceManager.getDefaultSharedPreferences( boolean autoQueue = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext()).getBoolean( getApplicationContext()).getBoolean(
PodcastApp.PREF_AUTO_QUEUE, true); PodcastApp.PREF_AUTO_QUEUE, true);

View File

@ -1,7 +1,9 @@
package de.danoeh.antennapod.service.download; package de.danoeh.antennapod.service.download;
import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.asynctask.DownloadStatus;
import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.os.StatFs;
/** Downloads files */ /** Downloads files */
public abstract class Downloader extends Thread { public abstract class Downloader extends Thread {

View File

@ -17,6 +17,7 @@ import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.DownloadStatus; import de.danoeh.antennapod.asynctask.DownloadStatus;
import de.danoeh.antennapod.util.DownloadError; import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.StorageUtils;
public class HttpDownloader extends Downloader { public class HttpDownloader extends Downloader {
private static final String TAG = "HttpDownloader"; private static final String TAG = "HttpDownloader";
@ -41,24 +42,31 @@ public class HttpDownloader extends Downloader {
if (AppConfig.DEBUG) { if (AppConfig.DEBUG) {
Log.d(TAG, "Connected to resource"); Log.d(TAG, "Connected to resource");
} }
if (StorageUtils.externalStorageMounted()) {
File destination = new File(status.getFeedFile().getFile_url()); File destination = new File(status.getFeedFile().getFile_url());
if (!destination.exists()) { if (!destination.exists()) {
InputStream in = new BufferedInputStream( InputStream in = new BufferedInputStream(
connection.getInputStream()); connection.getInputStream());
out = new BufferedOutputStream( out = new BufferedOutputStream(new FileOutputStream(
new FileOutputStream(destination)); destination));
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
int count = 0; int count = 0;
status.setStatusMsg(R.string.download_running); status.setStatusMsg(R.string.download_running);
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Getting size of download"); Log.d(TAG, "Getting size of download");
status.setSize(connection.getContentLength()); 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) if (AppConfig.DEBUG)
Log.d(TAG, "Size is " + status.getSize()); Log.d(TAG, "Size is " + status.getSize());
publishProgress(); publishProgress();
if (AppConfig.DEBUG) if (AppConfig.DEBUG)
Log.d(TAG, "Starting download"); Log.d(TAG, "Starting download");
while ((count = in.read(buffer)) != -1 && !isInterrupted()) { while ((count = in.read(buffer)) != -1
&& !isInterrupted()) {
out.write(buffer, 0, count); out.write(buffer, 0, count);
status.setSoFar(status.getSoFar() + count); status.setSoFar(status.getSoFar() + count);
status.setProgressPercent((int) (((double) status status.setProgressPercent((int) (((double) status
@ -69,9 +77,15 @@ public class HttpDownloader extends Downloader {
} else { } else {
onSuccess(); onSuccess();
} }
} else {
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE);
}
} else { } else {
onFail(DownloadError.ERROR_FILE_EXISTS); onFail(DownloadError.ERROR_FILE_EXISTS);
} }
} else {
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND);
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
onFail(DownloadError.ERROR_MALFORMED_URL); onFail(DownloadError.ERROR_MALFORMED_URL);

View File

@ -14,14 +14,21 @@ public class DownloadError {
public static final int ERROR_DOWNLOAD_CANCELLED = 7; public static final int ERROR_DOWNLOAD_CANCELLED = 7;
public static final int ERROR_DEVICE_NOT_FOUND = 8; public static final int ERROR_DEVICE_NOT_FOUND = 8;
public static final int ERROR_HTTP_DATA_ERROR = 9; 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. */ /** Get a human-readable string for a specific error code. */
public static String getErrorString(Context context, int code) { public static String getErrorString(Context context, int code) {
int resId; int resId;
switch(code) { switch(code) {
case ERROR_DEVICE_NOT_FOUND: case ERROR_NOT_ENOUGH_SPACE:
resId = R.string.download_error_insufficient_space; resId = R.string.download_error_insufficient_space;
break; 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: case ERROR_HTTP_DATA_ERROR:
resId = R.string.download_error_http_data_error; resId = R.string.download_error_http_data_error;
break; break;

View File

@ -4,6 +4,7 @@ import de.danoeh.antennapod.activity.StorageErrorActivity;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Environment; import android.os.Environment;
import android.os.StatFs;
/** Utility functions for handling storage errors */ /** Utility functions for handling storage errors */
public class StorageUtils { public class StorageUtils {
@ -25,4 +26,14 @@ public class StorageUtils {
} }
return storageAvailable; 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);
}
} }