Handle non-running PlaybackService better

Add a method to retrieve the PlaybackService instance that will start the
service if it is not running
This commit is contained in:
Christopher Eby 2010-04-26 17:45:54 -05:00
parent 80ebbafb6d
commit fe2ec0f975
6 changed files with 64 additions and 27 deletions

View File

@ -32,13 +32,9 @@ import android.content.Intent;
public class ContextApplication extends Application { public class ContextApplication extends Application {
private static ContextApplication mInstance; private static ContextApplication mInstance;
private static ArrayList<Activity> mActivities; private static ArrayList<Activity> mActivities;
private static PlaybackService mService;
private static Random mRandom; private static Random mRandom;
/**
* The PlaybackService instance, if any.
*/
public static PlaybackService service;
public ContextApplication() public ContextApplication()
{ {
mInstance = this; mInstance = this;
@ -62,6 +58,44 @@ public class ContextApplication extends Application {
return mInstance; return mInstance;
} }
/**
* Return the PlaybackService instance, creating one if needed.
*/
public static PlaybackService getService()
{
if (mService == null) {
mInstance.startService(new Intent(mInstance, PlaybackService.class));
while (mService == null) {
try {
mInstance.wait();
} catch (InterruptedException e) {
}
}
}
return mService;
}
/**
* Returns whether a PlaybackService instance is active.
*/
public static boolean hasService()
{
return mService != null;
}
/**
* Set the PlaybackService instance to <code>service</code> and notify all
* clients waiting for an instance.
*/
public static void setService(PlaybackService service)
{
mService = service;
synchronized (mInstance) {
mInstance.notifyAll();
}
}
/** /**
* Add an Activity to the list of Activities. * Add an Activity to the list of Activities.
* *

View File

@ -93,7 +93,7 @@ public final class CoverView extends View {
*/ */
public void initialize() public void initialize()
{ {
mTimelinePos = ContextApplication.service.getTimelinePos(); mTimelinePos = ContextApplication.getService().getTimelinePos();
querySongs(true); querySongs(true);
} }
@ -341,9 +341,10 @@ public final class CoverView extends View {
*/ */
private void querySongs(boolean force) private void querySongs(boolean force)
{ {
PlaybackService service = ContextApplication.getService();
for (int i = STORE_SIZE; --i != -1; ) { for (int i = STORE_SIZE; --i != -1; ) {
if (force || mSongs[i] == null) if (force || mSongs[i] == null)
setSong(i, ContextApplication.service.getSong(i - STORE_SIZE / 2)); setSong(i, service.getSong(i - STORE_SIZE / 2));
} }
} }

View File

@ -136,7 +136,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
{ {
super.onServiceReady(); super.onServiceReady();
mDuration = ContextApplication.service.getDuration(); mDuration = ContextApplication.getService().getDuration();
} }
@Override @Override
@ -145,7 +145,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
super.receive(intent); super.receive(intent);
if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) { if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) {
mDuration = ContextApplication.service.getDuration(); mDuration = ContextApplication.getService().getDuration();
mUiHandler.sendEmptyMessage(MSG_UPDATE_PROGRESS); mUiHandler.sendEmptyMessage(MSG_UPDATE_PROGRESS);
} }
} }
@ -220,7 +220,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
if (mPaused || mControlsTop.getVisibility() != View.VISIBLE || (mState & PlaybackService.FLAG_PLAYING) == 0) if (mPaused || mControlsTop.getVisibility() != View.VISIBLE || (mState & PlaybackService.FLAG_PLAYING) == 0)
return; return;
int position = ContextApplication.service.getPosition(); int position = ContextApplication.getService().getPosition();
if (!mSeekBarTracking) if (!mSeekBarTracking)
mSeekBar.setProgress(mDuration == 0 ? 0 : (int)(1000 * position / mDuration)); mSeekBar.setProgress(mDuration == 0 ? 0 : (int)(1000 * position / mDuration));
@ -285,7 +285,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{ {
if (fromUser) if (fromUser)
ContextApplication.service.seekToProgress(progress); ContextApplication.getService().seekToProgress(progress);
} }
public void onStartTrackingTouch(SeekBar seekBar) public void onStartTrackingTouch(SeekBar seekBar)

View File

@ -65,9 +65,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
{ {
super.onStart(); super.onStart();
startService(new Intent(this, PlaybackService.class)); if (ContextApplication.hasService())
if (ContextApplication.service != null)
onServiceReady(); onServiceReady();
else
startService(new Intent(this, PlaybackService.class));
} }
public static boolean handleKeyLongPress(int keyCode) public static boolean handleKeyLongPress(int keyCode)
@ -138,7 +139,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
{ {
if (mCoverView != null) if (mCoverView != null)
mCoverView.initialize(); mCoverView.initialize();
setState(ContextApplication.service.getState()); setState(ContextApplication.getService().getState());
} }
/** /**
@ -233,10 +234,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
if (text != -1) if (text != -1)
Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
ContextApplication.service.toggleFlag(flag); ContextApplication.getService().toggleFlag(flag);
break; break;
case MSG_SET_SONG: case MSG_SET_SONG:
ContextApplication.service.setCurrentSong(message.arg1); ContextApplication.getService().setCurrentSong(message.arg1);
break; break;
default: default:
return false; return false;

View File

@ -120,8 +120,6 @@ public final class PlaybackService extends Service implements Handler.Callback,
@Override @Override
public void onCreate() public void onCreate()
{ {
ContextApplication.service = this;
HandlerThread thread = new HandlerThread("PlaybackService"); HandlerThread thread = new HandlerThread("PlaybackService");
thread.start(); thread.start();
@ -139,6 +137,8 @@ public final class PlaybackService extends Service implements Handler.Callback,
mSongTimeline = new ArrayList<Song>(); mSongTimeline = new ArrayList<Song>();
} }
ContextApplication.setService(this);
mLooper = thread.getLooper(); mLooper = thread.getLooper();
mHandler = new Handler(mLooper, this); mHandler = new Handler(mLooper, this);
mHandler.sendEmptyMessage(CREATE); mHandler.sendEmptyMessage(CREATE);
@ -191,7 +191,7 @@ public final class PlaybackService extends Service implements Handler.Callback,
@Override @Override
public void onDestroy() public void onDestroy()
{ {
ContextApplication.service = null; ContextApplication.setService(null);
super.onDestroy(); super.onDestroy();

View File

@ -124,9 +124,10 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
mPlayPauseButton.setOnClickListener(this); mPlayPauseButton.setOnClickListener(this);
next.setOnClickListener(this); next.setOnClickListener(this);
if (ContextApplication.service != null) { if (ContextApplication.hasService()) {
setState(ContextApplication.service.getState()); PlaybackService service = ContextApplication.getService();
onSongChange(ContextApplication.service.getSong(0)); setState(service.getState());
onSongChange(service.getSong(0));
} }
} else if (!status && mStatus != null) { } else if (!status && mStatus != null) {
mStatus.setVisibility(View.GONE); mStatus.setVisibility(View.GONE);
@ -143,7 +144,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem
super.onServiceReady(); super.onServiceReady();
if (mStatusText != null) if (mStatusText != null)
onSongChange(ContextApplication.service.getSong(0)); onSongChange(ContextApplication.getService().getSong(0));
} }
@Override @Override