Commit 42c9b244 by olly Committed by Oliver Woodman

Make context non-optional in AudioFocusManager

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=217897796
parent 0834d128
...@@ -99,7 +99,7 @@ public final class AudioFocusManager { ...@@ -99,7 +99,7 @@ public final class AudioFocusManager {
private static final float VOLUME_MULTIPLIER_DUCK = 0.2f; private static final float VOLUME_MULTIPLIER_DUCK = 0.2f;
private static final float VOLUME_MULTIPLIER_DEFAULT = 1.0f; private static final float VOLUME_MULTIPLIER_DEFAULT = 1.0f;
private final @Nullable AudioManager audioManager; private final AudioManager audioManager;
private final AudioFocusListener focusListener; private final AudioFocusListener focusListener;
private final PlayerControl playerControl; private final PlayerControl playerControl;
private @Nullable AudioAttributes audioAttributes; private @Nullable AudioAttributes audioAttributes;
...@@ -117,12 +117,9 @@ public final class AudioFocusManager { ...@@ -117,12 +117,9 @@ public final class AudioFocusManager {
* @param context The current context. * @param context The current context.
* @param playerControl A {@link PlayerControl} to handle commands from this instance. * @param playerControl A {@link PlayerControl} to handle commands from this instance.
*/ */
public AudioFocusManager(@Nullable Context context, PlayerControl playerControl) { public AudioFocusManager(Context context, PlayerControl playerControl) {
this.audioManager = this.audioManager =
context == null (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
? null
: (AudioManager)
context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
this.playerControl = playerControl; this.playerControl = playerControl;
this.focusListener = new AudioFocusListener(); this.focusListener = new AudioFocusListener();
this.audioFocusState = AUDIO_FOCUS_STATE_NO_FOCUS; this.audioFocusState = AUDIO_FOCUS_STATE_NO_FOCUS;
...@@ -148,8 +145,6 @@ public final class AudioFocusManager { ...@@ -148,8 +145,6 @@ public final class AudioFocusManager {
return PLAYER_COMMAND_PLAY_WHEN_READY; return PLAYER_COMMAND_PLAY_WHEN_READY;
} }
Assertions.checkNotNull(
audioManager, "SimpleExoPlayer must be created with a context to handle audio focus.");
if (!Util.areEqual(this.audioAttributes, audioAttributes)) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) {
this.audioAttributes = audioAttributes; this.audioAttributes = audioAttributes;
focusGain = convertAudioAttributesToFocusGain(audioAttributes); focusGain = convertAudioAttributesToFocusGain(audioAttributes);
...@@ -177,10 +172,6 @@ public final class AudioFocusManager { ...@@ -177,10 +172,6 @@ public final class AudioFocusManager {
* @return A {@link PlayerCommand} to execute on the player. * @return A {@link PlayerCommand} to execute on the player.
*/ */
public @PlayerCommand int handlePrepare(boolean playWhenReady) { public @PlayerCommand int handlePrepare(boolean playWhenReady) {
if (audioManager == null) {
return PLAYER_COMMAND_PLAY_WHEN_READY;
}
return playWhenReady ? requestAudioFocus() : PLAYER_COMMAND_DO_NOT_PLAY; return playWhenReady ? requestAudioFocus() : PLAYER_COMMAND_DO_NOT_PLAY;
} }
...@@ -192,10 +183,6 @@ public final class AudioFocusManager { ...@@ -192,10 +183,6 @@ public final class AudioFocusManager {
* @return A {@link PlayerCommand} to execute on the player. * @return A {@link PlayerCommand} to execute on the player.
*/ */
public @PlayerCommand int handleSetPlayWhenReady(boolean playWhenReady, int playerState) { public @PlayerCommand int handleSetPlayWhenReady(boolean playWhenReady, int playerState) {
if (audioManager == null) {
return PLAYER_COMMAND_PLAY_WHEN_READY;
}
if (!playWhenReady) { if (!playWhenReady) {
abandonAudioFocus(); abandonAudioFocus();
return PLAYER_COMMAND_DO_NOT_PLAY; return PLAYER_COMMAND_DO_NOT_PLAY;
...@@ -209,10 +196,6 @@ public final class AudioFocusManager { ...@@ -209,10 +196,6 @@ public final class AudioFocusManager {
/** Called by the player as part of {@link ExoPlayer#stop(boolean)}. */ /** Called by the player as part of {@link ExoPlayer#stop(boolean)}. */
public void handleStop() { public void handleStop() {
if (audioManager == null) {
return;
}
abandonAudioFocus(/* forceAbandon= */ true); abandonAudioFocus(/* forceAbandon= */ true);
} }
...@@ -271,7 +254,6 @@ public final class AudioFocusManager { ...@@ -271,7 +254,6 @@ public final class AudioFocusManager {
} }
private int requestAudioFocusDefault() { private int requestAudioFocusDefault() {
AudioManager audioManager = Assertions.checkNotNull(this.audioManager);
return audioManager.requestAudioFocus( return audioManager.requestAudioFocus(
focusListener, focusListener,
Util.getStreamTypeForAudioUsage(Assertions.checkNotNull(audioAttributes).usage), Util.getStreamTypeForAudioUsage(Assertions.checkNotNull(audioAttributes).usage),
...@@ -296,17 +278,17 @@ public final class AudioFocusManager { ...@@ -296,17 +278,17 @@ public final class AudioFocusManager {
rebuildAudioFocusRequest = false; rebuildAudioFocusRequest = false;
} }
return Assertions.checkNotNull(audioManager).requestAudioFocus(audioFocusRequest); return audioManager.requestAudioFocus(audioFocusRequest);
} }
private void abandonAudioFocusDefault() { private void abandonAudioFocusDefault() {
Assertions.checkNotNull(audioManager).abandonAudioFocus(focusListener); audioManager.abandonAudioFocus(focusListener);
} }
@RequiresApi(26) @RequiresApi(26)
private void abandonAudioFocusV26() { private void abandonAudioFocusV26() {
if (audioFocusRequest != null) { if (audioFocusRequest != null) {
Assertions.checkNotNull(audioManager).abandonAudioFocusRequest(audioFocusRequest); audioManager.abandonAudioFocusRequest(audioFocusRequest);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment