Improve genre hack

This commit is contained in:
Adrian Ulrich 2015-11-08 13:22:09 +01:00
parent cb8debb2f4
commit e7a9bb74c0
2 changed files with 19 additions and 18 deletions

View File

@ -259,7 +259,7 @@ public class MediaAdapter
* @param forceMusicCheck Force the is_music check to be added to the * @param forceMusicCheck Force the is_music check to be added to the
* selection. * selection.
*/ */
private QueryTask buildQuery(String[] projection, boolean forceMusicCheck) private QueryTask buildQuery(String[] projection, boolean returnSongs)
{ {
String constraint = mConstraint; String constraint = mConstraint;
Limiter limiter = mLimiter; Limiter limiter = mLimiter;
@ -290,14 +290,17 @@ public class MediaAdapter
} }
sb.append(" ELSE 0 END %1s"); sb.append(" ELSE 0 END %1s");
sortStringRaw = sb.toString(); sortStringRaw = sb.toString();
} else if (returnSongs && mType != MediaUtils.TYPE_SONG) {
// We are in a non-song adapter but requested to return songs - sorting
// can only be done by using the adapters default sort mode :-(
sortStringRaw = mSongSort;
} }
String sort = String.format(sortStringRaw, sortDir); String sort = String.format(sortStringRaw, sortDir);
if (mType == MediaUtils.TYPE_SONG || forceMusicCheck) if (returnSongs || mType == MediaUtils.TYPE_SONG)
selection.append(MediaStore.Audio.Media.IS_MUSIC+" AND length(_data)"); selection.append(MediaStore.Audio.Media.IS_MUSIC+" AND length(_data)");
if (constraint != null && constraint.length() != 0) { if (constraint != null && constraint.length() != 0) {
String[] needles; String[] needles;
String[] keySource; String[] keySource;
@ -338,19 +341,22 @@ public class MediaAdapter
} }
} }
QueryTask query;
if (limiter != null && limiter.type == MediaUtils.TYPE_GENRE) { if (limiter != null && limiter.type == MediaUtils.TYPE_GENRE) {
// Genre is not standard metadata for MediaStore.Audio.Media. // Genre is not standard metadata for MediaStore.Audio.Media.
// We have to query it through a separate provider. : / // We have to query it through a separate provider. : /
return MediaUtils.buildGenreQuery((Long)limiter.data, projection, selection.toString(), selectionArgs, sort, mType); query = MediaUtils.buildGenreQuery((Long)limiter.data, projection, selection.toString(), selectionArgs, sort, mType, returnSongs);
} else { } else {
if (limiter != null) { if (limiter != null) {
if (selection.length() != 0) if (selection.length() != 0)
selection.append(" AND "); selection.append(" AND ");
selection.append(limiter.data); selection.append(limiter.data);
} }
query = new QueryTask(mStore, projection, selection.toString(), selectionArgs, sort);
return new QueryTask(mStore, projection, selection.toString(), selectionArgs, sort); if (returnSongs) // force query on song provider as we are requested to return songs
query.uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} }
return query;
} }
@Override @Override
@ -375,12 +381,6 @@ public class MediaAdapter
{ {
QueryTask query = buildQuery(projection, true); QueryTask query = buildQuery(projection, true);
query.type = mType; query.type = mType;
if (mType != MediaUtils.TYPE_SONG) {
query.uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Would be better to match the sort order in the adapter. This
// is likely to require significantly more work though.
query.sortOrder = mSongSort;
}
return query; return query;
} }

View File

@ -191,9 +191,10 @@ public class MediaUtils {
* @param selection The selection to pass to the query, or null. * @param selection The selection to pass to the query, or null.
* @param selectionArgs The arguments to substitute into the selection. * @param selectionArgs The arguments to substitute into the selection.
* @param sort The sort order. * @param sort The sort order.
* @param type The media type to return * @param type The media type to query and return
* @param returnSongs returns matching songs instead of `type' if true
*/ */
public static QueryTask buildGenreQuery(long id, String[] projection, String selection, String[] selectionArgs, String sort, int type) public static QueryTask buildGenreQuery(long id, String[] projection, String selection, String[] selectionArgs, String sort, int type, boolean returnSongs)
{ {
// Note: This function works on a raw sql query with way too much internal // Note: This function works on a raw sql query with way too much internal
// knowledge about the mediaProvider SQL table layout. Yes: it's ugly. // knowledge about the mediaProvider SQL table layout. Yes: it's ugly.
@ -219,7 +220,7 @@ public class MediaUtils {
// Prefix the SELECTed rows with the current table authority name // Prefix the SELECTed rows with the current table authority name
for (int i=0 ;i<clonedProjection.length; i++) { for (int i=0 ;i<clonedProjection.length; i++) {
if (clonedProjection[i].equals("0") == false) // do not prefix fake rows if (clonedProjection[i].equals("0") == false) // do not prefix fake rows
clonedProjection[i] = authority+"."+clonedProjection[i]; clonedProjection[i] = (returnSongs ? "audio" : authority)+"."+clonedProjection[i];
} }
sql += TextUtils.join(", ", clonedProjection); sql += TextUtils.join(", ", clonedProjection);
@ -230,10 +231,10 @@ public class MediaUtils {
sql += " AND("+selection.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2")+")"; sql += " AND("+selection.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2")+")";
if (type == TYPE_ARTIST) if (type == TYPE_ARTIST)
sql += " AND(artist_info._id = audio.artist_id) GROUP BY artist_info._id"; sql += " AND(artist_info._id = audio.artist_id)" + (returnSongs ? "" : " GROUP BY artist_info._id");
if (type == TYPE_ALBUM) if (type == TYPE_ALBUM)
sql += " AND(album_info._id = audio.album_id) GROUP BY album_info._id"; sql += " AND(album_info._id = audio.album_id)" + (returnSongs ? "" : " GROUP BY album_info._id");
if (sort != null && sort.length() > 0) if (sort != null && sort.length() > 0)
sql += " ORDER BY "+sort.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2"); sql += " ORDER BY "+sort.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2");
@ -266,7 +267,7 @@ public class MediaUtils {
case TYPE_PLAYLIST: case TYPE_PLAYLIST:
return buildPlaylistQuery(id, projection, selection); return buildPlaylistQuery(id, projection, selection);
case TYPE_GENRE: case TYPE_GENRE:
return buildGenreQuery(id, projection, selection, null, MediaStore.Audio.Genres.Members.TITLE_KEY, TYPE_SONG); return buildGenreQuery(id, projection, selection, null, MediaStore.Audio.Genres.Members.TITLE_KEY, TYPE_SONG, true);
default: default:
throw new IllegalArgumentException("Specified type not valid: " + type); throw new IllegalArgumentException("Specified type not valid: " + type);
} }