package com.google.android.material.search;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DimenRes;
import androidx.annotation.Dimension;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.ActionMenuView;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.internal.q0;
import com.google.android.material.internal.r0;
import com.google.android.material.shape.k;
import com.google.android.material.shape.s;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import s9.f;
import tv.danmaku.ijk.media.player.IjkMediaMeta;
import x8.g;
import x8.i;
import x8.l;
import x8.m;

/* JADX INFO: loaded from: classes2.dex */
public class SearchBar extends Toolbar {
    public static final int R0 = l.Widget_Material3_SearchBar;
    public final TextView A0;
    public final TextView B0;
    public final FrameLayout C0;
    public final int D0;
    public boolean E0;
    public final boolean F0;
    public final boolean G0;
    public final f H0;
    public final Drawable I0;
    public final boolean J0;
    public final boolean K0;
    public int L0;
    public boolean M0;
    public final k N0;
    public boolean O0;
    public int P0;
    public final s9.a Q0;

    @Nullable
    private View centerView;

    @Nullable
    private final ColorStateList liftOnScrollColor;

    @Nullable
    private ActionMenuView menuView;

    @Nullable
    private ImageButton navIconButton;

    @Nullable
    private Integer navigationIconTint;

    @Nullable
    private Drawable originalNavigationIconBackground;

    public static class ScrollingViewBehavior extends AppBarLayout.ScrollingViewBehavior {

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

        public ScrollingViewBehavior() {
            this.f22991g = false;
        }

        @Override // com.google.android.material.appbar.AppBarLayout.ScrollingViewBehavior, d0.b
        public final boolean h(CoordinatorLayout coordinatorLayout, View view, View view2) {
            super.h(coordinatorLayout, view, view2);
            if (!this.f22991g && (view2 instanceof AppBarLayout)) {
                this.f22991g = true;
                AppBarLayout appBarLayout = (AppBarLayout) view2;
                appBarLayout.setBackgroundColor(0);
                appBarLayout.setTargetElevation(0.0f);
            }
            return false;
        }

        public ScrollingViewBehavior(@NonNull Context context, @Nullable AttributeSet attributeSet) {
            super(context, attributeSet);
            this.f22991g = false;
        }
    }

    public SearchBar(@NonNull Context context) {
        this(context, null);
    }

    @Nullable
    private ActionMenuView findOrGetMenuView() {
        if (this.menuView == null) {
            this.menuView = r0.getActionMenuView(this);
        }
        return this.menuView;
    }

    @Nullable
    private ImageButton findOrGetNavView() {
        if (this.navIconButton == null) {
            this.navIconButton = r0.getNavigationIconButton(this);
        }
        return this.navIconButton;
    }

    @Nullable
    private AppBarLayout getAppBarLayoutParentIfExists() {
        for (ViewParent parent = getParent(); parent != null; parent = parent.getParent()) {
            if (parent instanceof AppBarLayout) {
                return (AppBarLayout) parent;
            }
        }
        return null;
    }

    @Nullable
    private Drawable maybeTintNavigationIcon(@Nullable Drawable drawable) {
        int iB;
        if (!this.J0 || drawable == null) {
            return drawable;
        }
        Integer num = this.navigationIconTint;
        if (num != null) {
            iB = num.intValue();
        } else {
            iB = h9.a.b(drawable == this.I0 ? x8.c.colorOnSurfaceVariant : x8.c.colorOnSurface, this);
        }
        Drawable drawableMutate = drawable.mutate();
        drawableMutate.setTint(iB);
        return drawableMutate;
    }

    private void setNavigationIconDecorative(boolean z10) {
        ImageButton navigationIconButton = r0.getNavigationIconButton(this);
        if (navigationIconButton == null) {
            return;
        }
        navigationIconButton.setClickable(!z10);
        navigationIconButton.setFocusable(!z10);
        Drawable background = navigationIconButton.getBackground();
        if (background != null) {
            this.originalNavigationIconBackground = background;
        }
        navigationIconButton.setBackgroundDrawable(z10 ? null : this.originalNavigationIconBackground);
        z();
    }

