Run playback activity song/state change methods on UI thread

This allows CoverView and song info views to be updated in one go, rather than
in multiple posts to the UI thread.
This commit is contained in:
Christopher Eby 2011-08-30 01:35:18 -05:00
parent cc1a5cb448
commit 69bbfbb255
3 changed files with 72 additions and 82 deletions

View File

@ -145,7 +145,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
* Show the message view overlay, creating it if necessary and clearing
* it of all content.
*/
void showMessageOverlay()
private void showMessageOverlay()
{
if (mMessageOverlay == null) {
mMessageOverlay = new RelativeLayout(this);
@ -162,7 +162,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
/**
* Hide the message overlay, if it exists.
*/
void hideMessageOverlay()
private void hideMessageOverlay()
{
if (mMessageOverlay != null)
mMessageOverlay.setVisibility(View.GONE);
@ -173,7 +173,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
* must have been created with showMessageOverlay before this method is
* called.
*/
void setNoMediaOverlayMessage()
private void setNoMediaOverlayMessage()
{
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
@ -187,52 +187,40 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
}
@Override
protected void setState(final int state)
protected void onStateChange(int state, int toggled)
{
int toggled = mState ^ state;
super.onStateChange(state, toggled);
if ((toggled & PlaybackService.FLAG_NO_MEDIA) != 0) {
runOnUiThread(new Runnable() {
@Override
public void run()
{
if ((state & PlaybackService.FLAG_NO_MEDIA) != 0) {
showMessageOverlay();
setNoMediaOverlayMessage();
} else {
hideMessageOverlay();
}
}
});
if ((state & PlaybackService.FLAG_NO_MEDIA) != 0) {
showMessageOverlay();
setNoMediaOverlayMessage();
} else {
hideMessageOverlay();
}
}
super.setState(state);
}
@Override
protected void onSongChange(final Song song)
{
mDuration = song == null ? 0 : song.duration;
if (mTitle != null) {
runOnUiThread(new Runnable() {
@Override
public void run()
{
if (song == null) {
mTitle.setText(null);
mAlbum.setText(null);
mArtist.setText(null);
} else {
mTitle.setText(song.title);
mAlbum.setText(song.album);
mArtist.setText(song.artist);
}
}
});
}
mUiHandler.sendEmptyMessage(MSG_UPDATE_PROGRESS);
super.onSongChange(song);
mDuration = song == null ? 0 : song.duration;
if (mTitle != null) {
if (song == null) {
mTitle.setText(null);
mAlbum.setText(null);
mArtist.setText(null);
} else {
mTitle.setText(song.title);
mAlbum.setText(song.album);
mArtist.setText(song.artist);
}
}
updateProgress();
}
@Override

View File

@ -36,13 +36,13 @@ import android.view.MenuItem;
import android.view.View;
public class PlaybackActivity extends Activity implements Handler.Callback, View.OnClickListener, CoverView.Callback {
Handler mHandler;
Looper mLooper;
protected Handler mHandler;
protected Looper mLooper;
CoverView mCoverView;
ControlButton mPlayPauseButton;
int mState;
protected CoverView mCoverView;
protected ControlButton mPlayPauseButton;
protected int mState;
private long mLastStateEvent;
private long mLastSongEvent;
@ -139,12 +139,12 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
public void nextSong()
{
onSongChange(ContextApplication.getService().nextSong());
setSong(ContextApplication.getService().nextSong());
}
public void previousSong()
{
onSongChange(ContextApplication.getService().previousSong());
setSong(ContextApplication.getService().previousSong());
}
public void onClick(View view)
@ -163,31 +163,32 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
}
/**
* Updates <code>mState</code> and the play/pause button. Override to
* implement further behavior in subclasses.
* Called when the PlaybackService state has changed.
*
* @param state PlaybackService state
* @param toggled The flags that have changed from the previous state
*/
protected void setState(int state)
protected void onStateChange(int state, int toggled)
{
if ((toggled & PlaybackService.FLAG_PLAYING) != 0 && mPlayPauseButton != null)
mPlayPauseButton.setImageResource((state & PlaybackService.FLAG_PLAYING) == 0 ? R.drawable.play : R.drawable.pause);
}
protected void setState(final int state)
{
mLastStateEvent = SystemClock.uptimeMillis();
if (mState == state)
return;
int toggled = mState ^ state;
if ((toggled & PlaybackService.FLAG_PLAYING) != 0 && mPlayPauseButton != null) {
final int res = (state & PlaybackService.FLAG_PLAYING) == 0 ? R.drawable.play : R.drawable.pause;
if (mState != state) {
final int toggled = mState ^ state;
mState = state;
runOnUiThread(new Runnable() {
public void run()
{
mPlayPauseButton.setImageResource(res);
onStateChange(state, toggled);
}
});
}
mState = state;
}
/**
@ -197,24 +198,30 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
protected void onServiceReady()
{
PlaybackService service = ContextApplication.getService();
onSongChange(service.getSong(0));
setSong(service.getSong(0));
setState(service.getState());
}
/**
* Called when the current song changes.
*
* @param song The new song
*/
protected void onSongChange(final Song song)
protected void onSongChange(Song song)
{
if (mCoverView != null)
mCoverView.setCurrentSong(song);
}
protected void setSong(final Song song)
{
mLastSongEvent = SystemClock.uptimeMillis();
if (mCoverView != null) {
runOnUiThread(new Runnable() {
public void run()
{
mCoverView.setCurrentSong(song);
}
});
}
runOnUiThread(new Runnable() {
public void run()
{
onSongChange(song);
}
});
}
/**
@ -236,7 +243,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
if (intent.hasExtra("song") && time > mLastSongEvent) {
Song song = intent.getParcelableExtra("song");
onSongChange(song);
setSong(song);
}
}
if (mCoverView != null)

View File

@ -71,8 +71,8 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
private View mClearButton;
private View mControls;
TextView mStatusText;
ImageView mCover;
private TextView mStatusText;
private ImageView mCover;
private ViewGroup mLimiterViews;
@ -686,20 +686,15 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
text = res.getString(R.string.title_by_artist, title, artist);
}
mStatusText.setText(text);
if (mCoverSize == -1) {
DisplayMetrics metrics = ContextApplication.getContext().getResources().getDisplayMetrics();
mCoverSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 64, metrics);
}
final CharSequence result = text;
final Bitmap cover = CoverBitmap.createScaledBitmap(song, mCoverSize);
runOnUiThread(new Runnable() {
public void run()
{
mStatusText.setText(result);
mCover.setImageBitmap(cover);
mCover.setVisibility(cover == null ? View.GONE : View.VISIBLE);
}
});
Bitmap cover = CoverBitmap.createScaledBitmap(song, mCoverSize);
mCover.setImageBitmap(cover);
mCover.setVisibility(cover == null ? View.GONE : View.VISIBLE);
}
}