From cb02b71bf4834415af51910c899cccdbc67c9bf6 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sat, 7 Nov 2015 11:21:13 +0100 Subject: [PATCH] Ability to filter artists by genre --- .../android/vanilla/MediaAdapter.java | 2 +- .../android/vanilla/MediaUtils.java | 26 +++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java index d78a5994..5752d57e 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -341,7 +341,7 @@ public class MediaAdapter if (limiter != null && limiter.type == MediaUtils.TYPE_GENRE) { // Genre is not standard metadata for MediaStore.Audio.Media. // We have to query it through a separate provider. : / - return MediaUtils.buildGenreQuery((Long)limiter.data, projection, selection.toString(), selectionArgs, sort, mType == MediaUtils.TYPE_ALBUM); + return MediaUtils.buildGenreQuery((Long)limiter.data, projection, selection.toString(), selectionArgs, sort, mType); } else { if (limiter != null) { if (selection.length() != 0) diff --git a/src/ch/blinkenlights/android/vanilla/MediaUtils.java b/src/ch/blinkenlights/android/vanilla/MediaUtils.java index 6cbfe9bd..35b85e2c 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaUtils.java +++ b/src/ch/blinkenlights/android/vanilla/MediaUtils.java @@ -191,8 +191,9 @@ public class MediaUtils { * @param selection The selection to pass to the query, or null. * @param selectionArgs The arguments to substitute into the selection. * @param sort The sort order. + * @param type The media type to return */ - public static QueryTask buildGenreQuery(long id, String[] projection, String selection, String[] selectionArgs, String sort, boolean returnAlbums) + public static QueryTask buildGenreQuery(long id, String[] projection, String selection, String[] selectionArgs, String sort, int type) { // 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. @@ -202,7 +203,12 @@ public class MediaUtils { Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external", id); String[] clonedProjection = projection.clone(); // we modify the projection, but this should not be visible to the caller String sql = ""; - String authority = (returnAlbums ? "album_info" : "audio"); + String authority = "audio"; + + if (type == TYPE_ARTIST) + authority = "artist_info"; + if (type == TYPE_ALBUM) + authority = "album_info"; // Our raw SQL query includes the album_info table (well: it's actually a view) // which shares some columns with audio. @@ -212,18 +218,22 @@ public class MediaUtils { // Prefix the SELECTed rows with the current table authority name for (int i=0 ;i 0) sql += " AND("+selection.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2")+")"; - if (returnAlbums) - sql += " GROUP BY album_info._id"; + if (type == TYPE_ARTIST) + sql += " AND(artist_info._id = audio.artist_id) GROUP BY artist_info._id"; + + if (type == TYPE_ALBUM) + sql += " AND(album_info._id = audio.album_id) GROUP BY album_info._id"; if (sort != null && sort.length() > 0) sql += " ORDER BY "+sort.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2"); @@ -256,7 +266,7 @@ public class MediaUtils { case TYPE_PLAYLIST: return buildPlaylistQuery(id, projection, selection); case TYPE_GENRE: - return buildGenreQuery(id, projection, selection, null, MediaStore.Audio.Genres.Members.TITLE_KEY, false); + return buildGenreQuery(id, projection, selection, null, MediaStore.Audio.Genres.Members.TITLE_KEY, TYPE_SONG); default: throw new IllegalArgumentException("Specified type not valid: " + type); }