package androidx.appcompat.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.view.b1;

/* loaded from: classes.dex */
public class ActionBarContextView extends androidx.appcompat.widget.a {
    private View A;
    private View B;
    private LinearLayout C;
    private TextView D;
    private TextView E;
    private int F;
    private int G;
    private boolean H;
    private int I;

    /* renamed from: x, reason: collision with root package name */
    private CharSequence f801x;

    /* renamed from: y, reason: collision with root package name */
    private CharSequence f802y;

    /* renamed from: z, reason: collision with root package name */
    private View f803z;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements View.OnClickListener {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ n.b f804p;

        a(n.b bVar) {
            this.f804p = bVar;
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            this.f804p.c();
        }
    }

    public ActionBarContextView(Context context) {
        this(context, null);
    }

    private void i() {
        if (this.C == null) {
            LayoutInflater.from(getContext()).inflate(h.g.f20243a, this);
            LinearLayout linearLayout = (LinearLayout) getChildAt(getChildCount() - 1);
            this.C = linearLayout;
            this.D = (TextView) linearLayout.findViewById(h.f.f20221e);
            this.E = (TextView) this.C.findViewById(h.f.f20220d);
            if (this.F != 0) {
                this.D.setTextAppearance(getContext(), this.F);
            }
            if (this.G != 0) {
                this.E.setTextAppearance(getContext(), this.G);
            }
        }
        this.D.setText(this.f801x);
        this.E.setText(this.f802y);
        boolean z10 = !TextUtils.isEmpty(this.f801x);
        boolean z11 = !TextUtils.isEmpty(this.f802y);
        int i10 = 0;
        this.E.setVisibility(z11 ? 0 : 8);
        LinearLayout linearLayout2 = this.C;
        if (!z10 && !z11) {
            i10 = 8;
        }
        linearLayout2.setVisibility(i10);
        if (this.C.getParent() == null) {
            addView(this.C);
        }
    }

    @Override // androidx.appcompat.widget.a
    public /* bridge */ /* synthetic */ b1 f(int i10, long j10) {
        return super.f(i10, j10);
    }

    public void g() {
        if (this.f803z == null) {
            k();
        }
    }

