support playing files missing from androids media database

This commit is contained in:
Adrian Ulrich 2014-10-12 15:09:02 +02:00
parent 4f69bc1feb
commit a0d49e9eda
4 changed files with 58 additions and 8 deletions

View File

@ -29,6 +29,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.zip.CRC32;
import junit.framework.Assert;
import android.content.ContentResolver;
@ -36,7 +38,10 @@ import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.database.MatrixCursor;
import ch.blinkenlights.bastp.Bastp;
/**
* Provides some static Song/MediaStore-related utility functions.
@ -545,10 +550,43 @@ public class MediaUtils {
final String query = "_data LIKE ? AND is_music";
String[] qargs = { path };
Log.d("VanillaMusic", "Running buildFileQuery for "+query+" with ARG="+qargs[0]);
Uri media = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
QueryTask result = new QueryTask(media, projection, query, qargs, DEFAULT_SORT);
result.type = TYPE_FILE;
return result;
}
/**
* Returns a (possibly empty) Cursor for given file path
* @param path The path to the file to be queried
* @return A new Cursor object
* */
public static Cursor getCursorForFileQuery(String path) {
MatrixCursor matrixCursor = new MatrixCursor(Song.FILLED_PROJECTION);
String[] keys = { "", "", "TITLE", "ALBUM", "ARTIST" };
HashMap tags = (new Bastp()).getTags(path);
if(tags.containsKey("TYPE")) { // File was parseable
/* This file is not in the media database but we still need
* to give it an unique id. We are going to use a negative
* ID calculated using the file path.
* This is not perfect as the same file may be accessed using
* multiple path variations - but getting the inode is not possible
* and hashing the file is out of question */
CRC32 crc = new CRC32();
crc.update(path.getBytes());
Long songId = (Long)(2+crc.getValue())*-1; // must at least be -2 (-1 defines Song-Object to be empty)
Object[] objData = new Object[] { songId, path, "", "", "", 0, 0, 600000, 0 };
for (int i=0; i<keys.length; i++) { // fill in values from BASTP
if (tags.containsKey(keys[i])) {
objData[i] = (String)((Vector)tags.get(keys[i])).get(0);
}
}
matrixCursor.addRow(objData);
}
return matrixCursor;
}
}

View File

@ -192,7 +192,7 @@ public class Song implements Comparable<Song> {
}
}
if (inputStream == null && (mCoverLoadMode & COVER_MODE_ANDROID) != 0) {
if (inputStream == null && (mCoverLoadMode & COVER_MODE_ANDROID) != 0 && key.id >= 0) {
Uri uri = Uri.parse("content://media/external/audio/media/" + key.id + "/albumart");
ContentResolver res = mContext.getContentResolver();
inputStream = res.openInputStream(uri);

View File

@ -616,15 +616,23 @@ public final class SongTimeline {
return 0;
}
int count = cursor.getCount();
if (count == 0) {
return 0;
}
int mode = query.mode;
int type = query.type;
long data = query.data;
int count = cursor.getCount();
if (count == 0 && type == MediaUtils.TYPE_FILE && query.selectionArgs.length == 1) {
String pathQuery = query.selectionArgs[0];
pathQuery = pathQuery.substring(0,pathQuery.length()-1); // remove '%' -> this used to be an sql query!
cursor = MediaUtils.getCursorForFileQuery(pathQuery);
count = cursor.getCount();
}
if (count == 0) {
return 0;
}
ArrayList<Song> timeline = mSongs;
synchronized (this) {
saveActiveSongs();

View File

@ -53,12 +53,15 @@ public class Bastp {
String magic = new String(file_ff);
if(magic.equals("fLaC")) {
tags = (new FlacFile()).getTags(s);
tags.put("TYPE", "FLAC");
}
else if(magic.equals("OggS")) {
tags = (new OggFile()).getTags(s);
tags.put("TYPE", "OGG");
}
else if(file_ff[0] == -1 && file_ff[1] == -5) { /* aka 0xfffb in real languages */
tags = (new LameHeader()).getTags(s);
tags.put("TYPE", "MP3");
}
else if(magic.substring(0,3).equals("ID3")) {
tags = (new ID3v2File()).getTags(s);
@ -69,6 +72,7 @@ public class Bastp {
inheritTag("REPLAYGAIN_TRACK_GAIN", lameInfo, tags);
inheritTag("REPLAYGAIN_ALBUM_GAIN", lameInfo, tags);
}
tags.put("TYPE", "MP3-ID3");
}
tags.put("_magic", magic);
}