Use the correct sort order when using play all

This commit is contained in:
Christopher Eby 2011-10-02 00:57:07 -05:00
parent fa1c9d23eb
commit f49d71f688
5 changed files with 71 additions and 21 deletions

View File

@ -479,11 +479,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
QueryTask query; QueryTask query;
if (intent.getBooleanExtra("isHeader", false)) { if (intent.getBooleanExtra("isHeader", false)) {
query = mAdapters[type - 1].buildQuery(true); query = mAdapters[type - 1].buildSongQuery(projection);
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);
} else { } else {
long id = intent.getLongExtra("id", -1); long id = intent.getLongExtra("id", -1);
query = MediaUtils.buildQuery(type, id, projection, null); query = MediaUtils.buildQuery(type, id, projection, null);
@ -737,7 +733,7 @@ public class LibraryActivity extends PlaybackActivity implements AdapterView.OnI
} }
case MSG_RUN_QUERY: { case MSG_RUN_QUERY: {
final MediaAdapter adapter = (MediaAdapter)message.obj; final MediaAdapter adapter = (MediaAdapter)message.obj;
QueryTask query = adapter.buildQuery(false); QueryTask query = adapter.buildQuery();
final Cursor cursor = query.runQuery(getContentResolver()); final Cursor cursor = query.runQuery(getContentResolver());
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override

View File

@ -71,6 +71,10 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
* used to speed up sorting and filtering. * used to speed up sorting and filtering.
*/ */
private String[] mFieldKeys; 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. * 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. * The text to show in the header.
*/ */
private String mHeaderText; private String mHeaderText;
/**
* The sort order for use with buildSongQuery().
*/
private String mSongSort;
/** /**
* Construct a MediaAdapter representing the given <code>type</code> of * Construct a MediaAdapter representing the given <code>type</code> of
@ -128,12 +136,14 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
mStore = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI; mStore = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
mFields = new String[] { MediaStore.Audio.Artists.ARTIST }; mFields = new String[] { MediaStore.Audio.Artists.ARTIST };
mFieldKeys = new String[] { MediaStore.Audio.Artists.ARTIST_KEY }; mFieldKeys = new String[] { MediaStore.Audio.Artists.ARTIST_KEY };
mSongSort = MediaUtils.DEFAULT_SORT;
break; break;
case MediaUtils.TYPE_ALBUM: case MediaUtils.TYPE_ALBUM:
mStore = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI; mStore = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
mFields = new String[] { MediaStore.Audio.Albums.ARTIST, MediaStore.Audio.Albums.ALBUM }; 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. // 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 }; mFieldKeys = new String[] { "artist_key", MediaStore.Audio.Albums.ALBUM_KEY };
mSongSort = "album_key,track";
break; break;
case MediaUtils.TYPE_SONG: case MediaUtils.TYPE_SONG:
mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; mStore = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
@ -153,6 +163,11 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
default: default:
throw new IllegalArgumentException("Invalid value for type: " + type); 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 @Override
@ -240,20 +255,15 @@ public class MediaAdapter extends CursorAdapter implements SectionIndexer {
/** /**
* Build the query to be run with runQuery(). * 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 * @param forceMusicCheck Force the is_music check to be added to the
* selection. * selection.
*/ */
public QueryTask buildQuery(boolean forceMusicCheck) public QueryTask buildQuery(String[] projection, boolean forceMusicCheck)
{ {
String constraint = mConstraint; String constraint = mConstraint;
Limiter limiter = mLimiter; 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(); StringBuilder selection = new StringBuilder();
String[] selectionArgs = null; 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 * Return the type of media represented by this adapter. One of
* MediaUtils.TYPE_*. * MediaUtils.TYPE_*.

View File

@ -53,6 +53,12 @@ public class MediaUtils {
*/ */
public static final int TYPE_GENRE = 5; 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. * Cached random instance.
*/ */
@ -125,8 +131,7 @@ public class MediaUtils {
selection.append(select); 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, DEFAULT_SORT);
return new QueryTask(media, projection, selection.toString(), null, sort);
} }
/** /**

View File

@ -724,9 +724,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
mHandler.removeMessages(PROCESS_SONG); mHandler.removeMessages(PROCESS_SONG);
mHandler.sendMessage(mHandler.obtainMessage(PROCESS_SONG, song)); mHandler.sendMessage(mHandler.obtainMessage(PROCESS_SONG, song));
Message msg = mHandler.obtainMessage(BROADCAST_CHANGE, -1, 0); mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_CHANGE, -1, 0, song));
msg.obj = song;
mHandler.sendMessage(msg);
return song; return song;
} }
@ -1145,9 +1143,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
*/ */
public void addSongs(int mode, QueryTask query) public void addSongs(int mode, QueryTask query)
{ {
Message msg = mHandler.obtainMessage(QUERY, query); mHandler.sendMessage(mHandler.obtainMessage(QUERY, mode, 0, query));
msg.arg1 = mode;
mHandler.sendMessage(msg);
} }
/** /**

View File

@ -69,6 +69,24 @@ public class QueryTask {
mProjection = projection; 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. * Run the query. Should be called on a background thread.
* *