Subclass MediaAdapter for songs

Override the sort order when limiting by album to sort by track number
This commit is contained in:
Christopher Eby 2010-04-03 10:59:04 -05:00
parent f5f86018ec
commit 8f76a2be25
3 changed files with 67 additions and 11 deletions

View File

@ -50,11 +50,10 @@ public class MediaAdapter extends CursorAdapter implements FilterQueryProvider {
private String[] mFields;
private String[] mFieldKeys;
private View.OnClickListener mExpanderListener;
private String mSelection;
private String[] mLimiter;
private CharSequence mConstraint;
public MediaAdapter(Context context, Uri store, String[] fields, String[] fieldKeys, View.OnClickListener expanderListener, String selection)
public MediaAdapter(Context context, Uri store, String[] fields, String[] fieldKeys, View.OnClickListener expanderListener)
{
super(context, null, true);
@ -62,7 +61,6 @@ public class MediaAdapter extends CursorAdapter implements FilterQueryProvider {
mFields = fields;
mFieldKeys = fieldKeys;
mExpanderListener = expanderListener;
mSelection = selection;
setFilterQueryProvider(this);
@ -75,6 +73,16 @@ public class MediaAdapter extends CursorAdapter implements FilterQueryProvider {
getFilter().filter(constraint, listener);
}
protected String getDefaultSelection()
{
return null;
}
protected String getSortOrder()
{
return mFieldKeys[mFieldKeys.length - 1];
}
public Cursor runQuery(CharSequence constraint)
{
ContentResolver resolver = ContextApplication.getContext().getContentResolver();
@ -83,8 +91,9 @@ public class MediaAdapter extends CursorAdapter implements FilterQueryProvider {
String[] selectionArgs;
String limiter;
if (mSelection != null)
selection.append(mSelection);
String defaultSelection = getDefaultSelection();
if (defaultSelection != null)
selection.append(defaultSelection);
if (mLimiter != null) {
int i = Math.min(mLimiter.length, mFields.length) - 1;
@ -132,7 +141,7 @@ public class MediaAdapter extends CursorAdapter implements FilterQueryProvider {
else
projection = new String[] { BaseColumns._ID, mFields[mFields.length - 1], mFields[0] };
return resolver.query(mStore, projection, selection.toString(), selectionArgs, mFieldKeys[mFieldKeys.length - 1]);
return resolver.query(mStore, projection, selection.toString(), selectionArgs, getSortOrder());
}
public final boolean hasExpanders()

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kreed.vanilla;
import android.content.Context;
import android.provider.MediaStore;
public class SongMediaAdapter extends MediaAdapter {
public SongMediaAdapter(Context context)
{
super(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, SONG_FIELDS, SONG_FIELD_KEYS, null);
}
@Override
protected String getDefaultSelection()
{
return MediaStore.Audio.Media.IS_MUSIC + "!=0";
}
@Override
protected String getSortOrder()
{
if (getLimiter() != null && getLimiter().length == 2)
return null; // use default sort order (by track)
return super.getSortOrder();
}
}

View File

@ -71,12 +71,12 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
return (MediaAdapter)getList(tab).getAdapter();
}
private void initializeList(int id, Uri store, String[] fields, String[] fieldKeys, View.OnClickListener expanderListener, String selection)
private void initializeList(int id, Uri store, String[] fields, String[] fieldKeys, View.OnClickListener expanderListener)
{
ListView view = (ListView)findViewById(id);
view.setOnItemClickListener(this);
view.setOnCreateContextMenuListener(this);
view.setAdapter(new MediaAdapter(getContext(), store, fields, fieldKeys, expanderListener, selection));
view.setAdapter(new MediaAdapter(getContext(), store, fields, fieldKeys, expanderListener));
}
public SongSelector(Context context)
@ -109,9 +109,13 @@ public class SongSelector extends Dialog implements AdapterView.OnItemClickListe
new Handler().post(new Runnable() {
public void run()
{
initializeList(R.id.artist_list, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, MediaAdapter.ARTIST_FIELDS, MediaAdapter.ARTIST_FIELD_KEYS, SongSelector.this, null);
initializeList(R.id.album_list, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, MediaAdapter.ALBUM_FIELDS, MediaAdapter.ALBUM_FIELD_KEYS,SongSelector.this, null);
initializeList(R.id.song_list, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MediaAdapter.SONG_FIELDS, MediaAdapter.SONG_FIELD_KEYS, null, MediaStore.Audio.Media.IS_MUSIC + "!=0");
initializeList(R.id.artist_list, MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, MediaAdapter.ARTIST_FIELDS, MediaAdapter.ARTIST_FIELD_KEYS, SongSelector.this);
initializeList(R.id.album_list, MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, MediaAdapter.ALBUM_FIELDS, MediaAdapter.ALBUM_FIELD_KEYS,SongSelector.this);
ListView view = (ListView)findViewById(R.id.song_list);
view.setOnItemClickListener(SongSelector.this);
view.setOnCreateContextMenuListener(SongSelector.this);
view.setAdapter(new SongMediaAdapter(getContext()));
}
});
}