Consolidate the adapters into one MediaAdapter class

This commit is contained in:
Christopher Eby 2010-03-10 23:04:49 -06:00
parent fe5ff05bad
commit 568c5be403
6 changed files with 36 additions and 135 deletions

View File

@ -1,19 +0,0 @@
package org.kreed.vanilla;
import android.content.Context;
public class AlbumAdapter extends AbstractAdapter {
public AlbumAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.AlbumComparator()), MediaView.EXPANDER | MediaView.SECONDARY_LINE, Song.FIELD_ALBUM);
}
@Override
protected void updateView(int position, MediaView view)
{
Song song = get(position);
view.setPrimaryText(song.album);
view.setSecondaryText(song.artist);
view.setMediaId(song.albumId);
}
}

View File

@ -1,36 +0,0 @@
/*
* 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;
public class ArtistAdapter extends AbstractAdapter {
public ArtistAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.ArtistComparator()), MediaView.EXPANDER, Song.FIELD_ARTIST);
}
@Override
protected void updateView(int position, MediaView view)
{
Song song = get(position);
view.setPrimaryText(song.artist);
view.setMediaId(song.artistId);
}
}

View File

@ -33,7 +33,7 @@ import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
public abstract class AbstractAdapter extends BaseAdapter implements Filterable {
public class MediaAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private OnClickListener mExpanderListener;
@ -44,15 +44,15 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
private int mLimiterId = -1;
private CharSequence mLastFilter;
private int mDrawFlags;
private int mMediaField;
private int mPrimaryField;
private int mSecondaryField;
public AbstractAdapter(Context context, Song[] allObjects, int drawFlags, int mediaField)
public MediaAdapter(Context context, Song[] allObjects, int primaryField, int secondaryField)
{
mContext = context;
mAllObjects = allObjects;
mDrawFlags = drawFlags;
mMediaField = mediaField;
mPrimaryField = primaryField;
mSecondaryField = secondaryField;
}
public void setExpanderListener(View.OnClickListener listener)
@ -66,8 +66,6 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
return true;
}
protected abstract void updateView(int position, MediaView view);
public View getView(int position, View convertView, ViewGroup parent)
{
MediaView view = null;
@ -77,13 +75,19 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
}
if (view == null) {
view = new MediaView(mContext, mDrawFlags);
int flags = 0;
if (mSecondaryField != -1)
flags |= MediaView.SECONDARY_LINE;
if (mExpanderListener != null)
flags |= MediaView.EXPANDER;
view = new MediaView(mContext, flags);
if (mExpanderListener != null)
view.setupExpander(mMediaField, mExpanderListener);
view.setupExpander(mPrimaryField, mExpanderListener);
}
updateView(position, view);
view.updateMedia(get(position), mPrimaryField, mSecondaryField);
return view;
}
@ -146,9 +150,9 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
for (int j = matchers.length; --j != -1; ) {
if (matchers[j].reset(song.artist).find())
continue;
if (mMediaField > 1 && matchers[j].reset(song.album).find())
if (mPrimaryField > 1 && matchers[j].reset(song.album).find())
continue;
if (mMediaField > 2 && matchers[j].reset(song.title).find())
if (mPrimaryField > 2 && matchers[j].reset(song.title).find())
continue;
continue outer;
}
@ -222,7 +226,7 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
Song song = get(i);
if (song == null)
return 0;
return song.getFieldId(mMediaField);
return song.getFieldId(mPrimaryField);
}
public Intent buildSongIntent(int action, int pos)
@ -232,10 +236,10 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
return null;
Intent intent = new Intent(mContext, PlaybackService.class);
intent.putExtra("type", mMediaField);
intent.putExtra("type", mPrimaryField);
intent.putExtra("action", action);
intent.putExtra("id", song.getFieldId(mMediaField));
intent.putExtra("title", song.getField(mMediaField));
intent.putExtra("id", song.getFieldId(mPrimaryField));
intent.putExtra("title", song.getField(mPrimaryField));
return intent;
}
}

View File

@ -133,21 +133,14 @@ public class MediaView extends ViewGroup {
mExpander.setOnClickListener(listener);
}
}
public void setPrimaryText(String text)
public void updateMedia(Song song, int primaryField, int secondaryField)
{
if (mPrimaryLine != null)
mPrimaryLine.setText(text);
}
public void setSecondaryText(String text)
{
mPrimaryLine.setText(song.getField(primaryField));
if (mSecondaryLine != null)
mSecondaryLine.setText(text);
}
public void setMediaId(int id)
{
mSecondaryLine.setText(song.getField(secondaryField));
if (mExpander != null)
mExpander.setTag(R.id.id, id);
mExpander.setTag(R.id.id, song.getFieldId(primaryField));
}
}

View File

@ -1,44 +0,0 @@
/*
* 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 java.util.Arrays;
import android.content.Context;
public class SongAdapter extends AbstractAdapter {
private static Song[] sort(Song[] songs)
{
Arrays.sort(songs, new Song.TitleComparator());
return songs;
}
public SongAdapter(Context context, Song[] allSongs)
{
super(ContextApplication.getContext(), sort(allSongs), MediaView.SECONDARY_LINE, Song.FIELD_TITLE);
}
@Override
protected void updateView(int position, MediaView view)
{
Song song = get(position);
view.setPrimaryText(song.title);
view.setSecondaryText(song.artist);
}
}

View File

@ -18,6 +18,8 @@
package org.kreed.vanilla;
import java.util.Arrays;
import org.kreed.vanilla.R;
import android.app.TabActivity;
@ -46,7 +48,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
private TabHost mTabHost;
private TextView mTextFilter;
private View mClearButton;
private AbstractAdapter[] mAdapters = new AbstractAdapter[3];
private MediaAdapter[] mAdapters = new MediaAdapter[3];
private int mDefaultAction;
@ -75,7 +77,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
mTabHost.addTab(mTabHost.newTabSpec("tab_songs").setIndicator(res.getText(R.string.songs), res.getDrawable(R.drawable.tab_songs)).setContent(R.id.song_list));
final Song[] songs = Song.getAllSongMetadata();
mAdapters[0] = new ArtistAdapter(this, songs);
mAdapters[0] = new MediaAdapter(this, Song.filter(songs, new Song.ArtistComparator()), Song.FIELD_ARTIST, -1);
mAdapters[0].setExpanderListener(this);
initializeListView(R.id.artist_list, mAdapters[0]);
@ -101,11 +103,12 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
new Handler().post(new Runnable() {
public void run()
{
mAdapters[1] = new AlbumAdapter(SongSelector.this, songs);
mAdapters[1] = new MediaAdapter(SongSelector.this, Song.filter(songs, new Song.AlbumComparator()), Song.FIELD_ALBUM, Song.FIELD_ARTIST);
mAdapters[1].setExpanderListener(SongSelector.this);
initializeListView(R.id.album_list, mAdapters[1]);
mAdapters[2] = new SongAdapter(SongSelector.this, songs);
Arrays.sort(songs, new Song.TitleComparator());
mAdapters[2] = new MediaAdapter(SongSelector.this, songs, Song.FIELD_TITLE, Song.FIELD_ARTIST);
initializeListView(R.id.song_list, mAdapters[2]);
}
});
@ -134,7 +137,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
public void onItemClick(AdapterView<?> list, View view, int pos, long id)
{
sendSongIntent(((AbstractAdapter)list.getAdapter()).buildSongIntent(mDefaultAction, pos));
sendSongIntent(((MediaAdapter)list.getAdapter()).buildSongIntent(mDefaultAction, pos));
}
public void afterTextChanged(Editable editable)
@ -147,7 +150,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
public void onTextChanged(CharSequence s, int start, int before, int count)
{
AbstractAdapter adapter = mAdapters[mTabHost.getCurrentTab()];
MediaAdapter adapter = mAdapters[mTabHost.getCurrentTab()];
if (adapter != null)
adapter.getFilter().filter(s);
}
@ -174,7 +177,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info)
{
AbstractAdapter adapter = (AbstractAdapter)((ListView)view).getAdapter();
MediaAdapter adapter = (MediaAdapter)((ListView)view).getAdapter();
int pos = (int)((AdapterView.AdapterContextMenuInfo)info).position;
menu.add(0, MENU_PLAY, 0, R.string.play).setIntent(adapter.buildSongIntent(PlaybackService.ACTION_PLAY, pos));
menu.add(0, MENU_ENQUEUE, 0, R.string.enqueue).setIntent(adapter.buildSongIntent(PlaybackService.ACTION_ENQUEUE, pos));