Minor refactoring.

This commit is contained in:
Felix Ableitner 2014-01-04 17:56:59 +01:00
parent b0f124c158
commit cb65836ff4

View file

@ -90,6 +90,8 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
private ImageButton mPlayPause; private ImageButton mPlayPause;
private ImageButton mShuffle; private ImageButton mShuffle;
private ImageButton mRepeat; private ImageButton mRepeat;
private TextView mCurrentTimeView;
private TextView mTotalTimeView;
private View mCurrentTrackView; private View mCurrentTrackView;
@ -188,6 +190,9 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
mPlayPause.setOnClickListener(this); mPlayPause.setOnClickListener(this);
mPlayPause.setImageResource(R.drawable.ic_action_play); mPlayPause.setImageResource(R.drawable.ic_action_play);
mCurrentTimeView = (TextView) getView().findViewById(R.id.current_time);
mTotalTimeView = (TextView) getView().findViewById(R.id.total_time);
getActivity().getApplicationContext().startService( getActivity().getApplicationContext().startService(
new Intent(getActivity(), MediaRouterPlayService.class)); new Intent(getActivity(), MediaRouterPlayService.class));
getActivity().getApplicationContext().bindService( getActivity().getApplicationContext().bindService(
@ -321,6 +326,7 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
mControls.setVisibility(View.VISIBLE); mControls.setVisibility(View.VISIBLE);
if (mStartPlayingOnSelect != -1) { if (mStartPlayingOnSelect != -1) {
mMediaRouterPlayService.getService().play(mStartPlayingOnSelect); mMediaRouterPlayService.getService().play(mStartPlayingOnSelect);
changePlayPauseState(true);
mStartPlayingOnSelect = -1; mStartPlayingOnSelect = -1;
} }
TextView emptyView = (TextView) mListView.getEmptyView(); TextView emptyView = (TextView) mListView.getEmptyView();
@ -390,15 +396,12 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
case R.id.playpause: case R.id.playpause:
if (mPlaying) { if (mPlaying) {
s.pause(); s.pause();
mPlayPause.setImageResource(R.drawable.ic_action_play); changePlayPauseState(false);
mPlayPause.setContentDescription(getResources().getString(R.string.play));
} else { } else {
s.resume(); s.resume();
mListView.smoothScrollToPosition(s.getCurrentTrack()); mListView.smoothScrollToPosition(s.getCurrentTrack());
mPlayPause.setImageResource(R.drawable.ic_action_pause); changePlayPauseState(true);
mPlayPause.setContentDescription(getResources().getString(R.string.pause));
} }
mPlaying = !mPlaying;
break; break;
case R.id.shuffle: case R.id.shuffle:
s.toggleShuffleEnabled(); s.toggleShuffleEnabled();
@ -498,9 +501,10 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
mPlaylistAdapter.add(playlist); mPlaylistAdapter.add(playlist);
mMediaRouterPlayService.getService().setPlaylist(playlist); mMediaRouterPlayService.getService().setPlaylist(playlist);
if (mSelectedRoute != null) if (mSelectedRoute != null) {
mMediaRouterPlayService.getService().play(start); mMediaRouterPlayService.getService().play(start);
else { changePlayPauseState(true);
} else {
Toast.makeText(getActivity(), R.string.select_route, Toast.LENGTH_SHORT) Toast.makeText(getActivity(), R.string.select_route, Toast.LENGTH_SHORT)
.show(); .show();
mStartPlayingOnSelect = start; mStartPlayingOnSelect = start;
@ -546,29 +550,37 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements
int currentTime = (int) status.getContentPosition() / 1000; int currentTime = (int) status.getContentPosition() / 1000;
int totalTime = (int) status.getContentDuration() / 1000; int totalTime = (int) status.getContentDuration() / 1000;
TextView currentTimeView = (TextView) getView().findViewById(R.id.current_time); mCurrentTimeView.setText(generateTimeString(currentTime));
currentTimeView.setText(generateTimeString(currentTime)); mTotalTimeView.setText(generateTimeString(totalTime));
TextView totalTimeView = (TextView) getView().findViewById(R.id.total_time);
totalTimeView.setText(generateTimeString(totalTime));
mProgressBar.setProgress(currentTime); mProgressBar.setProgress(currentTime);
mProgressBar.setMax(totalTime); mProgressBar.setMax(totalTime);
if (status.getPlaybackState() == MediaItemStatus.PLAYBACK_STATE_PLAYING || if (status.getPlaybackState() != MediaItemStatus.PLAYBACK_STATE_PLAYING &&
status.getPlaybackState() == MediaItemStatus.PLAYBACK_STATE_BUFFERING || status.getPlaybackState() != MediaItemStatus.PLAYBACK_STATE_BUFFERING &&
status.getPlaybackState() == MediaItemStatus.PLAYBACK_STATE_PENDING) { status.getPlaybackState() != MediaItemStatus.PLAYBACK_STATE_PENDING)
mPlaying = true; changePlayPauseState(false);
mPlayPause.setImageResource(R.drawable.ic_action_pause);
mPlayPause.setContentDescription(getResources().getString(R.string.pause));
}
else {
mPlaying = false;
mPlayPause.setImageResource(R.drawable.ic_action_play);
mPlayPause.setContentDescription(getResources().getString(R.string.play));
}
if (mListView.getAdapter() == mPlaylistAdapter) if (mListView.getAdapter() == mPlaylistAdapter)
enableTrackHighlight(); enableTrackHighlight();
} }
/**
* Changes the state of mPlayPause button to pause/resume according to
* current playback state, also sets mPlaying.
*
* @param playing True if an item is currently being played, false otherwise.
*/
private void changePlayPauseState(boolean playing) {
mPlaying = playing;
if (mPlaying) {
mPlayPause.setImageResource(R.drawable.ic_action_pause);
mPlayPause.setContentDescription(getResources().getString(R.string.pause));
}
else {
mPlayPause.setImageResource(R.drawable.ic_action_play);
mPlayPause.setContentDescription(getResources().getString(R.string.play));
}
}
} }