diff --git a/src/org/kreed/vanilla/ContextApplication.java b/src/org/kreed/vanilla/ContextApplication.java index 080d07cc..8f55d75b 100644 --- a/src/org/kreed/vanilla/ContextApplication.java +++ b/src/org/kreed/vanilla/ContextApplication.java @@ -32,13 +32,9 @@ import android.content.Intent; public class ContextApplication extends Application { private static ContextApplication mInstance; private static ArrayList mActivities; + private static PlaybackService mService; private static Random mRandom; - /** - * The PlaybackService instance, if any. - */ - public static PlaybackService service; - public ContextApplication() { mInstance = this; @@ -62,6 +58,44 @@ public class ContextApplication extends Application { 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 service 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. * @@ -116,4 +150,4 @@ public class ContextApplication extends Application { } mInstance.stopService(new Intent(mInstance, PlaybackService.class)); } -} \ No newline at end of file +} diff --git a/src/org/kreed/vanilla/CoverView.java b/src/org/kreed/vanilla/CoverView.java index 52ffe3e1..4ba6035c 100644 --- a/src/org/kreed/vanilla/CoverView.java +++ b/src/org/kreed/vanilla/CoverView.java @@ -93,7 +93,7 @@ public final class CoverView extends View { */ public void initialize() { - mTimelinePos = ContextApplication.service.getTimelinePos(); + mTimelinePos = ContextApplication.getService().getTimelinePos(); querySongs(true); } @@ -341,9 +341,10 @@ public final class CoverView extends View { */ private void querySongs(boolean force) { + PlaybackService service = ContextApplication.getService(); for (int i = STORE_SIZE; --i != -1; ) { if (force || mSongs[i] == null) - setSong(i, ContextApplication.service.getSong(i - STORE_SIZE / 2)); + setSong(i, service.getSong(i - STORE_SIZE / 2)); } } diff --git a/src/org/kreed/vanilla/FullPlaybackActivity.java b/src/org/kreed/vanilla/FullPlaybackActivity.java index 1aac0956..d1f915d8 100644 --- a/src/org/kreed/vanilla/FullPlaybackActivity.java +++ b/src/org/kreed/vanilla/FullPlaybackActivity.java @@ -136,7 +136,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On { super.onServiceReady(); - mDuration = ContextApplication.service.getDuration(); + mDuration = ContextApplication.getService().getDuration(); } @Override @@ -145,7 +145,7 @@ public class FullPlaybackActivity extends PlaybackActivity implements SeekBar.On super.receive(intent); if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) { - mDuration = ContextApplication.service.getDuration(); + mDuration = ContextApplication.getService().getDuration(); 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) return; - int position = ContextApplication.service.getPosition(); + int position = ContextApplication.getService().getPosition(); if (!mSeekBarTracking) 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) { if (fromUser) - ContextApplication.service.seekToProgress(progress); + ContextApplication.getService().seekToProgress(progress); } public void onStartTrackingTouch(SeekBar seekBar) diff --git a/src/org/kreed/vanilla/PlaybackActivity.java b/src/org/kreed/vanilla/PlaybackActivity.java index d3b8964c..60b98044 100644 --- a/src/org/kreed/vanilla/PlaybackActivity.java +++ b/src/org/kreed/vanilla/PlaybackActivity.java @@ -65,9 +65,10 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View { super.onStart(); - startService(new Intent(this, PlaybackService.class)); - if (ContextApplication.service != null) + if (ContextApplication.hasService()) onServiceReady(); + else + startService(new Intent(this, PlaybackService.class)); } public static boolean handleKeyLongPress(int keyCode) @@ -138,7 +139,7 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View { if (mCoverView != null) 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) Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); - ContextApplication.service.toggleFlag(flag); + ContextApplication.getService().toggleFlag(flag); break; case MSG_SET_SONG: - ContextApplication.service.setCurrentSong(message.arg1); + ContextApplication.getService().setCurrentSong(message.arg1); break; default: return false; @@ -244,4 +245,4 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View return true; } -} \ No newline at end of file +} diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index f6195e4a..08ca47fa 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -120,8 +120,6 @@ public final class PlaybackService extends Service implements Handler.Callback, @Override public void onCreate() { - ContextApplication.service = this; - HandlerThread thread = new HandlerThread("PlaybackService"); thread.start(); @@ -139,6 +137,8 @@ public final class PlaybackService extends Service implements Handler.Callback, mSongTimeline = new ArrayList(); } + ContextApplication.setService(this); + mLooper = thread.getLooper(); mHandler = new Handler(mLooper, this); mHandler.sendEmptyMessage(CREATE); @@ -191,7 +191,7 @@ public final class PlaybackService extends Service implements Handler.Callback, @Override public void onDestroy() { - ContextApplication.service = null; + ContextApplication.setService(null); super.onDestroy(); @@ -981,4 +981,4 @@ public final class PlaybackService extends Service implements Handler.Callback, { return null; } -} \ No newline at end of file +} diff --git a/src/org/kreed/vanilla/SongSelector.java b/src/org/kreed/vanilla/SongSelector.java index 0cbf59ab..b3ad0631 100644 --- a/src/org/kreed/vanilla/SongSelector.java +++ b/src/org/kreed/vanilla/SongSelector.java @@ -124,9 +124,10 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem mPlayPauseButton.setOnClickListener(this); next.setOnClickListener(this); - if (ContextApplication.service != null) { - setState(ContextApplication.service.getState()); - onSongChange(ContextApplication.service.getSong(0)); + if (ContextApplication.hasService()) { + PlaybackService service = ContextApplication.getService(); + setState(service.getState()); + onSongChange(service.getSong(0)); } } else if (!status && mStatus != null) { mStatus.setVisibility(View.GONE); @@ -143,7 +144,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem super.onServiceReady(); if (mStatusText != null) - onSongChange(ContextApplication.service.getSong(0)); + onSongChange(ContextApplication.getService().getSong(0)); } @Override @@ -451,4 +452,4 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem onSongChange(song); } } -} \ No newline at end of file +}