package D0;

import C3.AbstractC0055q;
import C3.W;
import C3.b0;
import C3.k0;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioProfile;
import android.media.AudioTrack;
import android.os.Build;
import android.provider.Settings;
import android.util.Pair;
import android.util.SparseArray;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import u0.C1567c;
import u0.C1579o;
import v0.AbstractC1664c;

/* JADX INFO: renamed from: D0.d, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0067d {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final C0067d f1537c = new C0067d(C3.G.p(C0066c.f1533d));

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

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

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

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

    static {
        Object[] objArr = {2, 5, 6};
        AbstractC0055q.a(3, objArr);
        f1538d = C3.G.i(3, objArr);
        A1.H h8 = new A1.H(4);
        h8.l(5, 6);
        h8.l(17, 6);
        h8.l(7, 6);
        h8.l(30, 10);
        h8.l(18, 6);
        h8.l(6, 8);
        h8.l(8, 8);
        h8.l(14, 8);
        f1539e = h8.d();
    }

    public C0067d(W w4) {
        for (int i = 0; i < w4.f1227t; i++) {
            C0066c c0066c = (C0066c) w4.get(i);
            this.f1540a.put(c0066c.f1534a, c0066c);
        }
        int iMax = 0;
        for (int i5 = 0; i5 < this.f1540a.size(); i5++) {
            iMax = Math.max(iMax, ((C0066c) this.f1540a.valueAt(i5)).f1535b);
        }
        this.f1541b = iMax;
    }

    public static W a(int[] iArr, int i) {
        C3.D dJ = C3.G.j();
        if (iArr == null) {
            iArr = new int[0];
        }
        for (int i5 : iArr) {
            dJ.a(new C0066c(i5, i));
        }
        return dJ.g();
    }

    /* JADX WARN: Removed duplicated region for block: B:84:0x023b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static C0067d b(Context context, Intent intent, C1567c c1567c, K4.c cVar) {
        K4.c cVar2;
        int i = 4;
        AudioManager audioManagerQ = AbstractC1664c.q(context);
        if (cVar != null) {
            cVar2 = cVar;
        } else {
            cVar2 = null;
            if (x0.v.f18553a >= 33) {
                try {
                    List audioDevicesForAttributes = audioManagerQ.getAudioDevicesForAttributes((AudioAttributes) c1567c.a().f14409q);
                    if (!audioDevicesForAttributes.isEmpty()) {
                        cVar2 = new K4.c(i, (AudioDeviceInfo) audioDevicesForAttributes.get(0));
                    }
                } catch (RuntimeException unused) {
                }
            }
        }
        int i5 = x0.v.f18553a;
        b0 b0Var = f1539e;
        if (i5 >= 33 && (x0.v.K(context) || (i5 >= 23 && context.getPackageManager().hasSystemFeature("android.hardware.type.automotive")))) {
            List directProfilesForAttributes = audioManagerQ.getDirectProfilesForAttributes((AudioAttributes) c1567c.a().f14409q);
            HashMap map = new HashMap();
            map.put(2, new HashSet(W3.b.a(12)));
            for (int i8 = 0; i8 < directProfilesForAttributes.size(); i8++) {
                AudioProfile audioProfileB = C0.j.b(directProfilesForAttributes.get(i8));
                if (audioProfileB.getEncapsulationType() != 1) {
                    int format = audioProfileB.getFormat();
                    if (x0.v.H(format) || b0Var.containsKey(Integer.valueOf(format))) {
                        if (map.containsKey(Integer.valueOf(format))) {
                            Set set = (Set) map.get(Integer.valueOf(format));
                            set.getClass();
                            set.addAll(W3.b.a(audioProfileB.getChannelMasks()));
                        } else {
                            map.put(Integer.valueOf(format), new HashSet(W3.b.a(audioProfileB.getChannelMasks())));
                        }
                    }
                }
            }
            C3.D dJ = C3.G.j();
            for (Map.Entry entry : map.entrySet()) {
                dJ.a(new C0066c(((Integer) entry.getKey()).intValue(), (Set) entry.getValue()));
            }
            return new C0067d(dJ.g());
        }
        if (i5 >= 23) {
            AudioDeviceInfo[] devices = cVar2 == null ? audioManagerQ.getDevices(2) : new AudioDeviceInfo[]{(AudioDeviceInfo) cVar2.f3604r};
            C3.I i9 = new C3.I(4);
            i9.b(8, 7);
            if (i5 >= 31) {
                i9.b(26, 27);
            }
            if (i5 >= 33) {
                i9.a(30);
            }
            C3.J jG = i9.g();
            for (AudioDeviceInfo audioDeviceInfo : devices) {
                if (jG.contains(Integer.valueOf(audioDeviceInfo.getType()))) {
                    return f1537c;
                }
            }
        }
        C3.I i10 = new C3.I(4);
        i10.a(2);
        int i11 = x0.v.f18553a;
        if (i11 >= 29 && (x0.v.K(context) || (i11 >= 23 && context.getPackageManager().hasSystemFeature("android.hardware.type.automotive")))) {
            C3.D dJ2 = C3.G.j();
            k0 it = b0Var.keySet().iterator();
            while (it.hasNext()) {
                Integer num = (Integer) it.next();
                int iIntValue = num.intValue();
                if (x0.v.f18553a >= x0.v.p(iIntValue) && AudioTrack.isDirectPlaybackSupported(new AudioFormat.Builder().setChannelMask(12).setEncoding(iIntValue).setSampleRate(48000).build(), (AudioAttributes) c1567c.a().f14409q)) {
                    dJ2.a(num);
                }
            }
            dJ2.a(2);
            W wG = dJ2.g();
            wG.getClass();
            i10.d(wG);
            return new C0067d(a(W3.b.w(i10.g()), 10));
        }
        ContentResolver contentResolver = context.getContentResolver();
        boolean z8 = Settings.Global.getInt(contentResolver, "use_external_surround_sound_flag", 0) == 1;
        if (!z8) {
            String str = Build.MANUFACTURER;
            if (str.equals("Amazon") || str.equals("Xiaomi")) {
                if (Settings.Global.getInt(contentResolver, "external_surround_sound_enabled", 0) == 1) {
                    W w4 = f1538d;
                    w4.getClass();
                    i10.d(w4);
                }
            }
        }
        if (intent == null || z8 || intent.getIntExtra("android.media.extra.AUDIO_PLUG_STATE", 0) != 1) {
            return new C0067d(a(W3.b.w(i10.g()), 10));
        }
        int[] intArrayExtra = intent.getIntArrayExtra("android.media.extra.ENCODINGS");
        if (intArrayExtra != null) {
            List listA = W3.b.a(intArrayExtra);
            listA.getClass();
            i10.d(listA);
        }
        return new C0067d(a(W3.b.w(i10.g()), intent.getIntExtra("android.media.extra.MAX_CHANNEL_COUNT", 10)));
    }

    public static C0067d c(Context context, C1567c c1567c, K4.c cVar) {
        return b(context, context.registerReceiver(null, new IntentFilter("android.media.action.HDMI_AUDIO_PLUG")), c1567c, cVar);
    }

    /* JADX WARN: Removed duplicated region for block: B:72:0x0101  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Pair d(C1579o c1579o, C1567c c1567c) {
        String str = c1579o.f17358n;
        str.getClass();
        int iC = u0.E.c(str, c1579o.f17355k);
        Integer numValueOf = Integer.valueOf(iC);
        b0 b0Var = f1539e;
        if (!b0Var.containsKey(numValueOf)) {
            return null;
        }
        SparseArray sparseArray = this.f1540a;
        int i = 6;
        i = 6;
        i = 6;
        if (iC == 18 && !x0.v.k(sparseArray, 18)) {
            iC = 6;
        } else if ((iC == 8 && !x0.v.k(sparseArray, 8)) || (iC == 30 && !x0.v.k(sparseArray, 30))) {
            iC = 7;
        }
        if (!x0.v.k(sparseArray, iC)) {
            return null;
        }
        C0066c c0066c = (C0066c) sparseArray.get(iC);
        c0066c.getClass();
        int iIntValue = 0;
        zContains = false;
        boolean zContains = false;
        int i5 = 10;
        int i8 = c0066c.f1535b;
        C3.J j8 = c0066c.f1536c;
        int i9 = c1579o.D;
        if (i9 == -1 || iC == 18) {
            int i10 = c1579o.f17337E;
            if (i10 == -1) {
                i10 = 48000;
            }
            if (j8 != null) {
                iIntValue = i8;
            } else {
                int i11 = x0.v.f18553a;
                int i12 = c0066c.f1534a;
                if (i11 >= 29) {
                    while (true) {
                        if (i5 <= 0) {
                            break;
                        }
                        int iR = x0.v.r(i5);
                        if (iR != 0 && AudioTrack.isDirectPlaybackSupported(new AudioFormat.Builder().setEncoding(i12).setSampleRate(i10).setChannelMask(iR).build(), (AudioAttributes) c1567c.a().f14409q)) {
                            iIntValue = i5;
                            break;
                        }
                        i5--;
                    }
                } else {
                    Object obj = b0Var.get(Integer.valueOf(i12));
                    iIntValue = ((Integer) (obj != null ? obj : 0)).intValue();
                }
            }
            i9 = iIntValue;
        } else if (!c1579o.f17358n.equals("audio/vnd.dts.uhd;profile=p2") || x0.v.f18553a >= 33) {
            if (j8 != null) {
                int iR2 = x0.v.r(i9);
                if (iR2 != 0) {
                    zContains = j8.contains(Integer.valueOf(iR2));
                }
            } else if (i9 <= i8) {
                zContains = true;
            }
            if (!zContains) {
                return null;
            }
        } else if (i9 > 10) {
            return null;
        }
        int i13 = x0.v.f18553a;
        if (i13 > 28) {
            i = i9;
        } else if (i9 == 7) {
            i = 8;
        } else if (i9 != 3 && i9 != 4 && i9 != 5) {
        }
        if (i13 <= 26 && "fugu".equals(Build.DEVICE) && i == 1) {
            i = 2;
        }
        int iR3 = x0.v.r(i);
        if (iR3 == 0) {
            return null;
        }
        return Pair.create(Integer.valueOf(iC), Integer.valueOf(iR3));
    }

    public final boolean equals(Object obj) {
        boolean zContentEquals;
        if (this != obj) {
            if (obj instanceof C0067d) {
                C0067d c0067d = (C0067d) obj;
                SparseArray sparseArray = this.f1540a;
                SparseArray sparseArray2 = c0067d.f1540a;
                int i = x0.v.f18553a;
                if (sparseArray != null) {
                    if (sparseArray2 != null) {
                        if (x0.v.f18553a >= 31) {
                            zContentEquals = sparseArray.contentEquals(sparseArray2);
                        } else {
                            int size = sparseArray.size();
                            if (size == sparseArray2.size()) {
                                for (int i5 = 0; i5 < size; i5++) {
                                    if (Objects.equals(sparseArray.valueAt(i5), sparseArray2.get(sparseArray.keyAt(i5)))) {
                                    }
                                }
                                zContentEquals = true;
                            }
                        }
                    }
                    zContentEquals = false;
                    break;
                } else {
                    if (sparseArray2 != null) {
                        zContentEquals = false;
                        break;
                    }
                    zContentEquals = true;
                }
                if (!zContentEquals || this.f1541b != c0067d.f1541b) {
                }
            }
            return false;
        }
        return true;
    }

    public final int hashCode() {
        int iContentHashCode;
        SparseArray sparseArray = this.f1540a;
        if (x0.v.f18553a >= 31) {
            iContentHashCode = sparseArray.contentHashCode();
        } else {
            int iHashCode = 17;
            for (int i = 0; i < sparseArray.size(); i++) {
                iHashCode = Objects.hashCode(sparseArray.valueAt(i)) + ((sparseArray.keyAt(i) + (iHashCode * 31)) * 31);
            }
            iContentHashCode = iHashCode;
        }
        return (iContentHashCode * 31) + this.f1541b;
    }

    public final String toString() {
        return "AudioCapabilities[maxChannelCount=" + this.f1541b + ", audioProfiles=" + this.f1540a + "]";
    }
}
