Rabbit-R1/original r1/java/sources/androidx/media3/exoplayer/StreamVolumeManager.java

172 lines
6 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package androidx.media3.exoplayer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Handler;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class StreamVolumeManager {
private static final String TAG = "StreamVolumeManager";
private static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
private final Context applicationContext;
private final AudioManager audioManager;
private final Handler eventHandler;
private final Listener listener;
private boolean muted;
private VolumeChangeReceiver receiver;
private int streamType;
private int volume;
/* loaded from: classes2.dex */
public interface Listener {
void onStreamTypeChanged(int i);
void onStreamVolumeChanged(int i, boolean z);
}
public int getVolume() {
return this.volume;
}
public boolean isMuted() {
return this.muted;
}
public StreamVolumeManager(Context context, Handler handler, Listener listener) {
Context applicationContext = context.getApplicationContext();
this.applicationContext = applicationContext;
this.eventHandler = handler;
this.listener = listener;
AudioManager audioManager = (AudioManager) Assertions.checkStateNotNull((AudioManager) applicationContext.getSystemService("audio"));
this.audioManager = audioManager;
this.streamType = 3;
this.volume = getVolumeFromManager(audioManager, 3);
this.muted = getMutedFromManager(audioManager, this.streamType);
VolumeChangeReceiver volumeChangeReceiver = new VolumeChangeReceiver();
try {
applicationContext.registerReceiver(volumeChangeReceiver, new IntentFilter(VOLUME_CHANGED_ACTION));
this.receiver = volumeChangeReceiver;
} catch (RuntimeException e) {
Log.w(TAG, "Error registering stream volume receiver", e);
}
}
public void setStreamType(int i) {
if (this.streamType == i) {
return;
}
this.streamType = i;
updateVolumeAndNotifyIfChanged();
this.listener.onStreamTypeChanged(i);
}
public int getMinVolume() {
if (Util.SDK_INT >= 28) {
return this.audioManager.getStreamMinVolume(this.streamType);
}
return 0;
}
public int getMaxVolume() {
return this.audioManager.getStreamMaxVolume(this.streamType);
}
public void setVolume(int i, int i2) {
if (i < getMinVolume() || i > getMaxVolume()) {
return;
}
this.audioManager.setStreamVolume(this.streamType, i, i2);
updateVolumeAndNotifyIfChanged();
}
public void increaseVolume(int i) {
if (this.volume >= getMaxVolume()) {
return;
}
this.audioManager.adjustStreamVolume(this.streamType, 1, i);
updateVolumeAndNotifyIfChanged();
}
public void decreaseVolume(int i) {
if (this.volume <= getMinVolume()) {
return;
}
this.audioManager.adjustStreamVolume(this.streamType, -1, i);
updateVolumeAndNotifyIfChanged();
}
public void setMuted(boolean z, int i) {
if (Util.SDK_INT >= 23) {
this.audioManager.adjustStreamVolume(this.streamType, z ? -100 : 100, i);
} else {
this.audioManager.setStreamMute(this.streamType, z);
}
updateVolumeAndNotifyIfChanged();
}
public void release() {
VolumeChangeReceiver volumeChangeReceiver = this.receiver;
if (volumeChangeReceiver != null) {
try {
this.applicationContext.unregisterReceiver(volumeChangeReceiver);
} catch (RuntimeException e) {
Log.w(TAG, "Error unregistering stream volume receiver", e);
}
this.receiver = null;
}
}
/* JADX INFO: Access modifiers changed from: private */
public void updateVolumeAndNotifyIfChanged() {
int volumeFromManager = getVolumeFromManager(this.audioManager, this.streamType);
boolean mutedFromManager = getMutedFromManager(this.audioManager, this.streamType);
if (this.volume == volumeFromManager && this.muted == mutedFromManager) {
return;
}
this.volume = volumeFromManager;
this.muted = mutedFromManager;
this.listener.onStreamVolumeChanged(volumeFromManager, mutedFromManager);
}
private static int getVolumeFromManager(AudioManager audioManager, int i) {
try {
return audioManager.getStreamVolume(i);
} catch (RuntimeException e) {
Log.w(TAG, "Could not retrieve stream volume for stream type " + i, e);
return audioManager.getStreamMaxVolume(i);
}
}
private static boolean getMutedFromManager(AudioManager audioManager, int i) {
if (Util.SDK_INT >= 23) {
return audioManager.isStreamMute(i);
}
return getVolumeFromManager(audioManager, i) == 0;
}
/* loaded from: classes2.dex */
private final class VolumeChangeReceiver extends BroadcastReceiver {
private VolumeChangeReceiver() {
}
@Override // android.content.BroadcastReceiver
public void onReceive(Context context, Intent intent) {
Handler handler = StreamVolumeManager.this.eventHandler;
final StreamVolumeManager streamVolumeManager = StreamVolumeManager.this;
handler.post(new Runnable() { // from class: androidx.media3.exoplayer.StreamVolumeManager$VolumeChangeReceiver$$ExternalSyntheticLambda0
@Override // java.lang.Runnable
public final void run() {
StreamVolumeManager.this.updateVolumeAndNotifyIfChanged();
}
});
}
}
}