package de.danoeh.antennapod.util;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.preferences.UserPreferences;

/** Utility functions for handling storage errors */
public class StorageUtils {
	private static final String TAG = "StorageUtils";

	public static boolean storageAvailable(Context context) {
		File dir = UserPreferences.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.
	 * 
	 * @param activity
	 *            the activity which would be finished if no storage is
	 *            available
	 * @return true if external storage is available
	 */
	public static boolean checkStorageAvailability(Activity activity) {
		boolean storageAvailable = storageAvailable(activity);
		if (!storageAvailable) {
			activity.finish();
			activity.startActivity(new Intent(activity,
					StorageErrorActivity.class));
		}
		return storageAvailable;
	}

	/** Get the number of free bytes that are available on the external storage. */
	public static long getFreeSpaceAvailable() {
		StatFs stat = new StatFs(UserPreferences.getDataFolder(
				PodcastApp.getInstance(), null).getAbsolutePath());
		long availableBlocks = stat.getAvailableBlocks();
		long blockSize = stat.getBlockSize();
		return availableBlocks * blockSize;
	}
}

@@ -143,16 +143,41 @@
 Intent;%0A
+import android.os.Build;%0A
 import a
@@ -1738,18 +1738,24 @@
 ath());%0A
-%09%09
+        
 long ava
@@ -1770,44 +1770,298 @@
 ocks
- = stat.getAvailableBlocks();%0A%09%09long
+;%0A        long blockSize;%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN_MR2) %7B%0A%09%09    availableBlocks = stat.getAvailableBlocksLong();%0A            blockSize = stat.getBlockSizeLong();%0A        %7D else %7B%0A            availableBlocks = stat.getAvailableBlocks();%0A           
  blo
@@ -2090,16 +2090,26 @@
 Size();%0A
+        %7D%0A
 %09%09return

Use new StatFS API if available