Rework adapter setup

Moves Toast code into SongSelector and media field code into Song and builds
media intents in AbstractAdapter
This commit is contained in:
Christopher Eby 2010-03-09 23:08:06 -06:00
parent da087bd3ac
commit 1cc008181e
7 changed files with 99 additions and 111 deletions

View File

@ -25,6 +25,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.TypedValue;
@ -48,15 +49,15 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
private float mSize;
private int mPadding;
private int mDrawFlags;
private int mFieldCount;
private int mMediaField;
private OnClickListener mExpanderListener;
public AbstractAdapter(Context context, Song[] allObjects, int drawFlags, int numFields)
public AbstractAdapter(Context context, Song[] allObjects, int drawFlags, int mediaField)
{
mContext = context;
mAllObjects = allObjects;
mDrawFlags = drawFlags;
mFieldCount = numFields;
mMediaField = mediaField;
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, metrics);
@ -99,7 +100,7 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
button.setImageResource(R.drawable.expander_arrow);
button.setId(3);
button.setLayoutParams(params);
button.setTag(R.id.list, mFieldCount);
button.setTag(R.id.list, mMediaField);
button.setTag(R.id.song, get(position));
button.setOnClickListener(mExpanderListener);
@ -187,9 +188,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 (mFieldCount > 1 && matchers[j].reset(song.album).find())
if (mMediaField > 1 && matchers[j].reset(song.album).find())
continue;
if (mFieldCount > 2 && matchers[j].reset(song.title).find())
if (mMediaField > 2 && matchers[j].reset(song.title).find())
continue;
continue outer;
}
@ -244,4 +245,26 @@ public abstract class AbstractAdapter extends BaseAdapter implements Filterable
{
return get(i);
}
public long getItemId(int i)
{
Song song = get(i);
if (song == null)
return 0;
return song.getFieldId(mMediaField);
}
public Intent buildSongIntent(int action, int pos)
{
Song song = get(pos);
if (song == null)
return null;
Intent intent = new Intent(mContext, PlaybackService.class);
intent.putExtra("type", mMediaField);
intent.putExtra("action", action);
intent.putExtra("id", song.getFieldId(mMediaField));
intent.putExtra("title", song.getField(mMediaField));
return intent;
}
}

View File

@ -6,7 +6,7 @@ import android.widget.TextView;
public class AlbumAdapter extends AbstractAdapter {
public AlbumAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.AlbumComparator()), 0, 2);
super(context, Song.filter(allSongs, new Song.AlbumComparator()), 0, Song.FIELD_ALBUM);
}
@Override
@ -16,12 +16,4 @@ public class AlbumAdapter extends AbstractAdapter {
upper.setText(song.album);
lower.setText(song.artist);
}
public long getItemId(int i)
{
Song song = get(i);
if (song == null)
return 0;
return song.albumId;
}
}

View File

@ -24,7 +24,7 @@ import android.widget.TextView;
public class ArtistAdapter extends AbstractAdapter {
public ArtistAdapter(Context context, Song[] allSongs)
{
super(context, Song.filter(allSongs, new Song.ArtistComparator()), ONE_LINE, 1);
super(context, Song.filter(allSongs, new Song.ArtistComparator()), ONE_LINE, Song.FIELD_ARTIST);
}
@Override
@ -33,12 +33,4 @@ public class ArtistAdapter extends AbstractAdapter {
Song song = get(position);
upper.setText(song.artist);
}
public long getItemId(int i)
{
Song song = get(i);
if (song == null)
return 0;
return song.artistId;
}
}

View File

