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

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioProfile;
import android.media.AudioTrack;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Pair;
import android.util.SparseArray;
import androidx.annotation.Nullable;
import androidx.media3.common.MimeTypes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final ok2 f15094c = new ok2(zzguf.o(nk2.f14659c));

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final zzguf f15095d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final zzgui f15096e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final SparseArray f15097a = new SparseArray();

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

    static {
        Object[] objArr = {2, 5, 6};
        ih1.t(3, objArr);
        f15095d = zzguf.u(3, objArr);
        ds1 ds1Var = new ds1(4);
        ds1Var.a(5, 6);
        ds1Var.a(17, 6);
        ds1Var.a(7, 6);
        ds1Var.a(30, 10);
        ds1Var.a(18, 6);
        ds1Var.a(6, 8);
        ds1Var.a(8, 8);
        ds1Var.a(14, 8);
        f15096e = ds1Var.d(true);
    }

    public ok2(zzguf zzgufVar) {
        for (int i10 = 0; i10 < zzgufVar.size(); i10++) {
            nk2 nk2Var = (nk2) zzgufVar.get(i10);
            this.f15097a.put(nk2Var.f14660a, nk2Var);
        }
        int iMax = 0;
        for (int i11 = 0; i11 < this.f15097a.size(); i11++) {
            iMax = Math.max(iMax, ((nk2) this.f15097a.valueAt(i11)).f14661b);
        }
        this.f15098b = iMax;
    }

    @SuppressLint({"UnprotectedReceiver"})
    public static ok2 zza(Context context, ud0 ud0Var, @Nullable AudioDeviceInfo audioDeviceInfo) {
        return zzb(context, context.registerReceiver(null, new IntentFilter("android.media.action.HDMI_AUDIO_PLUG")), ud0Var, audioDeviceInfo);
    }

    /* JADX WARN: Removed duplicated region for block: B:92:0x026e  */
    @SuppressLint({"InlinedApi"})
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static ok2 zzb(Context context, @Nullable Intent intent, ud0 ud0Var, @Nullable AudioDeviceInfo audioDeviceInfo) {
        AudioDeviceInfo audioDeviceInfo2;
        AudioManager audioManagerA = q50.a(context);
        if (audioDeviceInfo != null) {
            audioDeviceInfo2 = audioDeviceInfo;
        } else {
            audioDeviceInfo2 = null;
            if (Build.VERSION.SDK_INT >= 33) {
                List audioDevicesForAttributes = audioManagerA.getAudioDevicesForAttributes(ud0Var.a());
                if (!audioDevicesForAttributes.isEmpty()) {
                    audioDeviceInfo2 = (AudioDeviceInfo) audioDevicesForAttributes.get(0);
                }
            }
        }
        int i10 = Build.VERSION.SDK_INT;
        zzgui zzguiVar = f15096e;
        if (i10 >= 33 && (bb1.g(context) || context.getPackageManager().hasSystemFeature("android.hardware.type.automotive"))) {
            List directProfilesForAttributes = audioManagerA.getDirectProfilesForAttributes(ud0Var.a());
            HashMap map = new HashMap();
            map.put(2, new HashSet(za2.R(12)));
            for (int i11 = 0; i11 < directProfilesForAttributes.size(); i11++) {
                AudioProfile audioProfileE = androidx.media3.exoplayer.analytics.a0.e(directProfilesForAttributes.get(i11));
                if (audioProfileE.getEncapsulationType() != 1) {
                    int format = audioProfileE.getFormat();
                    if (bb1.a(format) || zzguiVar.containsKey(Integer.valueOf(format))) {
                        Integer numValueOf = Integer.valueOf(format);
                        if (map.containsKey(numValueOf)) {
                            Set set = (Set) map.get(numValueOf);
                            set.getClass();
                            set.addAll(za2.R(audioProfileE.getChannelMasks()));
                        } else {
                            map.put(numValueOf, new HashSet(za2.R(audioProfileE.getChannelMasks())));
                        }
                    }
                }
            }
            bs1 bs1Var = zzguf.f19771b;
            as1 as1Var = new as1(4);
            for (Map.Entry entry : map.entrySet()) {
                as1Var.a(new nk2(((Integer) entry.getKey()).intValue(), (Set) entry.getValue()));
            }
            return new ok2(as1Var.g());
        }
        AudioDeviceInfo[] devices = audioDeviceInfo2 == null ? audioManagerA.getDevices(2) : new AudioDeviceInfo[]{audioDeviceInfo2};
        gs1 gs1Var = new gs1(4);
        Integer[] numArr = {8, 7};
        if (gs1Var.f11866d != null) {
            for (int i12 = 0; i12 < 2; i12++) {
                gs1Var.g(numArr[i12]);
            }
        } else {
            ih1.t(2, numArr);
            gs1Var.e(2);
            System.arraycopy(numArr, 0, gs1Var.f19374a, gs1Var.f19375b, 2);
            gs1Var.f19375b += 2;
        }
        if (i10 >= 31) {
            Integer[] numArr2 = {26, 27};
            if (gs1Var.f11866d != null) {
                for (int i13 = 0; i13 < 2; i13++) {
                    gs1Var.g(numArr2[i13]);
                }
            } else {
                ih1.t(2, numArr2);
                gs1Var.e(2);
                System.arraycopy(numArr2, 0, gs1Var.f19374a, gs1Var.f19375b, 2);
                gs1Var.f19375b += 2;
            }
        }
        if (i10 >= 33) {
            gs1Var.g(30);
        }
        zzgup zzgupVarI = gs1Var.i();
        for (AudioDeviceInfo audioDeviceInfo3 : devices) {
            if (zzgupVarI.contains(Integer.valueOf(audioDeviceInfo3.getType()))) {
                return f15094c;
            }
        }
        gs1 gs1Var2 = new gs1(4);
        gs1Var2.g(2);
        if (Build.VERSION.SDK_INT < 29 || !(bb1.g(context) || context.getPackageManager().hasSystemFeature("android.hardware.type.automotive"))) {
            ContentResolver contentResolver = context.getContentResolver();
            boolean z10 = Settings.Global.getInt(contentResolver, "use_external_surround_sound_flag", 0) == 1;
            if (!z10) {
                String str = Build.MANUFACTURER;
                if (str.equals("Amazon") || str.equals("Xiaomi")) {
                    if (Settings.Global.getInt(contentResolver, "external_surround_sound_enabled", 0) == 1) {
                        gs1Var2.h(f15095d);
                    }
                }
            }
            if (intent == null || z10 || intent.getIntExtra("android.media.extra.AUDIO_PLUG_STATE", 0) != 1) {
                return new ok2(zzf(za2.O(gs1Var2.i()), 10));
            }
            int[] intArrayExtra = intent.getIntArrayExtra("android.media.extra.ENCODINGS");
            if (intArrayExtra != null) {
                gs1Var2.h(za2.R(intArrayExtra));
            }
            return new ok2(zzf(za2.O(gs1Var2.i()), intent.getIntExtra("android.media.extra.MAX_CHANNEL_COUNT", 10)));
        }
        bs1 bs1Var2 = zzguf.f19771b;
        as1 as1Var2 = new as1(4);
        zzgup zzgupVar = zzguiVar.f19773b;
        if (zzgupVar == null) {
            zzgwf zzgwfVar = (zzgwf) zzguiVar;
            zzgwd zzgwdVar = new zzgwd(zzgwfVar, new zzgwe(zzgwfVar.f19799e, 0, zzgwfVar.f19800f));
            zzguiVar.f19773b = zzgwdVar;
            zzgupVar = zzgwdVar;
        }
        ct1 ct1VarC = zzgupVar.c();
        while (ct1VarC.hasNext()) {
            int iIntValue = ((Integer) ct1VarC.next()).intValue();
            if (Build.VERSION.SDK_INT >= bb1.c(iIntValue) && AudioTrack.isDirectPlaybackSupported(new AudioFormat.Builder().setChannelMask(12).setEncoding(iIntValue).setSampleRate(48000).build(), ud0Var.a())) {
                as1Var2.a(Integer.valueOf(iIntValue));
            }
        }
        as1Var2.a(2);
        gs1Var2.h(as1Var2.g());
        return new ok2(zzf(za2.O(gs1Var2.i()), 10));
    }

    @Nullable
    public static Uri zzc() {
        String str = Build.MANUFACTURER;
        if (str.equals("Amazon") || str.equals("Xiaomi")) {
            return Settings.Global.getUriFor("external_surround_sound_enabled");
        }
        return null;
    }

    private static zzguf zzf(@Nullable int[] iArr, int i10) {
        bs1 bs1Var = zzguf.f19771b;
        as1 as1Var = new as1(4);
        if (iArr == null) {
            iArr = new int[0];
        }
        for (int i11 : iArr) {
            as1Var.a(new nk2(i11, i10));
        }
        return as1Var.g();
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x0047 A[RETURN] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean equals(@Nullable Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ok2)) {
            return false;
        }
        ok2 ok2Var = (ok2) obj;
        SparseArray sparseArray = this.f15097a;
        SparseArray sparseArray2 = ok2Var.f15097a;
        String str = bb1.f9474a;
        if (Build.VERSION.SDK_INT < 31) {
            int size = sparseArray.size();
            if (size == sparseArray2.size()) {
                for (int i10 = 0; i10 < size; i10++) {
                    if (!Objects.equals(sparseArray.valueAt(i10), sparseArray2.get(sparseArray.keyAt(i10)))) {
                        break;
                    }
                }
                if (this.f15098b != ok2Var.f15098b) {
                    break;
                }
            }
        } else if (sparseArray.contentEquals(sparseArray2)) {
            if (this.f15098b != ok2Var.f15098b) {
                return true;
            }
        }
        return false;
    }

    public final int hashCode() {
        int iContentHashCode;
        String str = bb1.f9474a;
        int i10 = Build.VERSION.SDK_INT;
        SparseArray sparseArray = this.f15097a;
        if (i10 >= 31) {
            iContentHashCode = sparseArray.contentHashCode();
        } else {
            int iHashCode = 17;
            for (int i11 = 0; i11 < sparseArray.size(); i11++) {
                iHashCode = Objects.hashCode(sparseArray.valueAt(i11)) + ((sparseArray.keyAt(i11) + (iHashCode * 31)) * 31);
            }
            iContentHashCode = iHashCode;
        }
        return (iContentHashCode * 31) + this.f15098b;
    }

    public final String toString() {
        String string = this.f15097a.toString();
        int i10 = this.f15098b;
        StringBuilder sb = new StringBuilder(String.valueOf(i10).length() + 50 + string.length() + 1);
        sb.append("AudioCapabilities[maxChannelCount=");
        sb.append(i10);
        sb.append(", audioProfiles=");
        sb.append(string);
        sb.append("]");
        return sb.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x002f  */
    /* JADX WARN: Removed duplicated region for block: B:16:0x0039 A[PHI: r1
      0x0039: PHI (r1v3 int) = (r1v2 int), (r1v6 int) binds: [B:11:0x002d, B:14:0x0035] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:51:0x0093  */
    @Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Pair zzd(zn2 zn2Var, ud0 ud0Var) {
        String str = zn2Var.zzo;
        str.getClass();
        int iZzg = hc.zzg(str, zn2Var.zzk);
        if (!f15096e.containsKey(Integer.valueOf(iZzg))) {
            return null;
        }
        SparseArray sparseArray = this.f15097a;
        int i10 = 8;
        if (iZzg != 18) {
            if (iZzg == 8) {
                if (sparseArray.indexOfKey(8) >= 0) {
                    iZzg = 8;
                    if (iZzg == 30) {
                    }
                }
                iZzg = 7;
            } else if (iZzg == 30 && sparseArray.indexOfKey(30) < 0) {
                iZzg = 7;
            }
        } else if (sparseArray.indexOfKey(18) >= 0) {
            iZzg = 18;
            if (iZzg == 8) {
            }
        } else {
            iZzg = 6;
        }
        if (sparseArray.indexOfKey(iZzg) < 0) {
            return null;
        }
        nk2 nk2Var = (nk2) sparseArray.get(iZzg);
        nk2Var.getClass();
        int iB = zn2Var.f19348u;
        if (iB == -1 || iZzg == 18) {
            int i11 = zn2Var.f19349v;
            if (i11 == -1) {
                i11 = 48000;
            }
            iB = nk2Var.b(i11, ud0Var);
        } else if (!str.equals(MimeTypes.AUDIO_DTS_X) || Build.VERSION.SDK_INT >= 33) {
            if (!nk2Var.a(iB)) {
                return null;
            }
        } else if (iB > 10) {
            return null;
        }
        int i12 = Build.VERSION.SDK_INT;
        if (i12 > 28) {
            i10 = iB;
        } else if (iB != 7) {
            if (iB == 3 || iB == 4 || iB == 5) {
                i10 = 6;
            }
        }
        if (i12 <= 26 && "fugu".equals(Build.DEVICE) && i10 == 1) {
            i10 = 2;
        }
        int iB2 = bb1.b(i10);
        if (iB2 != 0) {
            return Pair.create(Integer.valueOf(iZzg), Integer.valueOf(iB2));
        }
        return null;
    }
}
