Rabbit-R1/switch port/java/sources/androidx/media3/exoplayer/AudioFocusManager.java
2024-05-21 17:08:36 -04:00

275 lines
9.8 KiB
Java

package androidx.media3.exoplayer;
import android.content.Context;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Handler;
import androidx.media3.common.AudioAttributes;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.AudioFocusManager;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/* loaded from: classes2.dex */
final class AudioFocusManager {
private static final int AUDIOFOCUS_GAIN = 1;
private static final int AUDIOFOCUS_GAIN_TRANSIENT = 2;
private static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4;
private static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3;
private static final int AUDIOFOCUS_NONE = 0;
private static final int AUDIO_FOCUS_STATE_HAVE_FOCUS = 1;
private static final int AUDIO_FOCUS_STATE_LOSS_TRANSIENT = 2;
private static final int AUDIO_FOCUS_STATE_LOSS_TRANSIENT_DUCK = 3;
private static final int AUDIO_FOCUS_STATE_NO_FOCUS = 0;
public static final int PLAYER_COMMAND_DO_NOT_PLAY = -1;
public static final int PLAYER_COMMAND_PLAY_WHEN_READY = 1;
public static final int PLAYER_COMMAND_WAIT_FOR_CALLBACK = 0;
private static final String TAG = "AudioFocusManager";
private static final float VOLUME_MULTIPLIER_DEFAULT = 1.0f;
private static final float VOLUME_MULTIPLIER_DUCK = 0.2f;
private AudioAttributes audioAttributes;
private AudioFocusRequest audioFocusRequest;
private final AudioManager audioManager;
private int focusGainToRequest;
private final AudioFocusListener focusListener;
private PlayerControl playerControl;
private boolean rebuildAudioFocusRequest;
private float volumeMultiplier = 1.0f;
private int audioFocusState = 0;
@Target({ElementType.TYPE_USE})
@Documented
@Retention(RetentionPolicy.SOURCE)
/* loaded from: classes2.dex */
public @interface PlayerCommand {
}
/* loaded from: classes2.dex */
public interface PlayerControl {
void executePlayerCommand(int i);
void setVolumeMultiplier(float f);
}
private boolean shouldAbandonAudioFocusIfHeld(int i) {
return i == 1 || this.focusGainToRequest != 1;
}
AudioManager.OnAudioFocusChangeListener getFocusListener() {
return this.focusListener;
}
public float getVolumeMultiplier() {
return this.volumeMultiplier;
}
public AudioFocusManager(Context context, Handler handler, PlayerControl playerControl) {
this.audioManager = (AudioManager) Assertions.checkNotNull((AudioManager) context.getApplicationContext().getSystemService("audio"));
this.playerControl = playerControl;
this.focusListener = new AudioFocusListener(handler);
}
public void setAudioAttributes(AudioAttributes audioAttributes) {
if (Util.areEqual(this.audioAttributes, audioAttributes)) {
return;
}
this.audioAttributes = audioAttributes;
int convertAudioAttributesToFocusGain = convertAudioAttributesToFocusGain(audioAttributes);
this.focusGainToRequest = convertAudioAttributesToFocusGain;
boolean z = true;
if (convertAudioAttributesToFocusGain != 1 && convertAudioAttributesToFocusGain != 0) {
z = false;
}
Assertions.checkArgument(z, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME.");
}
public int updateAudioFocus(boolean z, int i) {
if (shouldAbandonAudioFocusIfHeld(i)) {
abandonAudioFocusIfHeld();
return z ? 1 : -1;
}
if (z) {
return requestAudioFocus();
}
return -1;
}
public void release() {
this.playerControl = null;
abandonAudioFocusIfHeld();
}
private int requestAudioFocus() {
if (this.audioFocusState == 1) {
return 1;
}
if ((Util.SDK_INT >= 26 ? requestAudioFocusV26() : requestAudioFocusDefault()) == 1) {
setAudioFocusState(1);
return 1;
}
setAudioFocusState(0);
return -1;
}
private void abandonAudioFocusIfHeld() {
if (this.audioFocusState == 0) {
return;
}
if (Util.SDK_INT >= 26) {
abandonAudioFocusV26();
} else {
abandonAudioFocusDefault();
}
setAudioFocusState(0);
}
private int requestAudioFocusDefault() {
return this.audioManager.requestAudioFocus(this.focusListener, Util.getStreamTypeForAudioUsage(((AudioAttributes) Assertions.checkNotNull(this.audioAttributes)).usage), this.focusGainToRequest);
}
private int requestAudioFocusV26() {
AudioFocusRequest.Builder builder;
AudioFocusRequest audioFocusRequest = this.audioFocusRequest;
if (audioFocusRequest == null || this.rebuildAudioFocusRequest) {
if (audioFocusRequest == null) {
builder = new AudioFocusRequest.Builder(this.focusGainToRequest);
} else {
builder = new AudioFocusRequest.Builder(this.audioFocusRequest);
}
this.audioFocusRequest = builder.setAudioAttributes(((AudioAttributes) Assertions.checkNotNull(this.audioAttributes)).getAudioAttributesV21().audioAttributes).setWillPauseWhenDucked(willPauseWhenDucked()).setOnAudioFocusChangeListener(this.focusListener).build();
this.rebuildAudioFocusRequest = false;
}
return this.audioManager.requestAudioFocus(this.audioFocusRequest);
}
private void abandonAudioFocusDefault() {
this.audioManager.abandonAudioFocus(this.focusListener);
}
private void abandonAudioFocusV26() {
AudioFocusRequest audioFocusRequest = this.audioFocusRequest;
if (audioFocusRequest != null) {
this.audioManager.abandonAudioFocusRequest(audioFocusRequest);
}
}
private boolean willPauseWhenDucked() {
AudioAttributes audioAttributes = this.audioAttributes;
return audioAttributes != null && audioAttributes.contentType == 1;
}
private static int convertAudioAttributesToFocusGain(AudioAttributes audioAttributes) {
if (audioAttributes == null) {
return 0;
}
switch (audioAttributes.usage) {
case 0:
Log.w(TAG, "Specify a proper usage in the audio attributes for audio focus handling. Using AUDIOFOCUS_GAIN by default.");
return 1;
case 1:
case 14:
return 1;
case 2:
case 4:
return 2;
case 3:
return 0;
case 11:
if (audioAttributes.contentType == 1) {
return 2;
}
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 12:
case 13:
return 3;
case 15:
default:
Log.w(TAG, "Unidentified audio usage: " + audioAttributes.usage);
return 0;
case 16:
return Util.SDK_INT >= 19 ? 4 : 2;
}
}
private void setAudioFocusState(int i) {
if (this.audioFocusState == i) {
return;
}
this.audioFocusState = i;
float f = i == 3 ? 0.2f : 1.0f;
if (this.volumeMultiplier == f) {
return;
}
this.volumeMultiplier = f;
PlayerControl playerControl = this.playerControl;
if (playerControl != null) {
playerControl.setVolumeMultiplier(f);
}
}
/* JADX INFO: Access modifiers changed from: private */
public void handlePlatformAudioFocusChange(int i) {
if (i == -3 || i == -2) {
if (i == -2 || willPauseWhenDucked()) {
executePlayerCommand(0);
setAudioFocusState(2);
return;
} else {
setAudioFocusState(3);
return;
}
}
if (i == -1) {
executePlayerCommand(-1);
abandonAudioFocusIfHeld();
} else if (i == 1) {
setAudioFocusState(1);
executePlayerCommand(1);
} else {
Log.w(TAG, "Unknown focus change type: " + i);
}
}
private void executePlayerCommand(int i) {
PlayerControl playerControl = this.playerControl;
if (playerControl != null) {
playerControl.executePlayerCommand(i);
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public class AudioFocusListener implements AudioManager.OnAudioFocusChangeListener {
private final Handler eventHandler;
public AudioFocusListener(Handler handler) {
this.eventHandler = handler;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: lambda$onAudioFocusChange$0$androidx-media3-exoplayer-AudioFocusManager$AudioFocusListener, reason: not valid java name */
public /* synthetic */ void m5197x93d291e3(int i) {
AudioFocusManager.this.handlePlatformAudioFocusChange(i);
}
@Override // android.media.AudioManager.OnAudioFocusChangeListener
public void onAudioFocusChange(final int i) {
this.eventHandler.post(new Runnable() { // from class: androidx.media3.exoplayer.AudioFocusManager$AudioFocusListener$$ExternalSyntheticLambda0
@Override // java.lang.Runnable
public final void run() {
AudioFocusManager.AudioFocusListener.this.m5197x93d291e3(i);
}
});
}
}
}