    @Override // android.view.ViewGroup
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new ViewGroup.MarginLayoutParams(-1, -2);
    }

    @Override // android.view.ViewGroup
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attributeSet) {
        return new ViewGroup.MarginLayoutParams(getContext(), attributeSet);
    }

    @Override // androidx.appcompat.widget.a
    public /* bridge */ /* synthetic */ int getAnimatedVisibility() {
        return super.getAnimatedVisibility();
    }

    @Override // androidx.appcompat.widget.a
    public /* bridge */ /* synthetic */ int getContentHeight() {
        return super.getContentHeight();
    }

    public CharSequence getSubtitle() {
        return this.f802y;
    }

    public CharSequence getTitle() {
        return this.f801x;
    }

    public void h(n.b bVar) {
        View view = this.f803z;
        if (view == null) {
            View inflate = LayoutInflater.from(getContext()).inflate(this.I, (ViewGroup) this, false);
            this.f803z = inflate;
            addView(inflate);
        } else if (view.getParent() == null) {
            addView(this.f803z);
        }
        View findViewById = this.f803z.findViewById(h.f.f20225i);
        this.A = findViewById;
        findViewById.setOnClickListener(new a(bVar));
        androidx.appcompat.view.menu.e eVar = (androidx.appcompat.view.menu.e) bVar.e();
        c cVar = this.f971s;
        if (cVar != null) {
            cVar.A();
        }
        c cVar2 = new c(getContext());
        this.f971s = cVar2;
        cVar2.L(true);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-2, -1);
        eVar.c(this.f971s, this.f969q);
        ActionMenuView actionMenuView = (ActionMenuView) this.f971s.q(this);
        this.f970r = actionMenuView;
        androidx.core.view.s0.q0(actionMenuView, null);
        addView(this.f970r, layoutParams);
    }

    public boolean j() {
        return this.H;
    }

    public void k() {
        removeAllViews();
        this.B = null;
        this.f970r = null;
        this.f971s = null;
        View view = this.A;
        if (view != null) {
            view.setOnClickListener(null);
        }
    }

    public boolean l() {
        c cVar = this.f971s;
        if (cVar != null) {
            return cVar.M();
        }
        return false;
    }

    @Override // android.view.ViewGroup, android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        c cVar = this.f971s;
        if (cVar != null) {
            cVar.D();
            this.f971s.E();
        }
    }

    @Override // androidx.appcompat.widget.a, android.view.View
    public /* bridge */ /* synthetic */ boolean onHoverEvent(MotionEvent motionEvent) {
        return super.onHoverEvent(motionEvent);
    }

    @Override // android.view.ViewGroup, android.view.View
    protected void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        boolean b10 = a1.b(this);
        int paddingRight = b10 ? (i12 - i10) - getPaddingRight() : getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingTop2 = ((i13 - i11) - getPaddingTop()) - getPaddingBottom();
        View view = this.f803z;
        if (view != null && view.getVisibility() != 8) {
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) this.f803z.getLayoutParams();
            int i14 = b10 ? marginLayoutParams.rightMargin : marginLayoutParams.leftMargin;
            int i15 = b10 ? marginLayoutParams.leftMargin : marginLayoutParams.rightMargin;
            int d10 = androidx.appcompat.widget.a.d(paddingRight, i14, b10);
            paddingRight = androidx.appcompat.widget.a.d(d10 + e(this.f803z, d10, paddingTop, paddingTop2, b10), i15, b10);
        }
        int i16 = paddingRight;
        LinearLayout linearLayout = this.C;
        if (linearLayout != null && this.B == null && linearLayout.getVisibility() != 8) {
            i16 += e(this.C, i16, paddingTop, paddingTop2, b10);
        }
        int i17 = i16;
        View view2 = this.B;
        if (view2 != null) {
            e(view2, i17, paddingTop, paddingTop2, b10);
        }
        int paddingLeft = b10 ? getPaddingLeft() : (i12 - i10) - getPaddingRight();
        ActionMenuView actionMenuView = this.f970r;
        if (actionMenuView != null) {
            e(actionMenuView, paddingLeft, paddingTop, paddingTop2, !b10);
        }
    }

    @Override // android.view.View
    protected void onMeasure(int i10, int i11) {
        if (View.MeasureSpec.getMode(i10) != 1073741824) {
            throw new IllegalStateException(getClass().getSimpleName() + " can only be used with android:layout_width=\"match_parent\" (or fill_parent)");
        }
        if (View.MeasureSpec.getMode(i11) == 0) {
            throw new IllegalStateException(getClass().getSimpleName() + " can only be used with android:layout_height=\"wrap_content\"");
        }
        int size = View.MeasureSpec.getSize(i10);
        int i12 = this.f972t;
        if (i12 <= 0) {
            i12 = View.MeasureSpec.getSize(i11);
        }
        int paddingTop = getPaddingTop() + getPaddingBottom();
        int paddingLeft = (size - getPaddingLeft()) - getPaddingRight();
        int i13 = i12 - paddingTop;
        int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i13, Integer.MIN_VALUE);
        View view = this.f803z;
        if (view != null) {
            int c10 = c(view, paddingLeft, makeMeasureSpec, 0);
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) this.f803z.getLayoutParams();
            paddingLeft = c10 - (marginLayoutParams.leftMargin + marginLayoutParams.rightMargin);
        }
        ActionMenuView actionMenuView = this.f970r;
        if (actionMenuView != null && actionMenuView.getParent() == this) {
            paddingLeft = c(this.f970r, paddingLeft, makeMeasureSpec, 0);
        }
        LinearLayout linearLayout = this.C;
        if (linearLayout != null && this.B == null) {
            if (this.H) {
                this.C.measure(View.MeasureSpec.makeMeasureSpec(0, 0), makeMeasureSpec);
                int measuredWidth = this.C.getMeasuredWidth();
                boolean z10 = measuredWidth <= paddingLeft;
                if (z10) {
                    paddingLeft -= measuredWidth;
                }
                this.C.setVisibility(z10 ? 0 : 8);
            } else {
                paddingLeft = c(linearLayout, paddingLeft, makeMeasureSpec, 0);
            }
        }
        View view2 = this.B;
        if (view2 != null) {
            ViewGroup.LayoutParams layoutParams = view2.getLayoutParams();
            int i14 = layoutParams.width;
            int i15 = i14 != -2 ? 1073741824 : Integer.MIN_VALUE;
            if (i14 >= 0) {
                paddingLeft = Math.min(i14, paddingLeft);
            }
            int i16 = layoutParams.height;
            int i17 = i16 == -2 ? Integer.MIN_VALUE : 1073741824;
            if (i16 >= 0) {
                i13 = Math.min(i16, i13);
            }
            this.B.measure(View.MeasureSpec.makeMeasureSpec(paddingLeft, i15), View.MeasureSpec.makeMeasureSpec(i13, i17));
        }
        if (this.f972t > 0) {
            setMeasuredDimension(size, i12);
            return;
        }
        int childCount = getChildCount();
        int i18 = 0;
        for (int i19 = 0; i19 < childCount; i19++) {
            int measuredHeight = getChildAt(i19).getMeasuredHeight() + paddingTop;
            if (measuredHeight > i18) {
                i18 = measuredHeight;
            }
        }
        setMeasuredDimension(size, i18);
    }

    @Override // androidx.appcompat.widget.a, android.view.View
    public /* bridge */ /* synthetic */ boolean onTouchEvent(MotionEvent motionEvent) {
        return super.onTouchEvent(motionEvent);
    }

    @Override // androidx.appcompat.widget.a
    public void setContentHeight(int i10) {
        this.f972t = i10;
    }

    public void setCustomView(View view) {
        LinearLayout linearLayout;
        View view2 = this.B;
        if (view2 != null) {
            removeView(view2);
        }
        this.B = view;
        if (view != null && (linearLayout = this.C) != null) {
            removeView(linearLayout);
            this.C = null;
        }
        if (view != null) {
            addView(view);
        }
        requestLayout();
    }

    public void setSubtitle(CharSequence charSequence) {
        this.f802y = charSequence;
        i();
    }

    public void setTitle(CharSequence charSequence) {
        this.f801x = charSequence;
        i();
        androidx.core.view.s0.p0(this, charSequence);
    }

    public void setTitleOptional(boolean z10) {
        if (z10 != this.H) {
            requestLayout();
        }
        this.H = z10;
    }

    @Override // androidx.appcompat.widget.a, android.view.View
    public /* bridge */ /* synthetic */ void setVisibility(int i10) {
        super.setVisibility(i10);
    }

    @Override // android.view.ViewGroup
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    public ActionBarContextView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, h.a.f20153g);
    }

    public ActionBarContextView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        v0 v10 = v0.v(context, attributeSet, h.j.f20401y, i10, 0);
        androidx.core.view.s0.q0(this, v10.g(h.j.f20406z));
        this.F = v10.n(h.j.D, 0);
        this.G = v10.n(h.j.C, 0);
        this.f972t = v10.m(h.j.B, 0);
        this.I = v10.n(h.j.A, h.g.f20246d);
        v10.w();
    }
}
