package com.google.android.gms.internal.ads;

import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioDeviceInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
import java.util.Objects;

/* JADX INFO: loaded from: classes2.dex */
public final class sk2 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f16467a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final rk2 f16468b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Handler f16469c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final pk2 f16470d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final e.i0 f16471e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public ud0 f16472f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f16473g;

    @Nullable
    private final qk2 zzf;

    @Nullable
    private ok2 zzg;

    @Nullable
    private AudioDeviceInfo zzh;

    public sk2(Context context, rk2 rk2Var, ud0 ud0Var, @Nullable AudioDeviceInfo audioDeviceInfo) {
        Context applicationContext = context.getApplicationContext();
        this.f16467a = applicationContext;
        this.f16468b = rk2Var;
        this.f16472f = ud0Var;
        this.zzh = audioDeviceInfo;
        String str = bb1.f9474a;
        Looper looperMyLooper = Looper.myLooper();
        Handler handler = new Handler(looperMyLooper == null ? Looper.getMainLooper() : looperMyLooper, null);
        this.f16469c = handler;
        this.f16470d = new pk2(this);
        this.f16471e = new e.i0(this);
        Uri uriZzc = ok2.zzc();
        this.zzf = uriZzc != null ? new qk2(this, handler, applicationContext.getContentResolver(), uriZzc) : null;
    }

    public final void a(ud0 ud0Var) {
        if (Objects.equals(ud0Var, this.f16472f)) {
            return;
        }
        this.f16472f = ud0Var;
        f(ok2.zza(this.f16467a, ud0Var, this.zzh));
    }

    public final ok2 b() {
        if (this.f16473g) {
            ok2 ok2Var = this.zzg;
            ok2Var.getClass();
            return ok2Var;
        }
        this.f16473g = true;
        qk2 qk2Var = this.zzf;
        if (qk2Var != null) {
            qk2Var.f15776a.registerContentObserver(qk2Var.f15777b, false, qk2Var);
        }
        Handler handler = this.f16469c;
        Context context = this.f16467a;
        q50.a(context).registerAudioDeviceCallback(this.f16470d, handler);
        ok2 ok2VarZzb = ok2.zzb(context, context.registerReceiver(this.f16471e, new IntentFilter("android.media.action.HDMI_AUDIO_PLUG"), null, handler), this.f16472f, this.zzh);
        this.zzg = ok2VarZzb;
        return ok2VarZzb;
    }

    public final void c() {
        if (this.f16473g) {
            this.zzg = null;
            Context context = this.f16467a;
            q50.a(context).unregisterAudioDeviceCallback(this.f16470d);
            context.unregisterReceiver(this.f16471e);
            qk2 qk2Var = this.zzf;
            if (qk2Var != null) {
                qk2Var.f15776a.unregisterContentObserver(qk2Var);
            }
            this.f16473g = false;
        }
    }

    public final /* synthetic */ AudioDeviceInfo d() {
        return this.zzh;
    }

    public final /* synthetic */ void e() {
        this.zzh = null;
    }

    public final void f(ok2 ok2Var) {
        if (!this.f16473g || ok2Var.equals(this.zzg)) {
            return;
        }
        this.zzg = ok2Var;
        em2 em2Var = ((bm2) this.f16468b).f9588a;
        em2Var.e();
        ok2 ok2Var2 = em2Var.f10859c;
        if (ok2Var2 == null || ok2Var.equals(ok2Var2)) {
            return;
        }
        em2Var.f10859c = ok2Var;
        lt0 lt0Var = em2Var.f10857a;
        if (lt0Var != null) {
            lt0Var.c(-1, am2.f8750a);
            lt0Var.d();
        }
    }

    public final void zzc(@Nullable AudioDeviceInfo audioDeviceInfo) {
        if (Objects.equals(audioDeviceInfo, this.zzh)) {
            return;
        }
        this.zzh = audioDeviceInfo;
        f(ok2.zza(this.f16467a, this.f16472f, audioDeviceInfo));
    }
}
