package androidx.media3.ui;

import D1.C0078c;
import D1.C0079d;
import D1.K;
import D1.S;
import a.AbstractC0386a;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.CaptioningManager;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import w0.a;
import w0.b;
import w0.f;

/* JADX INFO: loaded from: classes.dex */
public final class SubtitleView extends FrameLayout {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public List f8957q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public C0079d f8958r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f8959s;

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

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

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

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

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

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

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

    public SubtitleView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f8957q = Collections.EMPTY_LIST;
        this.f8958r = C0079d.f1723g;
        this.f8959s = 0;
        this.f8960t = 0.0533f;
        this.f8961u = 0.08f;
        this.f8962v = true;
        this.f8963w = true;
        C0078c c0078c = new C0078c(context);
        this.f8965y = c0078c;
        this.f8966z = c0078c;
        addView(c0078c);
        this.f8964x = 1;
    }

    private List<b> getCuesWithStylingPreferencesApplied() {
        if (this.f8962v && this.f8963w) {
            return this.f8957q;
        }
        ArrayList arrayList = new ArrayList(this.f8957q.size());
        for (int i = 0; i < this.f8957q.size(); i++) {
            a aVarA = ((b) this.f8957q.get(i)).a();
            if (!this.f8962v) {
                aVarA.f18176n = false;
                CharSequence charSequence = aVarA.f18164a;
                if (charSequence instanceof Spanned) {
                    if (!(charSequence instanceof Spannable)) {
                        aVarA.f18164a = SpannableString.valueOf(charSequence);
                    }
                    CharSequence charSequence2 = aVarA.f18164a;
                    charSequence2.getClass();
                    Spannable spannable = (Spannable) charSequence2;
                    for (Object obj : spannable.getSpans(0, spannable.length(), Object.class)) {
                        if (!(obj instanceof f)) {
                            spannable.removeSpan(obj);
                        }
                    }
                }
                AbstractC0386a.y(aVarA);
            } else if (!this.f8963w) {
                AbstractC0386a.y(aVarA);
            }
            arrayList.add(aVarA.a());
        }
        return arrayList;
    }

    private float getUserCaptionFontScale() {
        CaptioningManager captioningManager;
        if (isInEditMode() || (captioningManager = (CaptioningManager) getContext().getSystemService("captioning")) == null || !captioningManager.isEnabled()) {
            return 1.0f;
        }
        return captioningManager.getFontScale();
    }

    private C0079d getUserCaptionStyle() {
        boolean zIsInEditMode = isInEditMode();
        C0079d c0079d = C0079d.f1723g;
        if (zIsInEditMode) {
            return c0079d;
        }
        CaptioningManager captioningManager = (CaptioningManager) getContext().getSystemService("captioning");
        if (captioningManager != null && captioningManager.isEnabled()) {
            CaptioningManager.CaptionStyle userStyle = captioningManager.getUserStyle();
            c0079d = new C0079d(userStyle.hasForegroundColor() ? userStyle.foregroundColor : -1, userStyle.hasBackgroundColor() ? userStyle.backgroundColor : -16777216, userStyle.hasWindowColor() ? userStyle.windowColor : 0, userStyle.hasEdgeType() ? userStyle.edgeType : 0, userStyle.hasEdgeColor() ? userStyle.edgeColor : -1, userStyle.getTypeface());
        }
        return c0079d;
    }

    private <T extends View & K> void setView(T t6) {
        removeView(this.f8966z);
        View view = this.f8966z;
        if (view instanceof S) {
            ((S) view).f1708r.destroy();
        }
        this.f8966z = t6;
        this.f8965y = t6;
        addView(t6);
    }

    public final void a() {
        setStyle(getUserCaptionStyle());
    }

    public final void b() {
        setFractionalTextSize(getUserCaptionFontScale() * 0.0533f);
    }

    public final void c() {
        this.f8965y.a(getCuesWithStylingPreferencesApplied(), this.f8958r, this.f8960t, this.f8959s, this.f8961u);
    }

    public void setApplyEmbeddedFontSizes(boolean z8) {
        this.f8963w = z8;
        c();
    }

    public void setApplyEmbeddedStyles(boolean z8) {
        this.f8962v = z8;
        c();
    }

    public void setBottomPaddingFraction(float f3) {
        this.f8961u = f3;
        c();
    }

    public void setCues(List<b> list) {
        if (list == null) {
            list = Collections.EMPTY_LIST;
        }
        this.f8957q = list;
        c();
    }

    public void setFractionalTextSize(float f3) {
        this.f8959s = 0;
        this.f8960t = f3;
        c();
    }

    public void setStyle(C0079d c0079d) {
        this.f8958r = c0079d;
        c();
    }

    public void setViewType(int i) {
        if (this.f8964x == i) {
            return;
        }
        if (i == 1) {
            setView(new C0078c(getContext()));
        } else {
            if (i != 2) {
                throw new IllegalArgumentException();
            }
            setView(new S(getContext()));
        }
        this.f8964x = i;
    }
}
