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="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>

View File

@ -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);

View File

@ -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 {

View File

@ -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();

View File

@ -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;

View File

@ -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);
}
}