|
@@ -0,0 +1,83 @@
|
|
|
|
+package com.xuqinmin.android.app.common;
|
|
|
|
+
|
|
|
|
+import static android.os.Environment.MEDIA_MOUNTED;
|
|
|
|
+
|
|
|
|
+import android.content.Context;
|
|
|
|
+import android.os.Environment;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Provides application storage paths
|
|
|
|
+ * <p/>
|
|
|
|
+ * See https://github.com/nostra13/Android-Universal-Image-Loader
|
|
|
|
+ *
|
|
|
|
+ * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
|
|
|
|
+ * @since 1.0.0
|
|
|
|
+ */
|
|
|
|
+public class StorageUtils {
|
|
|
|
+
|
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger("StorageUtils");
|
|
|
|
+ private static final String INDIVIDUAL_DIR_NAME = "video-cache";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
|
|
|
|
+ * created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
|
|
|
|
+ * Else - Android defines cache directory on device's file system.
|
|
|
|
+ *
|
|
|
|
+ * @param context Application context
|
|
|
|
+ * @return Cache {@link File directory}
|
|
|
|
+ */
|
|
|
|
+ public static File getIndividualCacheDirectory(Context context) {
|
|
|
|
+ File cacheDir = getCacheDirectory(context, true);
|
|
|
|
+ return new File(cacheDir, INDIVIDUAL_DIR_NAME);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns application cache directory. Cache directory will be created on SD card
|
|
|
|
+ * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
|
|
|
|
+ * on device's file system depending incoming parameters.
|
|
|
|
+ *
|
|
|
|
+ * @param context Application context
|
|
|
|
+ * @param preferExternal Whether prefer external location for cache
|
|
|
|
+ * @return Cache {@link File directory}.<br />
|
|
|
|
+ * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
|
|
|
|
+ * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
|
|
|
|
+ */
|
|
|
|
+ private static File getCacheDirectory(Context context, boolean preferExternal) {
|
|
|
|
+ File appCacheDir = null;
|
|
|
|
+ String externalStorageState;
|
|
|
|
+ try {
|
|
|
|
+ externalStorageState = Environment.getExternalStorageState();
|
|
|
|
+ } catch (NullPointerException e) { // (sh)it happens
|
|
|
|
+ externalStorageState = "";
|
|
|
|
+ }
|
|
|
|
+ if (preferExternal && MEDIA_MOUNTED.equals(externalStorageState)) {
|
|
|
|
+ appCacheDir = getExternalCacheDir(context);
|
|
|
|
+ }
|
|
|
|
+ if (appCacheDir == null) {
|
|
|
|
+ appCacheDir = context.getCacheDir();
|
|
|
|
+ }
|
|
|
|
+ if (appCacheDir == null) {
|
|
|
|
+ String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
|
|
|
|
+ LOG.warn("Can't define system cache directory! '" + cacheDirPath + "%s' will be used.");
|
|
|
|
+ appCacheDir = new File(cacheDirPath);
|
|
|
|
+ }
|
|
|
|
+ return appCacheDir;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static File getExternalCacheDir(Context context) {
|
|
|
|
+ File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
|
|
|
|
+ File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
|
|
|
|
+ if (!appCacheDir.exists()) {
|
|
|
|
+ if (!appCacheDir.mkdirs()) {
|
|
|
|
+ LOG.warn("Unable to create external cache directory");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return appCacheDir;
|
|
|
|
+ }
|
|
|
|
+}
|