summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2014-09-17 20:51:45 +0200
committerdaniel oeh <daniel.oeh@gmail.com>2014-09-17 20:51:45 +0200
commit072639b5b22e816df9f78b5cd8a7d4e5379b6aff (patch)
tree341c574bd6eb64497470e7226b3222b0a7c5a824 /app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java
parent76add8ef68dbc9997e901f4c11c397f581e8eabe (diff)
downloadAntennaPod-072639b5b22e816df9f78b5cd8a7d4e5379b6aff.zip
Changed project structure
Switched from custom layout to standard gradle project structure
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java')
-rw-r--r--app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java b/app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java
new file mode 100644
index 000000000..ff0bde280
--- /dev/null
+++ b/app/src/main/java/de/danoeh/antennapod/util/StorageUtils.java
@@ -0,0 +1,66 @@
+package de.danoeh.antennapod.util;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Build;
+import android.os.StatFs;
+import android.util.Log;
+import de.danoeh.antennapod.BuildConfig;
+import de.danoeh.antennapod.PodcastApp;
+import de.danoeh.antennapod.activity.StorageErrorActivity;
+import de.danoeh.antennapod.preferences.UserPreferences;
+
+import java.io.File;
+
+/** 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 (BuildConfig.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;
+ long blockSize;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
+ availableBlocks = stat.getAvailableBlocksLong();
+ blockSize = stat.getBlockSizeLong();
+ } else {
+ availableBlocks = stat.getAvailableBlocks();
+ blockSize = stat.getBlockSize();
+ }
+ return availableBlocks * blockSize;
+ }
+}