From 4a24644485063f8fdbb8782717027fd2ffbc7542 Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 21 Mar 2010 16:29:02 -0500 Subject: [PATCH] Defer some service startup code The main advantage to this is the notification will now be executed after any pending go messages are sent; there won't be a sudden change in the notification when a play message is sent on startup --- src/org/kreed/vanilla/PlaybackService.java | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 30f4c080..b87af60e 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -301,6 +301,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On private Method mStopForeground; private static final int GO = 0; + private static final int POST_CREATE = 1; private static final int PREF_CHANGED = 3; private static final int DO_ITEM = 4; private static final int TRACK_CHANGED = 5; @@ -390,16 +391,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On if (mPendingGo != -1) mHandler.sendMessage(mHandler.obtainMessage(GO, mPendingGo, 0)); - updateNotification(getSong(0)); - - IntentFilter filter = new IntentFilter(); - filter.addAction(Intent.ACTION_HEADSET_PLUG); - filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); - filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); - registerReceiver(mReceiver, filter); - - TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); - telephonyManager.listen(mCallListener, PhoneStateListener.LISTEN_CALL_STATE); + mHandler.sendEmptyMessage(POST_CREATE); Looper.loop(); } @@ -747,6 +739,18 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On mHandler.removeMessages(SAVE_STATE); mHandler.sendEmptyMessageDelayed(SAVE_STATE, 5000); break; + case POST_CREATE: + updateNotification(getSong(0)); + + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_HEADSET_PLUG); + filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); + filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + registerReceiver(mReceiver, filter); + + TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); + telephonyManager.listen(mCallListener, PhoneStateListener.LISTEN_CALL_STATE); + break; } } }