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:
parent
80ebbafb6d
commit
fe2ec0f975
@ -32,13 +32,9 @@ import android.content.Intent;
|
||||
public class ContextApplication extends Application {
|
||||
private static ContextApplication mInstance;
|
||||
private static ArrayList<Activity> 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 <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.
|
||||
*
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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<Song>();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user