Commit 60d45e7d authored by Sébastien Toque's avatar Sébastien Toque

AudioService: split getCover and make it available from anywhere

parent e78d7d4c
...@@ -21,8 +21,10 @@ ...@@ -21,8 +21,10 @@
package org.videolan.vlc; package org.videolan.vlc;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger; import java.math.BigInteger;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -595,17 +597,14 @@ public class AudioService extends Service { ...@@ -595,17 +597,14 @@ public class AudioService extends Service {
mRepeating = RepeatType.values()[t]; mRepeating = RepeatType.values()[t];
} }
@SuppressLint("SdCardPath") private static Bitmap getCoverFromMediaStore(Context context, Media media) {
private Bitmap getCover() { ContentResolver contentResolver = context.getContentResolver();
try {
// try to get the cover from android MediaStore
ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; Uri uri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, new String[] { Cursor cursor = contentResolver.query(uri, new String[] {
MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ALBUM_ART }, MediaStore.Audio.Albums.ALBUM_ART },
MediaStore.Audio.Albums.ALBUM + " LIKE ?", MediaStore.Audio.Albums.ALBUM + " LIKE ?",
new String[] { mCurrentMedia.getAlbum() }, null); new String[] { media.getAlbum() }, null);
if (cursor == null) { if (cursor == null) {
// do nothing // do nothing
} else if (!cursor.moveToFirst()) { } else if (!cursor.moveToFirst()) {
...@@ -621,16 +620,19 @@ public class AudioService extends Service { ...@@ -621,16 +620,19 @@ public class AudioService extends Service {
return b; return b;
} }
} }
return null;
}
//cover not in MediaStore, trying vlc @SuppressLint("SdCardPath")
String artworkURL = mCurrentMedia.getArtworkURL(); private static Bitmap getCoverFromVlc(Context context, Media media) throws NoSuchAlgorithmException, UnsupportedEncodingException {
String artworkURL = media.getArtworkURL();
final String cacheDir = "/sdcard/Android/data/org.videolan.vlc/cache"; final String cacheDir = "/sdcard/Android/data/org.videolan.vlc/cache";
if (artworkURL != null && artworkURL.startsWith("file://")) { if (artworkURL != null && artworkURL.startsWith("file://")) {
return BitmapFactory.decodeFile(Uri.decode(artworkURL).replace("file://", "")); return BitmapFactory.decodeFile(Uri.decode(artworkURL).replace("file://", ""));
} else if(artworkURL != null && artworkURL.startsWith("attachment://")) { } else if(artworkURL != null && artworkURL.startsWith("attachment://")) {
// Decode if the album art is embedded in the file // Decode if the album art is embedded in the file
String mArtist = mCurrentMedia.getArtist(); String mArtist = media.getArtist();
String mAlbum = mCurrentMedia.getAlbum(); String mAlbum = media.getAlbum();
/* Parse decoded attachment */ /* Parse decoded attachment */
if( mArtist.length() == 0 || mAlbum.length() == 0 || if( mArtist.length() == 0 || mAlbum.length() == 0 ||
...@@ -639,7 +641,7 @@ public class AudioService extends Service { ...@@ -639,7 +641,7 @@ public class AudioService extends Service {
{ {
/* If artist or album are missing, it was cached by title MD5 hash */ /* If artist or album are missing, it was cached by title MD5 hash */
MessageDigest md = MessageDigest.getInstance("MD5"); MessageDigest md = MessageDigest.getInstance("MD5");
byte[] binHash = md.digest((artworkURL + mCurrentMedia.getTitle()).getBytes("UTF-8")); byte[] binHash = md.digest((artworkURL + media.getTitle()).getBytes("UTF-8"));
/* Convert binary hash to normal hash */ /* Convert binary hash to normal hash */
BigInteger hash = new BigInteger(1, binHash); BigInteger hash = new BigInteger(1, binHash);
String titleHash = hash.toString(16); String titleHash = hash.toString(16);
...@@ -653,20 +655,45 @@ public class AudioService extends Service { ...@@ -653,20 +655,45 @@ public class AudioService extends Service {
artworkURL = cacheDir + "/art/artistalbum/" + mArtist + "/" + mAlbum + "/art.png"; artworkURL = cacheDir + "/art/artistalbum/" + mArtist + "/" + mAlbum + "/art.png";
} }
Log.v(TAG, "artworkURL (calculated) = " + artworkURL);
return BitmapFactory.decodeFile(artworkURL); return BitmapFactory.decodeFile(artworkURL);
} }
return null;
}
//still no cover found, looking in folder ... private static Bitmap getCoverFromFolder(Context context, Media media) {
File f = Util.URItoFile(mCurrentMedia.getLocation()); File f = Util.URItoFile(media.getLocation());
for (File s : f.getParentFile().listFiles()) { for (File s : f.getParentFile().listFiles()) {
if (s.getAbsolutePath().endsWith("png") || if (s.getAbsolutePath().endsWith("png") ||
s.getAbsolutePath().endsWith("jpg")) s.getAbsolutePath().endsWith("jpg"))
return BitmapFactory.decodeFile(s.getAbsolutePath()); return BitmapFactory.decodeFile(s.getAbsolutePath());
} }
return null;
}
public static Bitmap getCover(Context context, Media media, int width) {
Bitmap cover = null;
try {
// try to get the cover from android MediaStore
cover = getCoverFromMediaStore(context, media);
//cover not in MediaStore, trying vlc
if (cover == null)
cover = getCoverFromVlc(context, media);
//still no cover found, looking in folder ...
if (cover == null)
cover = getCoverFromFolder(context, media);
//scale down if requested
if (cover != null && width > 0)
cover = Util.scaleDownBitmap(context, cover, width);
} catch (Exception e) { } catch (Exception e) {
} }
return null; return cover;
}
private Bitmap getCover() {
return getCover(this, mCurrentMedia, 0);
} }
private final IAudioService.Stub mInterface = new IAudioService.Stub() { private final IAudioService.Stub mInterface = new IAudioService.Stub() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment