From f895be6b1f7b0202802a3d1a196f36e9131b4db6 Mon Sep 17 00:00:00 2001 From: olly Date: Wed, 7 Nov 2018 11:35:23 -0800 Subject: [PATCH] AudioFocusManager - Treat idle state consistently ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=220500268 --- .../exoplayer2/ext/cast/ExoCastConstants.java | 83 ------------------- .../exoplayer2/audio/AudioFocusManager.java | 32 +++---- .../audio/AudioFocusManagerTest.java | 4 +- 3 files changed, 20 insertions(+), 99 deletions(-) delete mode 100644 extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/ExoCastConstants.java diff --git a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/ExoCastConstants.java b/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/ExoCastConstants.java deleted file mode 100644 index c5647dd949..0000000000 --- a/extensions/cast/src/main/java/com/google/android/exoplayer2/ext/cast/ExoCastConstants.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.ext.cast; - -/** Defines constants used by the Cast extension. */ -public final class ExoCastConstants { - - private ExoCastConstants() {} - - public static final int PROTOCOL_VERSION = 0; - - // String representations. - - public static final String STR_STATE_IDLE = "IDLE"; - public static final String STR_STATE_BUFFERING = "BUFFERING"; - public static final String STR_STATE_READY = "READY"; - public static final String STR_STATE_ENDED = "ENDED"; - - public static final String STR_REPEAT_MODE_OFF = "REPEAT_MODE_OFF"; - public static final String STR_REPEAT_MODE_ONE = "REPEAT_MODE_ONE"; - public static final String STR_REPEAT_MODE_ALL = "REPEAT_MODE_ALL"; - - // Methods. - - public static final String METHOD_BASE = "player."; - - public static final String METHOD_ADD_ITEMS = METHOD_BASE + "addItems"; - public static final String METHOD_MOVE_ITEM = METHOD_BASE + "moveItem"; - public static final String METHOD_REMOVE_ITEMS = METHOD_BASE + "removeItems"; - public static final String METHOD_SET_PLAY_WHEN_READY = METHOD_BASE + "setPlayWhenReady"; - public static final String METHOD_SET_REPEAT_MODE = METHOD_BASE + "setRepeatMode"; - public static final String METHOD_SET_SHUFFLE_MODE_ENABLED = - METHOD_BASE + "setShuffleModeEnabled"; - public static final String METHOD_SEEK_TO = METHOD_BASE + "seekTo"; - public static final String METHOD_SET_PLAYBACK_PARAMETERS = METHOD_BASE + "setPlaybackParameters"; - - // JSON message keys. - - public static final String KEY_PROTOCOL_VERSION = "protocolVersion"; - public static final String KEY_METHOD = "method"; - public static final String KEY_SEQUENCE_NUMBER = "sequenceNumber"; - public static final String KEY_INDEX = "index"; - public static final String KEY_ITEMS = "items"; - public static final String KEY_ARGS = "args"; - public static final String KEY_UUID = "uuid"; - public static final String KEY_UUIDS = "uuids"; - public static final String KEY_LICENSE_SERVER = "licenseServer"; - public static final String KEY_TITLE = "title"; - public static final String KEY_URI = "uri"; - public static final String KEY_REQUEST_HEADERS = "requestHeaders"; - public static final String KEY_DESCRIPTION = "description"; - public static final String KEY_MEDIA = "media"; - public static final String KEY_DRM_SCHEMES = "drmSchemes"; - public static final String KEY_START_POSITION_US = "startPositionUs"; - public static final String KEY_END_POSITION_US = "endPositionUs"; - public static final String KEY_MIME_TYPE = "mimeType"; - public static final String KEY_PLAY_WHEN_READY = "playWhenReady"; - public static final String KEY_REPEAT_MODE = "repeatMode"; - public static final String KEY_SHUFFLE_MODE_ENABLED = "shuffleModeEnabled"; - public static final String KEY_POSITION_MS = "positionMs"; - public static final String KEY_SPEED = "speed"; - public static final String KEY_PITCH = "pitch"; - public static final String KEY_SKIP_SILENCE = "skipSilence"; - public static final String KEY_PLAYBACK_STATE = "playbackState"; - public static final String KEY_MEDIA_QUEUE = "mediaQueue"; - public static final String KEY_IS_LOADING = "isLoading"; - public static final String KEY_PLAYBACK_ERROR = "error"; - public static final String KEY_PLAYBACK_POSITION = "playbackPosition"; - public static final String KEY_PLAYBACK_PARAMETERS = "playbackParameters"; -} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioFocusManager.java b/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioFocusManager.java index 09fd1f3f02..c20ede439f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioFocusManager.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/audio/AudioFocusManager.java @@ -139,7 +139,8 @@ public final class AudioFocusManager { * @param playerState The current player state; {@link ExoPlayer#getPlaybackState()}. * @return A {@link PlayerCommand} to execute on the player. */ - public @PlayerCommand int setAudioAttributes( + @PlayerCommand + public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; @@ -154,11 +155,9 @@ public final class AudioFocusManager { } } - if (playerState == Player.STATE_IDLE) { - return PLAYER_COMMAND_WAIT_FOR_CALLBACK; - } else { - return handlePrepare(playWhenReady); - } + return playerState == Player.STATE_IDLE + ? handleIdle(playWhenReady) + : handlePrepare(playWhenReady); } /** @@ -167,7 +166,8 @@ public final class AudioFocusManager { * @param playWhenReady The current state of {@link ExoPlayer#getPlayWhenReady()}. * @return A {@link PlayerCommand} to execute on the player. */ - public @PlayerCommand int handlePrepare(boolean playWhenReady) { + @PlayerCommand + public int handlePrepare(boolean playWhenReady) { return playWhenReady ? requestAudioFocus() : PLAYER_COMMAND_DO_NOT_PLAY; } @@ -178,16 +178,14 @@ public final class AudioFocusManager { * @param playerState The current state of the player. * @return A {@link PlayerCommand} to execute on the player. */ - public @PlayerCommand int handleSetPlayWhenReady(boolean playWhenReady, int playerState) { + @PlayerCommand + public int handleSetPlayWhenReady(boolean playWhenReady, int playerState) { if (!playWhenReady) { abandonAudioFocus(); return PLAYER_COMMAND_DO_NOT_PLAY; - } else if (playerState != Player.STATE_IDLE) { - return requestAudioFocus(); } - return focusGain != C.AUDIOFOCUS_NONE - ? PLAYER_COMMAND_WAIT_FOR_CALLBACK - : PLAYER_COMMAND_PLAY_WHEN_READY; + + return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : requestAudioFocus(); } /** Called by the player as part of {@link ExoPlayer#stop(boolean)}. */ @@ -197,7 +195,13 @@ public final class AudioFocusManager { // Internal methods. - private @PlayerCommand int requestAudioFocus() { + @PlayerCommand + private int handleIdle(boolean playWhenReady) { + return playWhenReady ? PLAYER_COMMAND_PLAY_WHEN_READY : PLAYER_COMMAND_DO_NOT_PLAY; + } + + @PlayerCommand + private int requestAudioFocus() { int focusRequestResult; if (focusGain == C.AUDIOFOCUS_NONE) { diff --git a/library/core/src/test/java/com/google/android/exoplayer2/audio/AudioFocusManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/audio/AudioFocusManagerTest.java index b175828d44..086c4ebc7f 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/audio/AudioFocusManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/audio/AudioFocusManagerTest.java @@ -58,7 +58,7 @@ public class AudioFocusManagerTest { assertThat( audioFocusManager.setAudioAttributes( /* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_IDLE)) - .isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK); + .isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY); assertThat( audioFocusManager.setAudioAttributes( /* audioAttributes= */ null, /* playWhenReady= */ true, Player.STATE_READY)) @@ -148,7 +148,7 @@ public class AudioFocusManagerTest { assertThat( audioFocusManager.setAudioAttributes( media, /* playWhenReady= */ true, Player.STATE_IDLE)) - .isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK); + .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); assertThat(Shadows.shadowOf(audioManager).getLastAudioFocusRequest()).isNull(); assertThat(audioFocusManager.handlePrepare(/* playWhenReady= */ true)) .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);