packages/apps/Music
Revision | aa331877dd1a0777042481e5c925cd951682588f (tree) |
---|---|
Time | 2010-04-21 09:00:26 |
Author | Jean-Michel Trivi <jmtrivi@goog...> |
Commiter | Jean-Michel Trivi |
Update the Music app to fully use AudioFocus to implement its logic
for pausing and resuming for phone rings and calls.
The Music app does no longer need to implement PhoneStateListener.
Change-Id: I9435969ec2ac7f755390136c3b689ec519742fa6
@@ -137,7 +137,6 @@ public class MediaPlaybackService extends Service { | ||
137 | 137 | private WakeLock mWakeLock; |
138 | 138 | private int mServiceStartId = -1; |
139 | 139 | private boolean mServiceInUse = false; |
140 | - private boolean mResumeAfterCall = false; | |
141 | 140 | private boolean mIsSupposedToBePlaying = false; |
142 | 141 | private boolean mQuietMode = false; |
143 | 142 | private AudioManager mAudioManager; |
@@ -152,33 +151,7 @@ public class MediaPlaybackService extends Service { | ||
152 | 151 | private MediaAppWidgetProvider mAppWidgetProvider = MediaAppWidgetProvider.getInstance(); |
153 | 152 | |
154 | 153 | // interval after which we stop the service when idle |
155 | - private static final int IDLE_DELAY = 60000; | |
156 | - | |
157 | - private PhoneStateListener mPhoneStateListener = new PhoneStateListener() { | |
158 | - @Override | |
159 | - public void onCallStateChanged(int state, String incomingNumber) { | |
160 | - if (state == TelephonyManager.CALL_STATE_RINGING) { | |
161 | - AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
162 | - int ringvolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); | |
163 | - mResumeAfterCall = (isPlaying() || mResumeAfterCall) && (getAudioId() >= 0); | |
164 | - if (ringvolume > 0) { | |
165 | - pause(); | |
166 | - } | |
167 | - } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) { | |
168 | - // pause the music while a conversation is in progress | |
169 | - mResumeAfterCall = (isPlaying() || mResumeAfterCall) && (getAudioId() >= 0); | |
170 | - pause(); | |
171 | - } else if (state == TelephonyManager.CALL_STATE_IDLE) { | |
172 | - // start playing again | |
173 | - if (mResumeAfterCall) { | |
174 | - // resume playback only if music was playing | |
175 | - // when the call was answered | |
176 | - startAndFadeIn(); | |
177 | - mResumeAfterCall = false; | |
178 | - } | |
179 | - } | |
180 | - } | |
181 | - }; | |
154 | + private static final int IDLE_DELAY = 60000; | |
182 | 155 | |
183 | 156 | private void startAndFadeIn() { |
184 | 157 | mMediaplayerHandler.sendEmptyMessageDelayed(FADEIN, 10); |
@@ -250,16 +223,16 @@ public class MediaPlaybackService extends Service { | ||
250 | 223 | } else if (CMDTOGGLEPAUSE.equals(cmd) || TOGGLEPAUSE_ACTION.equals(action)) { |
251 | 224 | if (isPlaying()) { |
252 | 225 | pause(); |
253 | - mResumeAfterCall = false; | |
226 | + mPausedByTransientLossOfFocus = false; | |
254 | 227 | } else { |
255 | 228 | play(); |
256 | 229 | } |
257 | 230 | } else if (CMDPAUSE.equals(cmd) || PAUSE_ACTION.equals(action)) { |
258 | 231 | pause(); |
259 | - mResumeAfterCall = false; | |
232 | + mPausedByTransientLossOfFocus = false; | |
260 | 233 | } else if (CMDSTOP.equals(cmd)) { |
261 | 234 | pause(); |
262 | - mResumeAfterCall = false; | |
235 | + mPausedByTransientLossOfFocus = false; | |
263 | 236 | seek(0); |
264 | 237 | } else if (MediaAppWidgetProvider.CMDAPPWIDGETUPDATE.equals(cmd)) { |
265 | 238 | // Someone asked us to refresh a set of specific widgets, probably |
@@ -293,7 +266,7 @@ public class MediaPlaybackService extends Service { | ||
293 | 266 | Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_GAIN"); |
294 | 267 | if(!isPlaying() && mPausedByTransientLossOfFocus) { |
295 | 268 | mPausedByTransientLossOfFocus = false; |
296 | - play(); | |
269 | + startAndFadeIn(); | |
297 | 270 | } |
298 | 271 | break; |
299 | 272 | default: |
@@ -333,8 +306,6 @@ public class MediaPlaybackService extends Service { | ||
333 | 306 | commandFilter.addAction(PREVIOUS_ACTION); |
334 | 307 | registerReceiver(mIntentReceiver, commandFilter); |
335 | 308 | |
336 | - TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); | |
337 | - tmgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); | |
338 | 309 | PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE); |
339 | 310 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName()); |
340 | 311 | mWakeLock.setReferenceCounted(false); |
@@ -367,9 +338,6 @@ public class MediaPlaybackService extends Service { | ||
367 | 338 | mCursor = null; |
368 | 339 | } |
369 | 340 | |
370 | - TelephonyManager tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); | |
371 | - tmgr.listen(mPhoneStateListener, 0); | |
372 | - | |
373 | 341 | unregisterReceiver(mIntentReceiver); |
374 | 342 | if (mUnmountReceiver != null) { |
375 | 343 | unregisterReceiver(mUnmountReceiver); |
@@ -634,16 +602,16 @@ public class MediaPlaybackService extends Service { | ||
634 | 602 | } else if (CMDTOGGLEPAUSE.equals(cmd) || TOGGLEPAUSE_ACTION.equals(action)) { |
635 | 603 | if (isPlaying()) { |
636 | 604 | pause(); |
637 | - mResumeAfterCall = false; | |
605 | + mPausedByTransientLossOfFocus = false; | |
638 | 606 | } else { |
639 | 607 | play(); |
640 | 608 | } |
641 | 609 | } else if (CMDPAUSE.equals(cmd) || PAUSE_ACTION.equals(action)) { |
642 | 610 | pause(); |
643 | - mResumeAfterCall = false; | |
611 | + mPausedByTransientLossOfFocus = false; | |
644 | 612 | } else if (CMDSTOP.equals(cmd)) { |
645 | 613 | pause(); |
646 | - mResumeAfterCall = false; | |
614 | + mPausedByTransientLossOfFocus = false; | |
647 | 615 | seek(0); |
648 | 616 | } |
649 | 617 | } |
@@ -663,9 +631,9 @@ public class MediaPlaybackService extends Service { | ||
663 | 631 | // Take a snapshot of the current playlist |
664 | 632 | saveQueue(true); |
665 | 633 | |
666 | - if (isPlaying() || mResumeAfterCall) { | |
634 | + if (isPlaying() || mPausedByTransientLossOfFocus) { | |
667 | 635 | // something is currently playing, or will be playing once |
668 | - // an in-progress call ends, so don't stop the service now. | |
636 | + // an in-progress action requesting audio focus ends, so don't stop the service now. | |
669 | 637 | return true; |
670 | 638 | } |
671 | 639 |
@@ -687,7 +655,7 @@ public class MediaPlaybackService extends Service { | ||
687 | 655 | @Override |
688 | 656 | public void handleMessage(Message msg) { |
689 | 657 | // Check again to make sure nothing is playing right now |
690 | - if (isPlaying() || mResumeAfterCall || mServiceInUse | |
658 | + if (isPlaying() || mPausedByTransientLossOfFocus || mServiceInUse | |
691 | 659 | || mMediaplayerHandler.hasMessages(TRACK_ENDED)) { |
692 | 660 | return; |
693 | 661 | } |