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 @@
package org.videolan.vlc;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -595,17 +597,14 @@ public class AudioService extends Service {
mRepeating = RepeatType.values()[t];
}
@SuppressLint("SdCardPath")
private Bitmap getCover() {
try {
// try to get the cover from android MediaStore
ContentResolver contentResolver = getContentResolver();
private static Bitmap getCoverFromMediaStore(Context context, Media media) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, new String[] {
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ALBUM_ART },
MediaStore.Audio.Albums.ALBUM + " LIKE ?",
new String[] { mCurrentMedia.getAlbum() }, null);
new String[] { media.getAlbum() }, null);
if (cursor == null) {
// do nothing
} else if (!cursor.moveToFirst()) {
......@@ -621,16 +620,19 @@ public class AudioService extends Service {
return b;
}
}
return null;
}
//cover not in MediaStore, trying vlc
String artworkURL = mCurrentMedia.getArtworkURL();
@SuppressLint("SdCardPath")
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";
if (artworkURL != null && artworkURL.startsWith("file://")) {
return BitmapFactory.decodeFile(Uri.decode(artworkURL).replace("file://", ""));
} else if(artworkURL != null && artworkURL.startsWith("attachment://")) {
// Decode if the album art is embedded in the file
String mArtist = mCurrentMedia.getArtist();
String mAlbum = mCurrentMedia.getAlbum();
String mArtist = media.getArtist();
String mAlbum = media.getAlbum();
/* Parse decoded attachment */
if( mArtist.length() == 0 || mAlbum.length() == 0 ||
......@@ -639,7 +641,7 @@ public class AudioService extends Service {
{
/* If artist or album are missing, it was cached by title MD5 hash */
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 */
BigInteger hash = new BigInteger(1, binHash);
String titleHash = hash.toString(16);
......@@ -653,20 +655,45 @@ public class AudioService extends Service {
artworkURL = cacheDir + "/art/artistalbum/" + mArtist + "/" + mAlbum + "/art.png";
}
Log.v(TAG, "artworkURL (calculated) = " + artworkURL);
return BitmapFactory.decodeFile(artworkURL);
}
return null;
}
//still no cover found, looking in folder ...
File f = Util.URItoFile(mCurrentMedia.getLocation());
private static Bitmap getCoverFromFolder(Context context, Media media) {
File f = Util.URItoFile(media.getLocation());
for (File s : f.getParentFile().listFiles()) {
if (s.getAbsolutePath().endsWith("png") ||
s.getAbsolutePath().endsWith("jpg"))
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) {
}
return null;
return cover;
}
private Bitmap getCover() {
return getCover(this, mCurrentMedia, 0);
}
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