[Android] do not hardcode cover cache path

Sébastien Toque git at videolan.org
Sun Oct 14 18:39:34 CEST 2012


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Sun Oct 14 17:40:41 2012 +0200| [b5b69a4e9933885d6063f54d3ef85f6bd63567f1] | committer: Sébastien Toque

do not hardcode cover cache path

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=b5b69a4e9933885d6063f54d3ef85f6bd63567f1
---

 vlc-android/src/org/videolan/vlc/AudioService.java      |    2 +-
 vlc-android/src/org/videolan/vlc/Util.java              |   12 ++++++++++++
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java       |   15 +++++++++++----
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index c14f3e2..8b2a48a 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -130,7 +130,7 @@ public class AudioService extends Service {
         if(Util.isICSOrLater())
             setUpRemoteControlClient();
 
-        AudioUtil.prepareCacheFolder();
+        AudioUtil.prepareCacheFolder(this);
     }
 
     @TargetApi(14)
diff --git a/vlc-android/src/org/videolan/vlc/Util.java b/vlc-android/src/org/videolan/vlc/Util.java
index 88146a8..017a217 100644
--- a/vlc-android/src/org/videolan/vlc/Util.java
+++ b/vlc-android/src/org/videolan/vlc/Util.java
@@ -36,6 +36,7 @@ import java.util.Properties;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.net.Uri;
+import android.os.Environment;
 import android.telephony.TelephonyManager;
 import android.util.DisplayMetrics;
 import android.util.TypedValue;
@@ -213,6 +214,11 @@ public class Util {
                          );
     }
 
+    public static boolean isFroyoOrLater()
+    {
+        return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO;
+    }
+
     public static boolean isGingerbreadOrLater()
     {
         return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;
@@ -233,6 +239,12 @@ public class Util {
         return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN;
     }
 
+    public static boolean hasExternalStorage() {
+        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ||
+                !Util.isGingerbreadOrLater() ||
+                Environment.isExternalStorageRemovable();
+    }
+
     public static boolean hasNavBar()
     {
         return hasNavBar;
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
index 59cb794..ed8ff1c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -46,6 +46,7 @@ import android.graphics.Bitmap.CompressFormat;
 import android.graphics.BitmapFactory;
 import android.media.RingtoneManager;
 import android.net.Uri;
+import android.os.Environment;
 import android.provider.MediaStore;
 import android.util.Log;
 
@@ -53,9 +54,8 @@ public class AudioUtil {
 
     public final static String TAG = "VLC/AudioUtil";
 
-    @SuppressLint("SdCardPath")
-    public final static String CACHE_DIR = "/sdcard/Android/data/org.videolan.vlc/cache";
-    public final static String COVER_DIR = CACHE_DIR + "/covers/";
+    public static String CACHE_DIR = null;
+    public static String COVER_DIR = null;
 
     public static void setRingtone( Media song, Activity activity){
         File newringtone = Util.URItoFile(song.getLocation());
@@ -78,7 +78,14 @@ public class AudioUtil {
                 );
     }
 
-    public static void prepareCacheFolder() {
+    @SuppressLint("NewApi")
+    public static void prepareCacheFolder(Context context) {
+        if (Util.isFroyoOrLater() && Util.hasExternalStorage())
+            CACHE_DIR = context.getExternalCacheDir().getPath();
+        else
+            CACHE_DIR = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + context.getPackageName() + "/cache";
+        COVER_DIR = CACHE_DIR + "/covers/";
+
         File file = new File(COVER_DIR);
         if (!file.exists())
             file.mkdirs();



More information about the Android mailing list