Don't call startService to pick songs from library

This commit is contained in:
Christopher Eby 2011-09-11 00:25:02 -05:00
parent 06915f6cf7
commit 744432a744
2 changed files with 86 additions and 79 deletions

View File

@ -97,42 +97,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
* when this is called.
*/
public static final String ACTION_PREVIOUS_SONG_AUTOPLAY = "org.kreed.vanilla.action.PREVIOUS_SONG_AUTOPLAY";
/**
* Intent action that may be invoked through startService.
*
* Given a song or group of songs, play the first and enqueues the rest after
* it.
*
* If FLAG_SHUFFLE is enabled, songs will be added to the song timeline in
* random order, otherwise, songs will be ordered by album name and then
* track number.
*
* Requires two extras: "type", which can be 1, 2, or 3, indicating artist,
* album, or song respectively, and "id", which is the MediaStore id for the
* song, album, or artist.
*/
public static final String ACTION_PLAY_ITEMS = "org.kreed.vanilla.action.PLAY_ITEMS";
/**
* Intent action that may be invoked through startService.
*
* Enqueues a song or group of songs.
*
* The first song from the group will be placed in the timeline either
* after the last enqueued song or after the playing song if the queue is
* empty. If FLAG_SHUFFLE is enabled, songs will be added to the song
* timeline in random order, otherwise, songs will be ordered by album name
* and then track number.
*
* Requires two extras: "type", which can be 1, 2, or 3, indicating artist,
* album, or song respectively, and "id", which is the MediaStore id for the
* song, album, or artist.
*/
public static final String ACTION_ENQUEUE_ITEMS = "org.kreed.vanilla.action.ENQUEUE_ITEMS";
/**
* Reset the position at which songs are enqueued, that is, new songs will
* be placed directly after the playing song after this action is invoked.
*/
public static final String ACTION_FINISH_ENQUEUEING = "org.kreed.vanilla.action.FINISH_ENQUEUEING";
public static final String EVENT_REPLACE_SONG = "org.kreed.vanilla.event.REPLACE_SONG";
public static final String EVENT_CHANGED = "org.kreed.vanilla.event.CHANGED";
@ -260,15 +224,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
go(-1, false);
} else if (ACTION_PREVIOUS_SONG_AUTOPLAY.equals(action)) {
go(-1, true);
} else if (ACTION_PLAY_ITEMS.equals(action)) {
mTimeline.chooseSongs(false, intent.getIntExtra("type", 3), intent.getLongExtra("id", -1));
mHandler.sendEmptyMessage(TRACK_CHANGED);
} else if (ACTION_ENQUEUE_ITEMS.equals(action)) {
mTimeline.chooseSongs(true, intent.getIntExtra("type", 3), intent.getLongExtra("id", -1));
mHandler.removeMessages(SAVE_STATE);
mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000);
} else if (ACTION_FINISH_ENQUEUEING.equals(action)) {
mTimeline.finishEnqueueing();
}
userActionTriggered();
@ -1011,4 +966,50 @@ public final class PlaybackService extends Service implements Handler.Callback,
mMediaPlayer.setVolume(mCurrentVolume, mCurrentVolume);
}
}
/**
* Given a song or group of songs represented by the given type and id, play
* the first and enqueues the rest after it.
*
* If FLAG_SHUFFLE is enabled, songs will be added to the song timeline in
* random order, otherwise, songs will be ordered by album name and then
* track number.
*
* @param type The media type, one of MediaUtils.TYPE_*
* @param id The MediaStore id of the media
* @return The song that is playing after this method is called
*/
public Song playSongs(int type, long id)
{
mTimeline.chooseSongs(false, type, id);
return setCurrentSong(+1);
}
/**
* Enqueues a song or group of songs represented by the given type and id.
*
* The first song from the group will be placed in the timeline either
* after the last enqueued song or after the playing song if the queue is
* empty. If FLAG_SHUFFLE is enabled, songs will be added to the song
* timeline in random order, otherwise, songs will be ordered by album name
* and then track number.
*
* @param type The media type, one of MediaUtils.TYPE_*
* @param id The MediaStore id of the media
*/
public void enqueueSongs(int type, long id)
{
mTimeline.chooseSongs(true, type, id);
mHandler.removeMessages(SAVE_STATE);
mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000);
}
/**
* Reset the position at which songs are enqueued. That is, the next song
* enqueued will be placed directly after the playing song.
*/
public void finishEnqueueing()
{
mTimeline.finishEnqueueing();
}
}

View File

