diff --git a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java index 5752d57e..37ff9e96 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaAdapter.java +++ b/src/ch/blinkenlights/android/vanilla/MediaAdapter.java @@ -259,7 +259,7 @@ public class MediaAdapter * @param forceMusicCheck Force the is_music check to be added to the * selection. */ - private QueryTask buildQuery(String[] projection, boolean forceMusicCheck) + private QueryTask buildQuery(String[] projection, boolean returnSongs) { String constraint = mConstraint; Limiter limiter = mLimiter; @@ -290,14 +290,17 @@ public class MediaAdapter } sb.append(" ELSE 0 END %1s"); 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); - if (mType == MediaUtils.TYPE_SONG || forceMusicCheck) + if (returnSongs || mType == MediaUtils.TYPE_SONG) selection.append(MediaStore.Audio.Media.IS_MUSIC+" AND length(_data)"); - if (constraint != null && constraint.length() != 0) { String[] needles; String[] keySource; @@ -338,19 +341,22 @@ public class MediaAdapter } } + QueryTask query; 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); + query = MediaUtils.buildGenreQuery((Long)limiter.data, projection, selection.toString(), selectionArgs, sort, mType, returnSongs); } else { if (limiter != null) { if (selection.length() != 0) selection.append(" AND "); selection.append(limiter.data); } - - return new QueryTask(mStore, projection, selection.toString(), selectionArgs, sort); + query = 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 @@ -375,12 +381,6 @@ public class MediaAdapter { QueryTask query = buildQuery(projection, true); 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; } diff --git a/src/ch/blinkenlights/android/vanilla/MediaUtils.java b/src/ch/blinkenlights/android/vanilla/MediaUtils.java index 35b85e2c..da494ed5 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaUtils.java +++ b/src/ch/blinkenlights/android/vanilla/MediaUtils.java @@ -191,9 +191,10 @@ 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 + * @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 // 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 for (int i=0 ;i 0) sql += " ORDER BY "+sort.replaceAll(_FORCE_AUDIO_SRC, "$1audio.$2"); @@ -266,7 +267,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, TYPE_SONG); + return buildGenreQuery(id, projection, selection, null, MediaStore.Audio.Genres.Members.TITLE_KEY, TYPE_SONG, true); default: throw new IllegalArgumentException("Specified type not valid: " + type); }