From f49d71f6888efcecceffbac02df6264bddfe11df Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 2 Oct 2011 00:57:07 -0500 Subject: [PATCH] Use the correct sort order when using play all --- src/org/kreed/vanilla/LibraryActivity.java | 8 +--- src/org/kreed/vanilla/MediaAdapter.java | 49 ++++++++++++++++++---- src/org/kreed/vanilla/MediaUtils.java | 9 +++- src/org/kreed/vanilla/PlaybackService.java | 8 +--- src/org/kreed/vanilla/QueryTask.java | 18 ++++++++ 5 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/org/kreed/vanilla/LibraryActivity.java b/src/org/kreed/vanilla/LibraryActivity.java index b37ebc4f..f142b2a8 100644 --- a/src/org/kreed/vanilla/LibraryActivity.java +++ b/src/org/kreed/vanilla/LibraryActivity.java @@ -479,11 +479,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI QueryTask query; if (intent.getBooleanExtra("isHeader", false)) { - query = mAdapters[type - 1].buildQuery(true); - query.setProjection(projection); - // we want to query songs, not albums or artists - if (type != MediaUtils.TYPE_SONG) - query.setUri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); + query = mAdapters[type - 1].buildSongQuery(projection); } else { long id = intent.getLongExtra("id", -1); query = MediaUtils.buildQuery(type, id, projection, null); @@ -737,7 +733,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI } case MSG_RUN_QUERY: { final MediaAdapter adapter = (MediaAdapter)message.obj; - QueryTask query = adapter.buildQuery(false); + QueryTask query = adapter.buildQuery(); final Cursor cursor = query.runQuery(getContentResolver()); runOnUiThread(new Runnable() { @Override diff --git a/src/org/kreed/vanilla/MediaAdapter.java b/src/org/kreed/vanilla/MediaAdapter.java index fec78fb4..075128ba 100644 --- a/src/org/kreed/vanilla/MediaAdapter.java +++ b/src/org/kreed/vanilla/MediaAdapter.java @@ -71,6 +71,10 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { * used to speed up sorting and filtering. */ private String[] mFieldKeys; + /** + * The columns to query from the content provider. + */ + private String[] mProjection; /** * If true, show an expand arrow next the the text in each view. */ @@ -98,6 +102,10 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { * The text to show in the header. */ private String mHeaderText; + /** + * The sort order for use with buildSongQuery(). + */ + private String mSongSort; /** * Construct a MediaAdapter representing the given type of @@ -128,12 +136,14 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { mStore = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI; mFields = new String[] { MediaStore.Audio.Artists.ARTIST }; mFieldKeys = new String[] { MediaStore.Audio.Artists.ARTIST_KEY }; + mSongSort = MediaUtils.DEFAULT_SORT; break; case MediaUtils.TYPE_ALBUM: mStore = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; mFields = new String[] { MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Albums.ALBUM }; // Why is there no artist_key column constant in the album MediaStore? The column does seem to exist. mFieldKeys = new String[] { "artist_key", MediaStore.Audio.Albums.ALBUM_KEY }; + mSongSort = "album_key,track"; break; case MediaUtils.TYPE_SONG: mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; @@ -153,6 +163,11 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { default: throw new IllegalArgumentException("Invalid value for type: " + type); } + + if (mFields.length == 1) + mProjection = new String[] { BaseColumns._ID, mFields[0] }; + else + mProjection = new String[] { BaseColumns._ID, mFields[mFields.length - 1], mFields[0] }; } @Override @@ -240,20 +255,15 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { /** * Build the query to be run with runQuery(). * + * @param projection The columns to query. * @param forceMusicCheck Force the is_music check to be added to the * selection. */ - public QueryTask buildQuery(boolean forceMusicCheck) + public QueryTask buildQuery(String[] projection, boolean forceMusicCheck) { String constraint = mConstraint; Limiter limiter = mLimiter; - String[] projection; - if (mFields.length == 1) - projection = new String[] { BaseColumns._ID, mFields[0] }; - else - projection = new String[] { BaseColumns._ID, mFields[mFields.length - 1], mFields[0] }; - StringBuilder selection = new StringBuilder(); String[] selectionArgs = null; @@ -318,6 +328,31 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer { } } + /** + * Build a query to populate the adapter with. The result should be set with + * changeCursor(). + */ + public QueryTask buildQuery() + { + return buildQuery(mProjection, false); + } + + /** + * Build a query for all the songs represented by this adapter, for adding + * to the timeline. + * + * @param projection The columns to query. + */ + public QueryTask buildSongQuery(String[] projection) + { + QueryTask query = buildQuery(projection, true); + if (mType != MediaUtils.TYPE_SONG) { + query.setUri(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); + query.setSortOrder(mSongSort); + } + return query; + } + /** * Return the type of media represented by this adapter. One of * MediaUtils.TYPE_*. diff --git a/src/org/kreed/vanilla/MediaUtils.java b/src/org/kreed/vanilla/MediaUtils.java index cd9ce806..ca9b0403 100644 --- a/src/org/kreed/vanilla/MediaUtils.java +++ b/src/org/kreed/vanilla/MediaUtils.java @@ -53,6 +53,12 @@ public class MediaUtils { */ public static final int TYPE_GENRE = 5; + /** + * The default sort order for media queries. First artist, then album, then + * track number. + */ + public static final String DEFAULT_SORT = "artist_key,album_key,track"; + /** * Cached random instance. */ @@ -125,8 +131,7 @@ public class MediaUtils { selection.append(select); } - String sort = MediaStore.Audio.Media.ARTIST_KEY + ',' + MediaStore.Audio.Media.ALBUM_KEY + ',' + MediaStore.Audio.Media.TRACK; - return new QueryTask(media, projection, selection.toString(), null, sort); + return new QueryTask(media, projection, selection.toString(), null, DEFAULT_SORT); } /** diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 2cf4278f..c50fbc8f 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -724,9 +724,7 @@ public final class PlaybackService extends Service implements Handler.Callback, mHandler.removeMessages(PROCESS_SONG); mHandler.sendMessage(mHandler.obtainMessage(PROCESS_SONG, song)); - Message msg = mHandler.obtainMessage(BROADCAST_CHANGE, -1, 0); - msg.obj = song; - mHandler.sendMessage(msg); + mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_CHANGE, -1, 0, song)); return song; } @@ -1145,9 +1143,7 @@ public final class PlaybackService extends Service implements Handler.Callback, */ public void addSongs(int mode, QueryTask query) { - Message msg = mHandler.obtainMessage(QUERY, query); - msg.arg1 = mode; - mHandler.sendMessage(msg); + mHandler.sendMessage(mHandler.obtainMessage(QUERY, mode, 0, query)); } /** diff --git a/src/org/kreed/vanilla/QueryTask.java b/src/org/kreed/vanilla/QueryTask.java index 67833e17..0feca4c8 100644 --- a/src/org/kreed/vanilla/QueryTask.java +++ b/src/org/kreed/vanilla/QueryTask.java @@ -69,6 +69,24 @@ public class QueryTask { mProjection = projection; } + /** + * Return the sort order of the pending query. + */ + public String getSortOrder() + { + return mSortOrder; + } + + /** + * Modify the sort order of the pending query. + * + * @param sortOrder The new sort order. + */ + public void setSortOrder(String sortOrder) + { + mSortOrder = sortOrder; + } + /** * Run the query. Should be called on a background thread. *