@ -63,6 +63,10 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
*/
private static final int TAB_COUNT = 5;
private static final int ACTION_PLAY = 0;
private static final int ACTION_ENQUEUE = 1;
private static final int ACTION_LAST_USED = 2;
private TabHost mTabHost;
private View mSearchBox;
@ -76,9 +80,9 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
private ViewGroup mLimiterViews;
private String mDefaultAction;
private boolean mDefaultIsLastAction;
private int mDefaultAction;
private int mLastAction = ACTION_PLAY;
private long mLastActedId;
private MediaAdapter[] mAdapters;
@ -177,9 +181,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
mControls = null;
}
int action = Integer.parseInt(settings.getString("default_action_int", "0"));
mDefaultAction = action == 1 ? PlaybackService.ACTION_ENQUEUE_ITEMS : PlaybackService.ACTION_PLAY_ITEMS;
mDefaultIsLastAction = action == 2;
mDefaultAction = Integer.parseInt(settings.getString("default_action_int", "0"));
mLastActedId = 0;
}
@ -203,7 +205,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
mTextFilter.setText("");
setSearchBoxVisible(false);
} else {
sendFinishEnqueueing();
ContextApplication.getService().finishEnqueueing();
finish();
}
break;
@ -234,33 +236,40 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
return false;
}
private void sendSongIntent(MediaAdapter.MediaView view, String action)
/**
* Adds songs matching the type and id of the given view to the song timelime.
*
* @param view The MediaView to get type/id data from
* @param action One of SongSelector.ACTION_*
*/
private void pickSongs(MediaAdapter.MediaView view, int action)
{
int res = PlaybackService.ACTION_PLAY_ITEMS.equals(action) ? R.string.playing : R.string.enqueued;
String text = getResources().getString(res, view.getTitle());
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
PlaybackService service = ContextApplication.getService();
Resources res = getResources();
int type = view.getMediaType();
long id = view.getMediaId();
Intent intent = new Intent(this, PlaybackService.class);
intent.setAction(action);
intent.putExtra("type", view.getMediaType());
intent.putExtra("id", id);
startService(intent);
if (action == ACTION_LAST_USED)
action = mLastAction;
else
mLastAction = action;
switch (action) {
case ACTION_PLAY:
Toast.makeText(this, getString(R.string.playing, view.getTitle()), Toast.LENGTH_SHORT).show();
setSong(service.playSongs(type, id));
break;
case ACTION_ENQUEUE:
Toast.makeText(this, getString(R.string.enqueued, view.getTitle()), Toast.LENGTH_SHORT).show();
service.enqueueSongs(type, id);
break;
default:
return;
}
mLastActedId = id;
}
/**
* Tell the PlaybackService that we are finished enqueueing songs.
*/
private void sendFinishEnqueueing()
{
Intent intent = new Intent(this, PlaybackService.class);
intent.setAction(PlaybackService.ACTION_FINISH_ENQUEUEING);
startService(intent);
}
private void expand(MediaAdapter.MediaView view)
{
mTabHost.setCurrentTab(setLimiter(view.getLimiter()));
@ -304,7 +313,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
else if (id == mLastActedId)
startActivity(new Intent(this, FullPlaybackActivity.class));
else
sendSongIntent(mediaView, mDefaultAction);
pickSongs(mediaView, mDefaultAction);
}
public void afterTextChanged(Editable editable)
@ -489,7 +498,6 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
@Override
public boolean onContextItemSelected(MenuItem item)
{
String action = PlaybackService.ACTION_PLAY_ITEMS;
int id = item.getItemId();
int type = item.getGroupId();
int mediaId = item.getOrder();
@ -499,12 +507,10 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
expand((MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView);
break;
case MENU_ENQUEUE:
action = PlaybackService.ACTION_ENQUEUE_ITEMS;
// fall through
pickSongs((MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView, ACTION_ENQUEUE);
break;
case MENU_PLAY:
if (mDefaultIsLastAction)
mDefaultAction = action;
sendSongIntent((MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView, action);
pickSongs((MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView, ACTION_PLAY);
break;
case MENU_NEW_PLAYLIST: {
NewPlaylistDialog dialog = new NewPlaylistDialog(this, null, R.string.create);
@ -565,7 +571,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
setSearchBoxVisible(!mSearchBoxVisible);
return true;
case MENU_PLAYBACK:
sendFinishEnqueueing();
ContextApplication.getService().finishEnqueueing();
startActivity(new Intent(this, FullPlaybackActivity.class));
return true;
default: