package z0;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class l implements h {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public h f19145A;

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

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final ArrayList f19147r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final h f19148s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public p f19149t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public C1778b f19150u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public e f19151v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public h f19152w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public C1776A f19153x;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public w f19155z;

    public l(Context context, h hVar) {
        this.f19146q = context.getApplicationContext();
        hVar.getClass();
        this.f19148s = hVar;
        this.f19147r = new ArrayList();
    }

    public static void j(h hVar, y yVar) {
        if (hVar != null) {
            hVar.k(yVar);
        }
    }

    @Override // z0.h
    public final void close() {
        h hVar = this.f19145A;
        if (hVar != null) {
            try {
                hVar.close();
            } finally {
                this.f19145A = null;
            }
        }
    }

    @Override // z0.h
    public final Uri e() {
        h hVar = this.f19145A;
        if (hVar == null) {
            return null;
        }
        return hVar.e();
    }

    @Override // z0.h
    public final long g(k kVar) {
        AbstractC1709a.j(this.f19145A == null);
        String scheme = kVar.f19138a.getScheme();
        int i = x0.v.f18553a;
        Uri uri = kVar.f19138a;
        String scheme2 = uri.getScheme();
        boolean zIsEmpty = TextUtils.isEmpty(scheme2);
        Context context = this.f19146q;
        if (zIsEmpty || "file".equals(scheme2)) {
            String path = uri.getPath();
            if (path == null || !path.startsWith("/android_asset/")) {
                if (this.f19149t == null) {
                    p pVar = new p(false);
                    this.f19149t = pVar;
                    h(pVar);
                }
                this.f19145A = this.f19149t;
            } else {
                if (this.f19150u == null) {
                    C1778b c1778b = new C1778b(context);
                    this.f19150u = c1778b;
                    h(c1778b);
                }
                this.f19145A = this.f19150u;
            }
        } else if ("asset".equals(scheme)) {
            if (this.f19150u == null) {
                C1778b c1778b2 = new C1778b(context);
                this.f19150u = c1778b2;
                h(c1778b2);
            }
            this.f19145A = this.f19150u;
        } else if ("content".equals(scheme)) {
            if (this.f19151v == null) {
                e eVar = new e(context);
                this.f19151v = eVar;
                h(eVar);
            }
            this.f19145A = this.f19151v;
        } else {
            boolean zEquals = "rtmp".equals(scheme);
            h hVar = this.f19148s;
            if (zEquals) {
                if (this.f19152w == null) {
                    try {
                        h hVar2 = (h) Class.forName("androidx.media3.datasource.rtmp.RtmpDataSource").getConstructor(null).newInstance(null);
                        this.f19152w = hVar2;
                        h(hVar2);
                    } catch (ClassNotFoundException unused) {
                        AbstractC1709a.B("DefaultDataSource", "Attempting to play RTMP stream without depending on the RTMP extension");
                    } catch (Exception e8) {
                        throw new RuntimeException("Error instantiating RTMP extension", e8);
                    }
                    if (this.f19152w == null) {
                        this.f19152w = hVar;
                    }
                }
                this.f19145A = this.f19152w;
            } else if ("udp".equals(scheme)) {
                if (this.f19153x == null) {
                    C1776A c1776a = new C1776A();
                    this.f19153x = c1776a;
                    h(c1776a);
                }
                this.f19145A = this.f19153x;
            } else if ("data".equals(scheme)) {
                if (this.f19154y == null) {
                    f fVar = new f(false);
                    this.f19154y = fVar;
                    h(fVar);
                }
                this.f19145A = this.f19154y;
            } else if ("rawresource".equals(scheme) || "android.resource".equals(scheme)) {
                if (this.f19155z == null) {
                    w wVar = new w(context);
                    this.f19155z = wVar;
                    h(wVar);
                }
                this.f19145A = this.f19155z;
            } else {
                this.f19145A = hVar;
            }
        }
        return this.f19145A.g(kVar);
    }

    public final void h(h hVar) {
        int i = 0;
        while (true) {
            ArrayList arrayList = this.f19147r;
            if (i >= arrayList.size()) {
                return;
            }
            hVar.k((y) arrayList.get(i));
            i++;
        }
    }

    @Override // z0.h
    public final void k(y yVar) {
        yVar.getClass();
        this.f19148s.k(yVar);
        this.f19147r.add(yVar);
        j(this.f19149t, yVar);
        j(this.f19150u, yVar);
        j(this.f19151v, yVar);
        j(this.f19152w, yVar);
        j(this.f19153x, yVar);
        j(this.f19154y, yVar);
        j(this.f19155z, yVar);
    }

    @Override // z0.h
    public final Map m() {
        h hVar = this.f19145A;
        return hVar == null ? Collections.EMPTY_MAP : hVar.m();
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) {
        h hVar = this.f19145A;
        hVar.getClass();
        return hVar.p(bArr, i, i5);
    }
}
