Give CoverView its own Handler

This commit is contained in:
Christopher Eby 2010-06-01 21:28:28 -05:00
parent 9de092f7fb
commit d92c8a2638
4 changed files with 56 additions and 16 deletions

View File

@ -26,6 +26,8 @@ import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
@ -38,15 +40,15 @@ import android.widget.Scroller;
* Displays a flingable/draggable View of cover art/song info images
* generated by CoverBitmap.
*/
public final class CoverView extends View {
public final class CoverView extends View implements Handler.Callback {
private static final int STORE_SIZE = 3;
private static int SNAP_VELOCITY = -1;
/**
* The Handler with which to do background work. Must be initialized by
* the containing Activity.
* The Handler with which to do background work. Will be null until
* setupHandler is called.
*/
Handler mHandler;
private Handler mHandler;
/**
* Whether or not to display song info on top of the cover art. Can be
* initialized by the containing Activity.
@ -88,6 +90,17 @@ public final class CoverView extends View {
SNAP_VELOCITY = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
}
/**
* Setup the Handler to act on the given looper. This must be called before
* the CoverView is used.
*
* @param looper The Looper to use.
*/
public void setupHandler(Looper looper)
{
mHandler = new Handler(looper, this);
}
/**
* Query the service for initial song info.
*/
@ -287,7 +300,7 @@ public final class CoverView extends View {
*
* Prunes old bitmaps if the timeline becomes full.
*/
void generateBitmap(Song song)
private void generateBitmap(Song song)
{
int id = (int)song.id;
boolean created = false;
@ -343,14 +356,8 @@ public final class CoverView extends View {
private void setSong(int i, final Song song)
{
mSongs[i] = song;
if (song != null) {
mHandler.post(new Runnable() {
public void run()
{
generateBitmap(song);
}
});
}
if (song != null)
mHandler.sendMessage(mHandler.obtainMessage(MSG_GENERATE_BITMAP, song));
}
/**
@ -387,4 +394,34 @@ public final class CoverView extends View {
querySongs(force);
}
}
/**
* Call {@link CoverView#generateBitmap(Song)} for the given song.
*
* obj must be the Song to generate a bitmap for.
*/
private static final int MSG_GENERATE_BITMAP = 0;
/**
* Tell PlaybackService to change the current song.
*
* arg1 should be the delta, -1 or 1, indicating the previous or next song,
* respectively.
*/
static final int MSG_SET_SONG = 1;
public boolean handleMessage(Message message)
{
switch (message.what) {
case MSG_GENERATE_BITMAP:
generateBitmap((Song)message.obj);
break;
case MSG_SET_SONG:
ContextApplication.getService().setCurrentSong(message.arg1);
break;
default:
return false;
}
return true;
}
}

View File

@ -63,8 +63,8 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
setContentView(R.layout.full_playback);
mCoverView = (CoverView)findViewById(R.id.cover_view);
mCoverView.mHandler = mHandler;
mCoverView.setOnClickListener(this);
mCoverView.setupHandler(mLooper);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
mCoverView.mSeparateInfo = settings.getBoolean("separate_info", false);

View File

@ -43,7 +43,7 @@ public class MiniPlaybackActivity extends PlaybackActivity implements View.OnCli
setContentView(R.layout.mini_playback);
mCoverView = (CoverView)findViewById(R.id.cover_view);
mCoverView.mHandler = mHandler;
mCoverView.setupHandler(mLooper);
View openButton = findViewById(R.id.open_button);
openButton.setOnClickListener(this);

View File

@ -233,7 +233,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
*/
static final int MSG_TOGGLE_FLAG = 0;
/**
* Tell PlaybackService to move to a song a position delta (passed as arg1).
* Tell PlaybackService to change the current song.
*
* arg1 should be the delta, -1 or 1, indicating the previous or next song,
* respectively.
*/
static final int MSG_SET_SONG = 1;