@ -49,12 +49,10 @@ import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class PlaybackService extends Service implements Runnable, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener {
private static final int NOTIFICATION_ID = 2;
@ -70,10 +68,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
public static final int ACTION_PLAY = 0;
public static final int ACTION_ENQUEUE = 1;
public static final int TYPE_SONG = 0;
public static final int TYPE_ALBUM = 1;
public static final int TYPE_ARTIST = 2;
public static final int STATE_NORMAL = 0;
public static final int STATE_NO_MEDIA = 1;
public static final int STATE_PLAYING = 2;
@ -722,22 +716,10 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
if (id == -1) {
mQueuePos = 0;
} else {
int type = intent.getIntExtra("type", TYPE_SONG);
boolean enqueue = intent.getIntExtra("action", ACTION_PLAY) == ACTION_ENQUEUE;
int[] songs;
Song first;
if (type == TYPE_SONG)
songs = new int[] { id };
else if (type == TYPE_ALBUM)
songs = Song.getAllSongIds(MediaStore.Audio.Media.ALBUM_ID + "=" + id);
else if (type == TYPE_ARTIST)
songs = Song.getAllSongIds(MediaStore.Audio.Media.ARTIST_ID + "=" + id);
else
break;
if (songs.length == 0)
int[] songs = Song.getAllSongIdsWith(intent.getIntExtra("type", Song.FIELD_TITLE), id);
if (songs == null || songs.length == 0)
break;
for (int i = songs.length; --i != 0; ) {
@ -759,8 +741,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mSongTimeline.add(song);
}
first = mSongTimeline.get(mCurrentSong + mQueuePos + 1);
mQueuePos += songs.length;
} else {
List<Song> view = mSongTimeline.subList(mCurrentSong + 1, mSongTimeline.size());
@ -774,30 +754,9 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
mSongTimeline.addAll(queue);
mQueuePos += songs.length - 1;
first = mSongTimeline.get(mCurrentSong + 1);
}
}
first.populate();
String title;
switch (type) {
case TYPE_ARTIST:
title = first.artist;
break;
case TYPE_SONG:
title = first.title;
break;
case TYPE_ALBUM:
title = first.album;
break;
default:
title = null;
}
String text = getResources().getString(enqueue ? R.string.enqueued : R.string.playing, title);
Toast.makeText(PlaybackService.this, text, Toast.LENGTH_SHORT).show();
if (!enqueue)
mHandler.sendEmptyMessage(TRACK_CHANGED);

View File

