Bug 1357639 - part2 : use enum for audio focus states. r=sebastian
☠☠ backed out by 28500031e15e ☠ ☠
authorAlastor Wu <alwu@mozilla.com>
Tue, 02 May 2017 18:53:13 +0800
changeset 356002 48e901332b4def6534def84fb58aef717c7fb92b
parent 356001 ecf487cbca1b9fd4bea1af1f8e0350bbd9a7a078
child 356003 3e7bc9a792c290b49c32ec2c645ec27f1f7a2cee
push id31754
push userkwierso@gmail.com
push dateWed, 03 May 2017 00:28:51 +0000
treeherdermozilla-central@5eaf2d70eded [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1357639
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1357639 - part2 : use enum for audio focus states. r=sebastian MozReview-Commit-ID: AQ2R6zxnONU
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);
+    }
 }