Updated storage availability check

This commit is contained in:
daniel oeh 2012-12-30 22:03:55 +01:00
parent 65491d5436
commit cd9d69d129
3 changed files with 35 additions and 17 deletions

View File

@ -39,7 +39,7 @@ public class StorageErrorActivity extends SherlockActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if (StorageUtils.storageAvailable()) { if (StorageUtils.storageAvailable(this)) {
leaveErrorState(); leaveErrorState();
} else { } else {
registerReceiver(mediaUpdate, new IntentFilter( registerReceiver(mediaUpdate, new IntentFilter(

View File

@ -19,6 +19,7 @@ import org.apache.http.HttpStatus;
import android.util.Log; import android.util.Log;
import de.danoeh.antennapod.AppConfig; import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
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;
@ -104,7 +105,7 @@ 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()) { if (StorageUtils.storageAvailable(PodcastApp.getInstance())) {
File destination = new File(status.getFeedFile() File destination = new File(status.getFeedFile()
.getFile_url()); .getFile_url());
if (!destination.exists()) { if (!destination.exists()) {

View File

@ -1,41 +1,58 @@
package de.danoeh.antennapod.util; package de.danoeh.antennapod.util;
import java.io.File;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Environment; import android.os.Environment;
import android.os.StatFs; import android.os.StatFs;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.activity.StorageErrorActivity; import de.danoeh.antennapod.activity.StorageErrorActivity;
/** Utility functions for handling storage errors */ /** Utility functions for handling storage errors */
public class StorageUtils { public class StorageUtils {
public static boolean storageAvailable() { private static final String TAG = "StorageUtils";
String state = Environment.getExternalStorageState();
return state.equals(Environment.MEDIA_MOUNTED); public static boolean storageAvailable(Context context) {
File dir = PodcastApp.getDataFolder(context, null);
if (dir != null) {
return dir.exists() && dir.canRead() && dir.canWrite();
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "Storage not available: data folder is null");
return false;
}
} }
/**Checks if external storage is available. If external storage isn't /**
* available, the current activity is finsished an an error activity is launched. * Checks if external storage is available. If external storage isn't
* @param activity the activity which would be finished if no storage is available * available, the current activity is finsished an an error activity is
* launched.
*
* @param activity
* the activity which would be finished if no storage is
* available
* @return true if external storage is available * @return true if external storage is available
*/ */
public static boolean checkStorageAvailability(Activity activity) { public static boolean checkStorageAvailability(Activity activity) {
boolean storageAvailable = storageAvailable(); boolean storageAvailable = storageAvailable(activity);
if (!storageAvailable) { if (!storageAvailable) {
activity.finish(); activity.finish();
activity.startActivity(new Intent(activity, StorageErrorActivity.class)); activity.startActivity(new Intent(activity,
StorageErrorActivity.class));
} }
return storageAvailable; return storageAvailable;
} }
/** Get the number of free bytes that are available on the external storage. */ /** Get the number of free bytes that are available on the external storage. */
public static long getFreeSpaceAvailable() { public static long getFreeSpaceAvailable() {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
long availableBlocks = stat.getAvailableBlocks(); long availableBlocks = stat.getAvailableBlocks();
long blockSize = stat.getBlockSize(); long blockSize = stat.getBlockSize();
return availableBlocks * blockSize; return availableBlocks * blockSize;
} }
public static boolean externalStorageMounted() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
} }