    private void validateAttributes(@Nullable AttributeSet attributeSet) {
        if (attributeSet == null) {
            return;
        }
        if (attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", IjkMediaMeta.IJKM_KEY_TITLE) != null) {
            throw new UnsupportedOperationException("SearchBar does not support title. Use hint or text instead.");
        }
        if (attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "subtitle") != null) {
            throw new UnsupportedOperationException("SearchBar does not support subtitle. Use hint or text instead.");
        }
    }

    public final void A() {
        if (getLayoutParams() instanceof com.google.android.material.appbar.k) {
            com.google.android.material.appbar.k kVar = (com.google.android.material.appbar.k) getLayoutParams();
            if (this.M0) {
                if (kVar.f22201a == 0) {
                    kVar.f22201a = 53;
                }
            } else if (kVar.f22201a == 53) {
                kVar.f22201a = 0;
            }
        }
    }

    @Override // android.view.ViewGroup
    public final void addView(View view, int i10, ViewGroup.LayoutParams layoutParams) {
        if (this.F0 && this.centerView == null && !(view instanceof ActionMenuView)) {
            this.centerView = view;
            view.setAlpha(0.0f);
        }
        super.addView(view, i10, layoutParams);
    }

    @CanIgnoreReturnValue
    public boolean collapse(@NonNull View view, @Nullable AppBarLayout appBarLayout) {
        return collapse(view, appBarLayout, false);
    }

    @CanIgnoreReturnValue
    public boolean expand(@NonNull View view, @Nullable AppBarLayout appBarLayout) {
        return expand(view, appBarLayout, false);
    }

    @Nullable
    public View getCenterView() {
        return this.centerView;
    }

    public float getCompatElevation() {
        k kVar = this.N0;
        return kVar != null ? kVar.f23095b.f23074g : getElevation();
    }

    public float getCornerSize() {
        return this.N0.p();
    }

    @DimenRes
    @RestrictTo({RestrictTo.Scope.f556b})
    public int getDefaultMarginVerticalResource() {
        return x8.e.m3_searchbar_margin_vertical;
    }

    @DrawableRes
    @RestrictTo({RestrictTo.Scope.f556b})
    public int getDefaultNavigationIconResource() {
        return x8.f.ic_search_black_24;
    }

    @Nullable
    public CharSequence getHint() {
        return this.A0.getHint();
    }

    @Px
    public int getMaxWidth() {
        return this.P0;
    }

    public int getMenuResId() {
        return this.L0;
    }

    public TextView getPlaceholderTextView() {
        return this.B0;
    }

    @ColorInt
    public int getStrokeColor() {
        return this.N0.getStrokeColor().getDefaultColor();
    }

    @Dimension
    public float getStrokeWidth() {
        return this.N0.f23095b.f23071d;
    }

    @NonNull
    public CharSequence getText() {
        return this.A0.getText();
    }

    public boolean getTextCentered() {
        return this.O0;
    }

    @NonNull
    public TextView getTextView() {
        return this.A0;
    }

    @Override // androidx.appcompat.widget.Toolbar
    public final void m(int i10) {
        super.m(i10);
        this.L0 = i10;
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.ViewGroup, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        com.bumptech.glide.f.v(this, this.N0);
        if (this.G0 && (getLayoutParams() instanceof ViewGroup.MarginLayoutParams)) {
            Resources resources = getResources();
            int dimensionPixelSize = resources.getDimensionPixelSize(x8.e.m3_searchbar_margin_horizontal);
            int dimensionPixelSize2 = resources.getDimensionPixelSize(getDefaultMarginVerticalResource());
            ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
            int i10 = marginLayoutParams.leftMargin;
            if (i10 == 0) {
                i10 = dimensionPixelSize;
            }
            marginLayoutParams.leftMargin = i10;
            int i11 = marginLayoutParams.topMargin;
            if (i11 == 0) {
                i11 = dimensionPixelSize2;
            }
            marginLayoutParams.topMargin = i11;
            int i12 = marginLayoutParams.rightMargin;
            if (i12 != 0) {
                dimensionPixelSize = i12;
            }
            marginLayoutParams.rightMargin = dimensionPixelSize;
            int i13 = marginLayoutParams.bottomMargin;
            if (i13 != 0) {
                dimensionPixelSize2 = i13;
            }
            marginLayoutParams.bottomMargin = dimensionPixelSize2;
        }
        A();
        if (this.E0) {
            y();
        }
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.ViewGroup, android.view.View
    public final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        AppBarLayout appBarLayoutParentIfExists = getAppBarLayoutParentIfExists();
        if (appBarLayoutParentIfExists != null) {
            appBarLayoutParentIfExists.Q.remove(this.Q0);
        }
    }

    @Override // android.view.View
    public final void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo accessibilityNodeInfo) {
        super.onInitializeAccessibilityNodeInfo(accessibilityNodeInfo);
        accessibilityNodeInfo.setClassName(EditText.class.getCanonicalName());
        accessibilityNodeInfo.setEditable(isEnabled());
        CharSequence text = getText();
        boolean zIsEmpty = TextUtils.isEmpty(text);
        if (Build.VERSION.SDK_INT >= 26) {
            accessibilityNodeInfo.setHintText(getHint());
            accessibilityNodeInfo.setShowingHintText(zIsEmpty);
        }
        if (zIsEmpty) {
            text = getHint();
        }
        accessibilityNodeInfo.setText(text);
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.ViewGroup, android.view.View
    public final void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        super.onLayout(z10, i10, i11, i12, i13);
        View view = this.centerView;
        if (view != null && view != null) {
            int measuredWidth = view.getMeasuredWidth();
            int measuredWidth2 = (getMeasuredWidth() / 2) - (measuredWidth / 2);
            int i14 = measuredWidth + measuredWidth2;
            int measuredHeight = view.getMeasuredHeight();
            int measuredHeight2 = (getMeasuredHeight() / 2) - (measuredHeight / 2);
            int i15 = measuredHeight + measuredHeight2;
            if (getLayoutDirection() == 1) {
                view.layout(getMeasuredWidth() - i14, measuredHeight2, getMeasuredWidth() - measuredWidth2, i15);
            } else {
                view.layout(measuredWidth2, measuredHeight2, i14, i15);
            }
        }
        z();
        TextView textView = this.A0;
        if (textView == null || !this.O0) {
            return;
        }
        int measuredWidth3 = getMeasuredWidth() / 2;
        FrameLayout frameLayout = this.C0;
        int measuredWidth4 = measuredWidth3 - (frameLayout.getMeasuredWidth() / 2);
        int measuredWidth5 = frameLayout.getMeasuredWidth() + measuredWidth4;
        int measuredHeight3 = (getMeasuredHeight() / 2) - (frameLayout.getMeasuredHeight() / 2);
        int measuredHeight4 = frameLayout.getMeasuredHeight() + measuredHeight3;
        boolean z11 = getLayoutDirection() == 1;
        View viewFindOrGetMenuView = findOrGetMenuView();
        ImageButton imageButtonFindOrGetNavView = findOrGetNavView();
        int measuredWidth6 = (frameLayout.getMeasuredWidth() / 2) - (textView.getMeasuredWidth() / 2);
        int measuredWidth7 = textView.getMeasuredWidth() + measuredWidth6;
        int i16 = measuredWidth6 + measuredWidth4;
        int i17 = measuredWidth7 + measuredWidth4;
        View view2 = z11 ? viewFindOrGetMenuView : imageButtonFindOrGetNavView;
        if (z11) {
            viewFindOrGetMenuView = imageButtonFindOrGetNavView;
        }
        int iMax = view2 != null ? Math.max(view2.getRight() - i16, 0) : 0;
        int i18 = i16 + iMax;
        int i19 = i17 + iMax;
        int iMax2 = viewFindOrGetMenuView != null ? Math.max(i19 - viewFindOrGetMenuView.getLeft(), 0) : 0;
        int i20 = i18 - iMax2;
        int i21 = i19 - iMax2;
        int iMax3 = ((iMax - iMax2) + Math.max(Math.max(getPaddingLeft() - i20, getContentInsetLeft() - i20), 0)) - Math.max(Math.max(i21 - (getMeasuredWidth() - getPaddingRight()), i21 - (getMeasuredWidth() - getContentInsetRight())), 0);
        frameLayout.layout(measuredWidth4 + iMax3, measuredHeight3, measuredWidth5 + iMax3, measuredHeight4);
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.View
    public final void onMeasure(int i10, int i11) {
        int i12 = this.P0;
        if (i12 >= 0 && i12 < View.MeasureSpec.getSize(i10)) {
            i10 = View.MeasureSpec.makeMeasureSpec(this.P0, View.MeasureSpec.getMode(i10));
        }
        super.onMeasure(i10, i11);
        View view = this.centerView;
        if (view != null) {
            view.measure(i10, i11);
        }
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.View
    public final void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof s9.b)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        s9.b bVar = (s9.b) parcelable;
        super.onRestoreInstanceState(bVar.getSuperState());
        setText(bVar.f32674c);
    }

    @Override // androidx.appcompat.widget.Toolbar, android.view.View
    public final Parcelable onSaveInstanceState() {
        s9.b bVar = new s9.b(super.onSaveInstanceState());
        CharSequence text = getText();
        bVar.f32674c = text == null ? null : text.toString();
        return bVar;
    }

    public void setCenterView(@Nullable View view) {
        View view2 = this.centerView;
        if (view2 != null) {
            removeView(view2);
            this.centerView = null;
        }
        if (view != null) {
            addView(view);
        }
    }

    public void setDefaultScrollFlagsEnabled(boolean z10) {
        this.M0 = z10;
        A();
    }

    @Override // android.view.View
    public void setElevation(float f10) {
        super.setElevation(f10);
        k kVar = this.N0;
        if (kVar != null) {
            kVar.w(f10);
        }
    }

    public void setHint(@Nullable CharSequence charSequence) {
        this.A0.setHint(charSequence);
    }

    public void setLiftOnScroll(boolean z10) {
        this.E0 = z10;
        if (z10) {
            y();
            return;
        }
        AppBarLayout appBarLayoutParentIfExists = getAppBarLayoutParentIfExists();
        if (appBarLayoutParentIfExists != null) {
            appBarLayoutParentIfExists.Q.remove(this.Q0);
        }
    }

    public void setMaxWidth(@Px int i10) {
        if (this.P0 != i10) {
            this.P0 = i10;
            requestLayout();
        }
    }

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

    @Override // androidx.appcompat.widget.Toolbar
    public void setNavigationOnClickListener(View.OnClickListener onClickListener) {
        if (this.K0) {
            return;
        }
        super.setNavigationOnClickListener(onClickListener);
        setNavigationIconDecorative(onClickListener == null);
    }

    public void setOnLoadAnimationFadeInEnabled(boolean z10) {
        this.H0.f32690e = z10;
    }

    public void setPlaceholderText(String str) {
        this.B0.setText(str);
    }

    public void setStrokeColor(@ColorInt int i10) {
        if (getStrokeColor() != i10) {
            this.N0.setStrokeColor(ColorStateList.valueOf(i10));
        }
    }

    public void setStrokeWidth(@Dimension float f10) {
        if (getStrokeWidth() != f10) {
            this.N0.D(f10);
        }
    }

    @Override // androidx.appcompat.widget.Toolbar
    public void setSubtitle(CharSequence charSequence) {
    }

    public void setText(@Nullable CharSequence charSequence) {
        this.A0.setText(charSequence);
        this.B0.setText(charSequence);
    }

    public void setTextCentered(boolean z10) {
        this.O0 = z10;
        TextView textView = this.A0;
        if (textView == null) {
            return;
        }
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) textView.getLayoutParams();
        if (z10) {
            layoutParams.gravity = 1;
            textView.setGravity(1);
        } else {
            layoutParams.gravity = 0;
            textView.setGravity(0);
        }
        textView.setLayoutParams(layoutParams);
        this.B0.setLayoutParams(layoutParams);
    }

    @Override // androidx.appcompat.widget.Toolbar
    public void setTitle(CharSequence charSequence) {
    }

    public final void y() {
        AppBarLayout appBarLayoutParentIfExists = getAppBarLayoutParentIfExists();
        if (appBarLayoutParentIfExists == null || this.liftOnScrollColor == null) {
            return;
        }
        appBarLayoutParentIfExists.Q.add(this.Q0);
    }

    public final void z() {
        if (Build.VERSION.SDK_INT < 34) {
            return;
        }
        boolean z10 = getLayoutDirection() == 1;
        ImageButton navigationIconButton = r0.getNavigationIconButton(this);
        int width = (navigationIconButton == null || !navigationIconButton.isClickable()) ? 0 : z10 ? getWidth() - navigationIconButton.getLeft() : navigationIconButton.getRight();
        ActionMenuView actionMenuView = r0.getActionMenuView(this);
        int right = actionMenuView != null ? z10 ? actionMenuView.getRight() : getWidth() - actionMenuView.getLeft() : 0;
        float f10 = -(z10 ? right : width);
        if (!z10) {
            width = right;
        }
        setHandwritingBoundsOffsets(f10, 0.0f, -width, 0.0f);
    }

    public SearchBar(@NonNull Context context, @Nullable AttributeSet attributeSet) {
        this(context, attributeSet, x8.c.materialSearchBarStyle);
    }

    @CanIgnoreReturnValue
    public boolean collapse(@NonNull View view, @Nullable AppBarLayout appBarLayout, boolean z10) {
        int visibility = view.getVisibility();
        f fVar = this.H0;
        if ((visibility != 0 || fVar.f32689d) && !fVar.f32688c) {
            return false;
        }
        fVar.startCollapseAnimation(this, view, appBarLayout, z10);
        return true;
    }

    @CanIgnoreReturnValue
    public boolean expand(@NonNull View view, @Nullable AppBarLayout appBarLayout, boolean z10) {
        int visibility = view.getVisibility();
        f fVar = this.H0;
        if ((visibility == 0 || fVar.f32688c) && !fVar.f32689d) {
            return false;
        }
        fVar.startExpandAnimation(this, view, appBarLayout, z10);
        return true;
    }

    public void setHint(@StringRes int i10) {
        this.A0.setHint(i10);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public SearchBar(@NonNull Context context, @Nullable AttributeSet attributeSet, int i10) {
        boolean z10;
        int i11 = R0;
        super(x9.a.wrap(context, attributeSet, i10, i11), attributeSet, i10);
        this.L0 = -1;
        this.Q0 = new s9.a(this);
        Context context2 = getContext();
        validateAttributes(attributeSet);
        Drawable drawable = f.a.getDrawable(context2, getDefaultNavigationIconResource());
        this.I0 = drawable;
        this.H0 = new f();
        TypedArray typedArrayC = q0.c(context2, attributeSet, m.SearchBar, i10, i11, new int[0]);
        s sVarA = s.c(context2, attributeSet, i10, i11).a();
        int color = typedArrayC.getColor(m.SearchBar_backgroundTint, 0);
        this.D0 = color;
        this.liftOnScrollColor = q9.d.getColorStateList(context2, typedArrayC, m.SearchBar_liftOnScrollColor);
        float dimension = typedArrayC.getDimension(m.SearchBar_elevation, 0.0f);
        this.G0 = typedArrayC.getBoolean(m.SearchBar_defaultMarginsEnabled, true);
        this.M0 = typedArrayC.getBoolean(m.SearchBar_defaultScrollFlagsEnabled, true);
        boolean z11 = typedArrayC.getBoolean(m.SearchBar_hideNavigationIcon, false);
        this.K0 = typedArrayC.getBoolean(m.SearchBar_forceDefaultNavigationOnClickListener, false);
        this.J0 = typedArrayC.getBoolean(m.SearchBar_tintNavigationIcon, true);
        int i12 = m.SearchBar_navigationIconTint;
        if (typedArrayC.hasValue(i12)) {
            this.navigationIconTint = Integer.valueOf(typedArrayC.getColor(i12, -1));
        }
        int resourceId = typedArrayC.getResourceId(m.SearchBar_android_textAppearance, -1);
        String string = typedArrayC.getString(m.SearchBar_android_text);
        String string2 = typedArrayC.getString(m.SearchBar_android_hint);
        float dimension2 = typedArrayC.getDimension(m.SearchBar_strokeWidth, -1.0f);
        int color2 = typedArrayC.getColor(m.SearchBar_strokeColor, 0);
        this.O0 = typedArrayC.getBoolean(m.SearchBar_textCentered, false);
        this.E0 = typedArrayC.getBoolean(m.SearchBar_liftOnScroll, false);
        this.P0 = typedArrayC.getDimensionPixelSize(m.SearchBar_android_maxWidth, -1);
        typedArrayC.recycle();
        if (z11) {
            z10 = true;
        } else {
            setNavigationIcon(getNavigationIcon() != null ? getNavigationIcon() : drawable);
            z10 = true;
            setNavigationIconDecorative(true);
        }
        setClickable(z10);
        setFocusable(z10);
        LayoutInflater.from(context2).inflate(i.mtrl_search_bar, this);
        this.F0 = z10;
        TextView textView = (TextView) findViewById(g.open_search_bar_text_view);
        this.A0 = textView;
        TextView textView2 = (TextView) findViewById(g.open_search_bar_placeholder_text_view);
        this.B0 = textView2;
        this.C0 = (FrameLayout) findViewById(g.open_search_bar_text_view_container);
        setElevation(dimension);
        if (resourceId != -1) {
            textView.setTextAppearance(resourceId);
            textView2.setTextAppearance(resourceId);
        }
        setText(string);
        setHint(string2);
        setTextCentered(this.O0);
        k kVar = new k(sVarA);
        this.N0 = kVar;
        kVar.s(getContext());
        this.N0.w(dimension);
        if (dimension2 >= 0.0f) {
            k kVar2 = this.N0;
            kVar2.D(dimension2);
            kVar2.setStrokeColor(ColorStateList.valueOf(color2));
        }
        int iB = h9.a.b(d.a.colorControlHighlight, this);
        this.N0.setFillColor(ColorStateList.valueOf(color));
        ColorStateList colorStateListValueOf = ColorStateList.valueOf(iB);
        k kVar3 = this.N0;
        setBackground(new RippleDrawable(colorStateListValueOf, kVar3, kVar3));
    }

    public void setText(@StringRes int i10) {
        this.A0.setText(i10);
        this.B0.setText(i10);
    }
}
