package androidx.media3.exoplayer;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.media3.common.audio.AudioManagerCompat;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.BackgroundThreadStateHandler;
import androidx.media3.common.util.Clock;
import androidx.media3.common.util.Log;
import androidx.media3.exoplayer.StreamVolumeManager;

/* JADX INFO: loaded from: classes.dex */
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 AudioManager audioManager;
    private final Listener listener;

    @Nullable
    private VolumeChangeReceiver receiver;
    private final BackgroundThreadStateHandler<StreamVolumeState> stateHandler;
    private int volumeBeforeMute;

    public interface Listener {
        void onStreamTypeChanged(int i10);

        void onStreamVolumeChanged(int i10, boolean z10);
    }

    public static final class StreamVolumeState {
        public final int maxVolume;
        public final int minVolume;
        public final boolean muted;
        public final int streamType;
        public final int volume;

        public StreamVolumeState(int i10, int i11, boolean z10, int i12, int i13) {
            this.streamType = i10;
            this.volume = i11;
            this.muted = z10;
            this.minVolume = i12;
            this.maxVolume = i13;
        }
    }

    public final class VolumeChangeReceiver extends BroadcastReceiver {
        private VolumeChangeReceiver() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public /* synthetic */ void lambda$onReceive$0() {
            if (StreamVolumeManager.this.receiver == null) {
                return;
            }
            StreamVolumeManager.this.stateHandler.setStateInBackground(StreamVolumeManager.this.generateState(((StreamVolumeState) StreamVolumeManager.this.stateHandler.get()).streamType));
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            StreamVolumeManager.this.stateHandler.runInBackground(new b(this, 4));
        }
    }

    public StreamVolumeManager(Context context, Listener listener, final int i10, Looper looper, Looper looper2, Clock clock) {
        this.applicationContext = context.getApplicationContext();
        this.listener = listener;
        BackgroundThreadStateHandler<StreamVolumeState> backgroundThreadStateHandler = new BackgroundThreadStateHandler<>(new StreamVolumeState(i10, 0, false, 0, 0), looper, looper2, clock, new t(this, 10));
        this.stateHandler = backgroundThreadStateHandler;
        backgroundThreadStateHandler.runInBackground(new Runnable() { // from class: androidx.media3.exoplayer.s0
            @Override // java.lang.Runnable
            public final void run() {
                this.f3144a.lambda$new$0(i10);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public StreamVolumeState generateState(int i10) {
        Assertions.checkNotNull(this.audioManager);
        return new StreamVolumeState(i10, AudioManagerCompat.getStreamVolume(this.audioManager, i10), AudioManagerCompat.isStreamMute(this.audioManager, i10), AudioManagerCompat.getStreamMinVolume(this.audioManager, i10), AudioManagerCompat.getStreamMaxVolume(this.audioManager, i10));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ StreamVolumeState lambda$decreaseVolume$7(StreamVolumeState streamVolumeState) {
        int i10 = streamVolumeState.streamType;
        int i11 = streamVolumeState.volume;
        int i12 = streamVolumeState.minVolume;
        return new StreamVolumeState(i10, i11 > i12 ? i11 - 1 : i12, i11 <= 1, i12, streamVolumeState.maxVolume);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$decreaseVolume$8(int i10, StreamVolumeState streamVolumeState) {
        if (streamVolumeState.volume <= streamVolumeState.minVolume) {
            return streamVolumeState;
        }
        ((AudioManager) Assertions.checkNotNull(this.audioManager)).adjustStreamVolume(streamVolumeState.streamType, -1, i10);
        return generateState(streamVolumeState.streamType);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ StreamVolumeState lambda$increaseVolume$5(StreamVolumeState streamVolumeState) {
        int i10 = streamVolumeState.streamType;
        int i11 = streamVolumeState.volume;
        int i12 = streamVolumeState.maxVolume;
        return new StreamVolumeState(i10, i11 < i12 ? i11 + 1 : i12, false, streamVolumeState.minVolume, i12);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$increaseVolume$6(int i10, StreamVolumeState streamVolumeState) {
        if (streamVolumeState.volume >= streamVolumeState.maxVolume) {
            return streamVolumeState;
        }
        ((AudioManager) Assertions.checkNotNull(this.audioManager)).adjustStreamVolume(streamVolumeState.streamType, 1, i10);
        return generateState(streamVolumeState.streamType);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$new$0(int i10) {
        this.audioManager = (AudioManager) Assertions.checkStateNotNull((AudioManager) this.applicationContext.getSystemService("audio"));
        VolumeChangeReceiver volumeChangeReceiver = new VolumeChangeReceiver();
        try {
            this.applicationContext.registerReceiver(volumeChangeReceiver, new IntentFilter(VOLUME_CHANGED_ACTION));
            this.receiver = volumeChangeReceiver;
        } catch (RuntimeException e10) {
            Log.w(TAG, "Error registering stream volume receiver", e10);
        }
        this.stateHandler.setStateInBackground(generateState(i10));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ StreamVolumeState lambda$release$11(StreamVolumeState streamVolumeState) {
        return streamVolumeState;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$release$12(StreamVolumeState streamVolumeState) {
        VolumeChangeReceiver volumeChangeReceiver = this.receiver;
        if (volumeChangeReceiver != null) {
            try {
                this.applicationContext.unregisterReceiver(volumeChangeReceiver);
            } catch (RuntimeException e10) {
                Log.w(TAG, "Error unregistering stream volume receiver", e10);
            }
            this.receiver = null;
        }
        return streamVolumeState;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$setMuted$10(boolean z10, int i10, StreamVolumeState streamVolumeState) {
        if (streamVolumeState.muted == z10) {
            return streamVolumeState;
        }
        Assertions.checkNotNull(this.audioManager);
        this.audioManager.adjustStreamVolume(streamVolumeState.streamType, z10 ? -100 : 100, i10);
        return generateState(streamVolumeState.streamType);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$setMuted$9(boolean z10, StreamVolumeState streamVolumeState) {
        return new StreamVolumeState(streamVolumeState.streamType, streamVolumeState.muted == z10 ? streamVolumeState.volume : z10 ? 0 : this.volumeBeforeMute, z10, streamVolumeState.minVolume, streamVolumeState.maxVolume);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ StreamVolumeState lambda$setStreamType$1(int i10, StreamVolumeState streamVolumeState) {
        return new StreamVolumeState(i10, streamVolumeState.volume, streamVolumeState.muted, streamVolumeState.minVolume, streamVolumeState.maxVolume);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$setStreamType$2(int i10, StreamVolumeState streamVolumeState) {
        return streamVolumeState.streamType == i10 ? streamVolumeState : generateState(i10);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ StreamVolumeState lambda$setVolume$3(int i10, StreamVolumeState streamVolumeState) {
        int i11 = streamVolumeState.streamType;
        int i12 = streamVolumeState.minVolume;
        return new StreamVolumeState(i11, (i10 < i12 || i10 > streamVolumeState.maxVolume) ? streamVolumeState.volume : i10, i10 == 0, i12, streamVolumeState.maxVolume);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ StreamVolumeState lambda$setVolume$4(int i10, int i11, StreamVolumeState streamVolumeState) {
        if (i10 == streamVolumeState.volume || i10 < streamVolumeState.minVolume || i10 > streamVolumeState.maxVolume) {
            return streamVolumeState;
        }
        ((AudioManager) Assertions.checkNotNull(this.audioManager)).setStreamVolume(streamVolumeState.streamType, i10, i11);
        return generateState(streamVolumeState.streamType);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void onStreamVolumeStateChanged(StreamVolumeState streamVolumeState, StreamVolumeState streamVolumeState2) {
        boolean z10 = streamVolumeState.muted;
        if (!z10 && streamVolumeState2.muted) {
            this.volumeBeforeMute = streamVolumeState.volume;
        }
        int i10 = streamVolumeState.volume;
        int i11 = streamVolumeState2.volume;
        if (i10 != i11 || z10 != streamVolumeState2.muted) {
            this.listener.onStreamVolumeChanged(i11, streamVolumeState2.muted);
        }
        int i12 = streamVolumeState.streamType;
        int i13 = streamVolumeState2.streamType;
        if (i12 == i13 && streamVolumeState.minVolume == streamVolumeState2.minVolume && streamVolumeState.maxVolume == streamVolumeState2.maxVolume) {
            return;
        }
        this.listener.onStreamTypeChanged(i13);
    }

    @SuppressLint({"WrongConstant"})
    public void decreaseVolume(int i10) {
        this.stateHandler.updateStateAsync(new q0(1), new r0(i10, 0, this));
    }

    public int getMaxVolume() {
        return this.stateHandler.get().maxVolume;
    }

    public int getMinVolume() {
        return this.stateHandler.get().minVolume;
    }

    public int getVolume() {
        return this.stateHandler.get().volume;
    }

    @SuppressLint({"WrongConstant"})
    public void increaseVolume(int i10) {
        this.stateHandler.updateStateAsync(new q0(2), new r0(i10, 1, this));
    }

    public boolean isMuted() {
        return this.stateHandler.get().muted;
    }

    public void release() {
        this.stateHandler.updateStateAsync(new q0(0), new o(this, 2));
    }

    @SuppressLint({"WrongConstant"})
    public void setMuted(final boolean z10, final int i10) {
        this.stateHandler.updateStateAsync(new com.google.common.base.e() { // from class: androidx.media3.exoplayer.t0
            @Override // com.google.common.base.e
            public final Object apply(Object obj) {
                return this.f3225a.lambda$setMuted$9(z10, (StreamVolumeManager.StreamVolumeState) obj);
            }
        }, new com.google.common.base.e() { // from class: androidx.media3.exoplayer.u0
            @Override // com.google.common.base.e
            public final Object apply(Object obj) {
                return this.f3242a.lambda$setMuted$10(z10, i10, (StreamVolumeManager.StreamVolumeState) obj);
            }
        });
    }

    public void setStreamType(int i10) {
        this.stateHandler.updateStateAsync(new q(i10, 4), new r0(i10, 2, this));
    }

    @SuppressLint({"WrongConstant"})
    public void setVolume(final int i10, final int i11) {
        this.stateHandler.updateStateAsync(new q(i10, 3), new com.google.common.base.e() { // from class: androidx.media3.exoplayer.p0
            @Override // com.google.common.base.e
            public final Object apply(Object obj) {
                return this.f3124a.lambda$setVolume$4(i10, i11, (StreamVolumeManager.StreamVolumeState) obj);
            }
        });
    }
}
