Bug 1357639 - part3 : change audio focus state before notifying observers. draft
authorAlastor Wu <alwu@mozilla.com>
Mon, 08 May 2017 14:10:43 +0800
changeset 573965 9c98cf69f20f0212fadfa71076e30859d2e9510f
parent 573964 24f910de830aac0bbadb5835ded0ec8badc42da5
child 573966 39e42b2a94e353c99d4576add43c72c4bfc277a3
push id57550
push useralwu@mozilla.com
push dateMon, 08 May 2017 07:06:13 +0000
bugs1357639
milestone55.0a1
Bug 1357639 - part3 : change audio focus state before notifying observers. Notify observer might cause the method (notifyStoppedPlaying) is called by C++ side, and we should change our internal state before calling the method. MozReview-Commit-ID: 5xNXhGmAIrR
mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
@@ -54,41 +54,42 @@ public class AudioFocusAgent {
         mContext = context;
         mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
 
         mAfChangeListener = new OnAudioFocusChangeListener() {
             public void onAudioFocusChange(int focusChange) {
                 switch (focusChange) {
                     case AudioManager.AUDIOFOCUS_LOSS:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS");
+                        mAudioFocusState = State.LOST_FOCUS;
                         notifyObservers("audioFocusChanged", "lostAudioFocus");
                         notifyMediaControlService(MediaControlService.ACTION_PAUSE_BY_AUDIO_FOCUS);
-                        mAudioFocusState = State.LOST_FOCUS;
                         break;
                     case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS_TRANSIENT");
+                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT;
                         notifyObservers("audioFocusChanged", "lostAudioFocusTransiently");
                         notifyMediaControlService(MediaControlService.ACTION_PAUSE_BY_AUDIO_FOCUS);
-                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT;
                         break;
                     case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
+                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT_CAN_DUCK;
                         notifyMediaControlService(MediaControlService.ACTION_START_AUDIO_DUCK);
-                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT_CAN_DUCK;
                         break;
                     case AudioManager.AUDIOFOCUS_GAIN:
-                        if (mAudioFocusState.equals(State.LOST_FOCUS_TRANSIENT_CAN_DUCK)) {
+                        State state = mAudioFocusState;
+                        mAudioFocusState = State.OWN_FOCUS;
+                        if (state.equals(State.LOST_FOCUS_TRANSIENT_CAN_DUCK)) {
                             Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_GAIN (from DUCKING)");
                             notifyMediaControlService(MediaControlService.ACTION_STOP_AUDIO_DUCK);
-                        } else if (mAudioFocusState.equals(State.LOST_FOCUS_TRANSIENT)) {
+                        } else if (state.equals(State.LOST_FOCUS_TRANSIENT)) {
                             Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_GAIN");
                             notifyObservers("audioFocusChanged", "gainAudioFocus");
                             notifyMediaControlService(MediaControlService.ACTION_RESUME_BY_AUDIO_FOCUS);
                         }
-                        mAudioFocusState = State.OWN_FOCUS;
                         break;
                     default:
                 }
             }
         };
         notifyMediaControlService(MediaControlService.ACTION_INIT);
     }