package com.google.android.material.appbar;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.y;
import c1.g;
import c1.h;
import com.google.android.material.internal.m;
import com.google.android.material.internal.n;
import l0.j;
import l0.k;

/* JADX INFO: loaded from: classes.dex */
public class MaterialToolbar extends Toolbar {

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    private static final int f3546d0 = j.f5459n;

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    private static final ImageView.ScaleType[] f3547e0 = {ImageView.ScaleType.MATRIX, ImageView.ScaleType.FIT_XY, ImageView.ScaleType.FIT_START, ImageView.ScaleType.FIT_CENTER, ImageView.ScaleType.FIT_END, ImageView.ScaleType.CENTER, ImageView.ScaleType.CENTER_CROP, ImageView.ScaleType.CENTER_INSIDE};
    private Integer V;
    private boolean W;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    private boolean f3548a0;

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    private ImageView.ScaleType f3549b0;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    private Boolean f3550c0;

    public MaterialToolbar(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, l0.b.H);
    }

    private Pair<Integer, Integer> Q(TextView textView, TextView textView2) {
        int measuredWidth = getMeasuredWidth();
        int i3 = measuredWidth / 2;
        int paddingLeft = getPaddingLeft();
        int paddingRight = measuredWidth - getPaddingRight();
        for (int i4 = 0; i4 < getChildCount(); i4++) {
            View childAt = getChildAt(i4);
            if (childAt.getVisibility() != 8 && childAt != textView && childAt != textView2) {
                if (childAt.getRight() < i3 && childAt.getRight() > paddingLeft) {
                    paddingLeft = childAt.getRight();
                }
                if (childAt.getLeft() > i3 && childAt.getLeft() < paddingRight) {
                    paddingRight = childAt.getLeft();
                }
            }
        }
        return new Pair<>(Integer.valueOf(paddingLeft), Integer.valueOf(paddingRight));
    }

    private void R(Context context) {
        Drawable background = getBackground();
        if (background == null || (background instanceof ColorDrawable)) {
            g gVar = new g();
            gVar.X(ColorStateList.valueOf(background != null ? ((ColorDrawable) background).getColor() : 0));
            gVar.N(context);
            gVar.W(y.y(this));
            y.v0(this, gVar);
        }
    }

    private void S(View view, Pair<Integer, Integer> pair) {
        int measuredWidth = getMeasuredWidth();
        int measuredWidth2 = view.getMeasuredWidth();
        int i3 = (measuredWidth / 2) - (measuredWidth2 / 2);
        int i4 = measuredWidth2 + i3;
        int iMax = Math.max(Math.max(((Integer) pair.first).intValue() - i3, 0), Math.max(i4 - ((Integer) pair.second).intValue(), 0));
        if (iMax > 0) {
            i3 += iMax;
            i4 -= iMax;
            view.measure(View.MeasureSpec.makeMeasureSpec(i4 - i3, 1073741824), view.getMeasuredHeightAndState());
        }
        view.layout(i3, view.getTop(), i4, view.getBottom());
    }

    private void T() {
        if (this.W || this.f3548a0) {
            TextView textViewE = n.e(this);
            TextView textViewC = n.c(this);
            if (textViewE == null && textViewC == null) {
                return;
            }
            Pair<Integer, Integer> pairQ = Q(textViewE, textViewC);
            if (this.W && textViewE != null) {
                S(textViewE, pairQ);
            }
            if (!this.f3548a0 || textViewC == null) {
                return;
            }
            S(textViewC, pairQ);
        }
    }

    private Drawable U(Drawable drawable) {
        if (drawable == null || this.V == null) {
            return drawable;
        }
        Drawable drawableR = androidx.core.graphics.drawable.a.r(drawable.mutate());
        androidx.core.graphics.drawable.a.n(drawableR, this.V.intValue());
        return drawableR;
    }

    private void V() {
        ImageView imageViewB = n.b(this);
        if (imageViewB != null) {
            Boolean bool = this.f3550c0;
            if (bool != null) {
                imageViewB.setAdjustViewBounds(bool.booleanValue());
            }
            ImageView.ScaleType scaleType = this.f3549b0;
            if (scaleType != null) {
                imageViewB.setScaleType(scaleType);
            }
        }
    }

    public ImageView.ScaleType getLogoScaleType() {
        return this.f3549b0;
    }

    public Integer getNavigationIconTint() {
        return this.V;
    }

    @Override // android.view.ViewGroup, android.view.View
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        h.e(this);
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.ViewGroup, android.view.View
    protected void onLayout(boolean z2, int i3, int i4, int i5, int i6) {
        super.onLayout(z2, i3, i4, i5, i6);
        T();
        V();
    }

    @Override // android.view.View
    public void setElevation(float f3) {
        super.setElevation(f3);
        h.d(this, f3);
    }

    public void setLogoAdjustViewBounds(boolean z2) {
        Boolean bool = this.f3550c0;
        if (bool == null || bool.booleanValue() != z2) {
            this.f3550c0 = Boolean.valueOf(z2);
            requestLayout();
        }
    }

    public void setLogoScaleType(ImageView.ScaleType scaleType) {
        if (this.f3549b0 != scaleType) {
            this.f3549b0 = scaleType;
            requestLayout();
        }
    }

    @Override // androidx.appcompat.widget.Toolbar
    public void setNavigationIcon(Drawable drawable) {
        super.setNavigationIcon(U(drawable));
    }

    public void setNavigationIconTint(int i3) {
        this.V = Integer.valueOf(i3);
        Drawable navigationIcon = getNavigationIcon();
        if (navigationIcon != null) {
            setNavigationIcon(navigationIcon);
        }
    }

    public void setSubtitleCentered(boolean z2) {
        if (this.f3548a0 != z2) {
            this.f3548a0 = z2;
            requestLayout();
        }
    }

    public void setTitleCentered(boolean z2) {
        if (this.W != z2) {
            this.W = z2;
            requestLayout();
        }
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public MaterialToolbar(Context context, AttributeSet attributeSet, int i3) {
        int i4 = f3546d0;
        super(e1.a.c(context, attributeSet, i3, i4), attributeSet, i3);
        Context context2 = getContext();
        TypedArray typedArrayH = m.h(context2, attributeSet, k.f5483f2, i3, i4, new int[0]);
        int i5 = k.f5495i2;
        if (typedArrayH.hasValue(i5)) {
            setNavigationIconTint(typedArrayH.getColor(i5, -1));
        }
        this.W = typedArrayH.getBoolean(k.f5503k2, false);
        this.f3548a0 = typedArrayH.getBoolean(k.f5499j2, false);
        int i6 = typedArrayH.getInt(k.f5491h2, -1);
        if (i6 >= 0) {
            ImageView.ScaleType[] scaleTypeArr = f3547e0;
            if (i6 < scaleTypeArr.length) {
                this.f3549b0 = scaleTypeArr[i6];
            }
        }
        int i7 = k.f5487g2;
        if (typedArrayH.hasValue(i7)) {
            this.f3550c0 = Boolean.valueOf(typedArrayH.getBoolean(i7, false));
        }
        typedArrayH.recycle();
        R(context2);
    }
}