@ -30,6 +30,10 @@ import android.provider.MediaStore;
import android.util.SparseArray;
public class Song implements Parcelable {
public static final int FIELD_ARTIST = 1;
public static final int FIELD_ALBUM = 2;
public static final int FIELD_TITLE = 3;
int id;
int albumId;
int artistId;
@ -131,6 +135,17 @@ public class Song implements Parcelable {
cursor.close();
return songs;
}
public static int[] getAllSongIdsWith(int type, int id)
{
if (type == FIELD_TITLE)
return new int[] { id };
else if (type == FIELD_ALBUM)
return Song.getAllSongIds(MediaStore.Audio.Media.ALBUM_ID + "=" + id);
else if (type == FIELD_ARTIST)
return Song.getAllSongIds(MediaStore.Audio.Media.ARTIST_ID + "=" + id);
return null;
}
public static Song[] getAllSongMetadata()
{
@ -164,6 +179,32 @@ public class Song implements Parcelable {
return songs;
}
public String getField(int field)
{
switch (field) {
case FIELD_TITLE:
return title;
case FIELD_ARTIST:
return artist;
case FIELD_ALBUM:
return album;
}
return null;
}
public int getFieldId(int field)
{
switch (field) {
case FIELD_TITLE:
return id;
case FIELD_ARTIST:
return artistId;
case FIELD_ALBUM:
return albumId;
}
return 0;
}
public boolean equals(Song other)
{
if (other == null)

View File

@ -32,7 +32,7 @@ public class SongAdapter extends AbstractAdapter {
public SongAdapter(Context context, Song[] allSongs)
{
super(ContextApplication.getContext(), sort(allSongs), 0, 3);
super(ContextApplication.getContext(), sort(allSongs), 0, Song.FIELD_TITLE);
}
@Override
@ -42,12 +42,4 @@ public class SongAdapter extends AbstractAdapter {
upper.setText(song.title);
lower.setText(song.artist);
}
public long getItemId(int i)
{
Song song = get(i);
if (song == null)
return 0;
return song.id;
}
}

View File

@ -41,31 +41,19 @@ import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
public class SongSelector extends TabActivity implements AdapterView.OnItemClickListener, TextWatcher, View.OnClickListener {
private TabHost mTabHost;
private TextView mTextFilter;
private AbstractAdapter[] mAdapters = new AbstractAdapter[3];
private ListView mArtistView;
private ListView mAlbumView;
private int listToMediaType(Object list)
{
if (list == mArtistView)
return PlaybackService.TYPE_ARTIST;
if (list == mAlbumView)
return PlaybackService.TYPE_ALBUM;
return PlaybackService.TYPE_SONG;
}
private ListView initializeListView(int id, BaseAdapter adapter)
private void initializeListView(int id, BaseAdapter adapter)
{
ListView view = (ListView)findViewById(id);
view.setOnItemClickListener(this);
view.setOnCreateContextMenuListener(this);
view.setAdapter(adapter);
return view;
}
@Override
@ -88,7 +76,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
mAdapters[0] = new ArtistAdapter(this, songs);
mAdapters[0].setExpanderListener(this);
mArtistView = initializeListView(R.id.artist_list, mAdapters[0]);
initializeListView(R.id.artist_list, mAdapters[0]);
mTextFilter = (TextView)findViewById(R.id.filter_text);
mTextFilter.addTextChangedListener(this);
@ -111,7 +99,7 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
{
mAdapters[1] = new AlbumAdapter(SongSelector.this, songs);
mAdapters[1].setExpanderListener(SongSelector.this);
mAlbumView = initializeListView(R.id.album_list, mAdapters[1]);
initializeListView(R.id.album_list, mAdapters[1]);
mAdapters[2] = new SongAdapter(SongSelector.this, songs);
initializeListView(R.id.song_list, mAdapters[2]);
@ -129,23 +117,25 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
return mTextFilter.onKeyDown(keyCode, event);
}
private Intent buildSongIntent(int action, int type, int id)
private void sendSongIntent(Intent intent)
{
Intent intent = new Intent(this, PlaybackService.class);
intent.putExtra("type", type);
intent.putExtra("action", action);
intent.putExtra("id", id);
return intent;
int action = intent.getIntExtra("action", PlaybackService.ACTION_PLAY) == PlaybackService.ACTION_PLAY ? R.string.playing : R.string.enqueued;
String title = intent.getStringExtra("title");
String text = getResources().getString(action, title);
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
intent.removeExtra("title");
startService(intent);
}
public void onItemClick(AdapterView<?> list, View view, int pos, long id)
{
if (mHandler.hasMessages(MSG_ITEM_CLICK, view)) {
mHandler.removeMessages(MSG_ITEM_CLICK, view);
startService(buildSongIntent(PlaybackService.ACTION_ENQUEUE, listToMediaType(list), (int)id));
if (mHandler.hasMessages(MSG_ITEM_CLICK, list)) {
mHandler.removeMessages(MSG_ITEM_CLICK, list);
sendSongIntent(((AbstractAdapter)list.getAdapter()).buildSongIntent(PlaybackService.ACTION_ENQUEUE, pos));
} else {
Message message = mHandler.obtainMessage(MSG_ITEM_CLICK, view);
message.arg1 = (int)id;
Message message = mHandler.obtainMessage(MSG_ITEM_CLICK, list);
message.arg1 = pos;
mHandler.sendMessageDelayed(message, 333);
}
}
@ -184,9 +174,8 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
{
switch (message.what) {
case MSG_ITEM_CLICK:
int type = listToMediaType(((View)message.obj).getParent());
int id = message.arg1;
startService(buildSongIntent(PlaybackService.ACTION_PLAY, type, id));
AbstractAdapter adapter = (AbstractAdapter)((ListView)message.obj).getAdapter();
sendSongIntent(adapter.buildSongIntent(PlaybackService.ACTION_PLAY, message.arg1));
break;
}
}
@ -198,16 +187,16 @@ public class SongSelector extends TabActivity implements AdapterView.OnItemClick
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo info)
{
int type = listToMediaType(view);
int id = (int)((AdapterView.AdapterContextMenuInfo)info).id;
menu.add(0, MENU_PLAY, 0, R.string.play).setIntent(buildSongIntent(PlaybackService.ACTION_PLAY, type, id));
menu.add(0, MENU_ENQUEUE, 0, R.string.enqueue).setIntent(buildSongIntent(PlaybackService.ACTION_ENQUEUE, type, id));
AbstractAdapter adapter = (AbstractAdapter)((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));
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
startService(item.getIntent());
sendSongIntent(item.getIntent());
return true;
}
}