Bug 1357639 - part2 : use enum for audio focus states. draft
authorAlastor Wu <alwu@mozilla.com>
Mon, 08 May 2017 14:10:41 +0800
changeset 573964 24f910de830aac0bbadb5835ded0ec8badc42da5
parent 573963 7c544ea30bfc713e2b877f04427ca1fa4a2eee94
child 573965 9c98cf69f20f0212fadfa71076e30859d2e9510f
push id57550
push useralwu@mozilla.com
push dateMon, 08 May 2017 07:06:13 +0000
bugs1357639
milestone55.0a1
Bug 1357639 - part2 : use enum for audio focus states. MozReview-Commit-ID: LwLxs7WS5XF
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
@@ -4,31 +4,34 @@ import org.mozilla.gecko.annotation.Robo
 import org.mozilla.gecko.annotation.WrapForJNI;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 
 import android.content.Context;
 import android.content.Intent;
 import android.media.AudioManager;
 import android.media.AudioManager.OnAudioFocusChangeListener;
+import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 
 public class AudioFocusAgent {
     private static final String LOGTAG = "AudioFocusAgent";
 
     private static Context mContext;
     private AudioManager mAudioManager;
     private OnAudioFocusChangeListener mAfChangeListener;
 
-    public static final String OWN_FOCUS = "own_focus";
-    public static final String LOST_FOCUS = "lost_focus";
-    public static final String LOST_FOCUS_TRANSIENT = "lost_focus_transient";
-    public static final String LOST_FOCUS_TRANSIENT_CAN_DUCK = "lost_focus_transient_can_duck";
+    public enum State {
+        OWN_FOCUS,
+        LOST_FOCUS,
+        LOST_FOCUS_TRANSIENT,
+        LOST_FOCUS_TRANSIENT_CAN_DUCK
+    }
 
-    private String mAudioFocusState = LOST_FOCUS;
+    private State mAudioFocusState = State.LOST_FOCUS;
 
     @WrapForJNI(calledFrom = "gecko")
     public static void notifyStartedPlaying() {
         if (!isAttachedToContext()) {
             return;
         }
         Log.d(LOGTAG, "NotifyStartedPlaying");
         AudioFocusAgent.getInstance().requestAudioFocusIfNeeded();
@@ -53,39 +56,39 @@ public class AudioFocusAgent {
 
         mAfChangeListener = new OnAudioFocusChangeListener() {
             public void onAudioFocusChange(int focusChange) {
                 switch (focusChange) {
                     case AudioManager.AUDIOFOCUS_LOSS:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS");
                         notifyObservers("audioFocusChanged", "lostAudioFocus");
                         notifyMediaControlService(MediaControlService.ACTION_PAUSE_BY_AUDIO_FOCUS);
-                        mAudioFocusState = LOST_FOCUS;
+                        mAudioFocusState = State.LOST_FOCUS;
                         break;
                     case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS_TRANSIENT");
                         notifyObservers("audioFocusChanged", "lostAudioFocusTransiently");
                         notifyMediaControlService(MediaControlService.ACTION_PAUSE_BY_AUDIO_FOCUS);
-                        mAudioFocusState = LOST_FOCUS_TRANSIENT;
+                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT;
                         break;
                     case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
                         Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
                         notifyMediaControlService(MediaControlService.ACTION_START_AUDIO_DUCK);
-                        mAudioFocusState = LOST_FOCUS_TRANSIENT_CAN_DUCK;
+                        mAudioFocusState = State.LOST_FOCUS_TRANSIENT_CAN_DUCK;
                         break;
                     case AudioManager.AUDIOFOCUS_GAIN:
-                        if (mAudioFocusState.equals(LOST_FOCUS_TRANSIENT_CAN_DUCK)) {
+                        if (mAudioFocusState.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(LOST_FOCUS_TRANSIENT)) {
+                        } else if (mAudioFocusState.equals(State.LOST_FOCUS_TRANSIENT)) {
                             Log.d(LOGTAG, "onAudioFocusChange, AUDIOFOCUS_GAIN");
                             notifyObservers("audioFocusChanged", "gainAudioFocus");
                             notifyMediaControlService(MediaControlService.ACTION_RESUME_BY_AUDIO_FOCUS);
                         }
-                        mAudioFocusState = OWN_FOCUS;
+                        mAudioFocusState = State.OWN_FOCUS;
                         break;
                     default:
                 }
             }
         };
         notifyMediaControlService(MediaControlService.ACTION_INIT);
     }
 
@@ -104,40 +107,50 @@ public class AudioFocusAgent {
 
     private void notifyObservers(String topic, String data) {
         GeckoAppShell.notifyObservers(topic, data);
     }
 
     private AudioFocusAgent() {}
 
     private void requestAudioFocusIfNeeded() {
-        if (mAudioFocusState.equals(OWN_FOCUS)) {
+        if (mAudioFocusState.equals(State.OWN_FOCUS)) {
             return;
         }
 
         int result = mAudioManager.requestAudioFocus(mAfChangeListener,
                                                      AudioManager.STREAM_MUSIC,
                                                      AudioManager.AUDIOFOCUS_GAIN);
 
         String focusMsg = (result == AudioManager.AUDIOFOCUS_GAIN) ?
             "AudioFocus request granted" : "AudioFoucs request failed";
         Log.d(LOGTAG, focusMsg);
         if (result == AudioManager.AUDIOFOCUS_GAIN) {
-            mAudioFocusState = OWN_FOCUS;
+            mAudioFocusState = State.OWN_FOCUS;
         }
     }
 
     private void abandonAudioFocusIfNeeded() {
-        if (!mAudioFocusState.equals(OWN_FOCUS)) {
+        if (!mAudioFocusState.equals(State.OWN_FOCUS)) {
             return;
         }
 
         Log.d(LOGTAG, "Abandon AudioFocus");
         mAudioManager.abandonAudioFocus(mAfChangeListener);
-        mAudioFocusState = LOST_FOCUS;
+        mAudioFocusState = State.LOST_FOCUS;
     }
 
     private void notifyMediaControlService(String action) {
         Intent intent = new Intent(mContext, MediaControlService.class);
         intent.setAction(action);
         mContext.startService(intent);
     }
+
+    @VisibleForTesting
+    public State getAudioFocusState() {
+        return mAudioFocusState;
+    }
+
+    @VisibleForTesting
+    public void changeAudioFocus(int focusChange) {
+        mAfChangeListener.onAudioFocusChange(focusChange);
+    }
 }