package androidx.appcompat.widget;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.app.SearchableInfo;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.org.conscrypt.PSKKeyManager;
import java.lang.reflect.Method;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public class SearchView extends m0 implements androidx.appcompat.view.c {

    /* JADX INFO: renamed from: u0, reason: collision with root package name */
    static final o f675u0;
    final ImageView A;
    private final View B;
    private q C;
    private Rect D;
    private Rect E;
    private int[] F;
    private int[] G;
    private final ImageView H;
    private final Drawable I;
    private final int J;
    private final int K;
    private final Intent L;
    private final Intent M;
    private final CharSequence N;
    private m O;
    private l P;
    View.OnFocusChangeListener Q;
    private n R;
    private View.OnClickListener S;
    private boolean T;
    private boolean U;
    v.a V;
    private boolean W;

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

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

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

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    private int f679d0;

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

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    private CharSequence f681f0;

    /* JADX INFO: renamed from: g0, reason: collision with root package name */
    private CharSequence f682g0;

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

    /* JADX INFO: renamed from: i0, reason: collision with root package name */
    private int f684i0;

    /* JADX INFO: renamed from: j0, reason: collision with root package name */
    SearchableInfo f685j0;

    /* JADX INFO: renamed from: k0, reason: collision with root package name */
    private Bundle f686k0;

    /* JADX INFO: renamed from: l0, reason: collision with root package name */
    private final Runnable f687l0;

    /* JADX INFO: renamed from: m0, reason: collision with root package name */
    private Runnable f688m0;

    /* JADX INFO: renamed from: n0, reason: collision with root package name */
    private final WeakHashMap<String, Drawable.ConstantState> f689n0;

    /* JADX INFO: renamed from: o0, reason: collision with root package name */
    private final View.OnClickListener f690o0;

    /* JADX INFO: renamed from: p0, reason: collision with root package name */
    View.OnKeyListener f691p0;

    /* JADX INFO: renamed from: q0, reason: collision with root package name */
    private final TextView.OnEditorActionListener f692q0;

    /* JADX INFO: renamed from: r0, reason: collision with root package name */
    private final AdapterView.OnItemClickListener f693r0;

    /* JADX INFO: renamed from: s0, reason: collision with root package name */
    private final AdapterView.OnItemSelectedListener f694s0;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    final SearchAutoComplete f695t;

    /* JADX INFO: renamed from: t0, reason: collision with root package name */
    private TextWatcher f696t0;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    private final View f697u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    private final View f698v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    private final View f699w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    final ImageView f700x;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    final ImageView f701y;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    final ImageView f702z;

    public static class SearchAutoComplete extends androidx.appcompat.widget.d {

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        private int f703i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        private SearchView f704j;

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

        /* JADX INFO: renamed from: l, reason: collision with root package name */
        final Runnable f706l;

        class a implements Runnable {
            a() {
            }

            @Override // java.lang.Runnable
            public void run() {
                SearchAutoComplete.this.d();
            }
        }

        public SearchAutoComplete(Context context, AttributeSet attributeSet) {
            this(context, attributeSet, c.a.f3165p);
        }

        private int getSearchViewTextMinWidthDp() {
            Configuration configuration = getResources().getConfiguration();
            int i3 = configuration.screenWidthDp;
            int i4 = configuration.screenHeightDp;
            if (i3 >= 960 && i4 >= 720 && configuration.orientation == 2) {
                return PSKKeyManager.MAX_KEY_LENGTH_BYTES;
            }
            if (i3 < 600) {
                return (i3 < 640 || i4 < 480) ? 160 : 192;
            }
            return 192;
        }

        void b() {
            if (Build.VERSION.SDK_INT < 29) {
                SearchView.f675u0.c(this);
                return;
            }
            k.b(this, 1);
            if (enoughToFilter()) {
                showDropDown();
            }
        }

        boolean c() {
            return TextUtils.getTrimmedLength(getText()) == 0;
        }

        void d() {
            if (this.f705k) {
                ((InputMethodManager) getContext().getSystemService("input_method")).showSoftInput(this, 0);
                this.f705k = false;
            }
        }

        @Override // android.widget.AutoCompleteTextView
        public boolean enoughToFilter() {
            return this.f703i <= 0 || super.enoughToFilter();
        }

        @Override // androidx.appcompat.widget.d, android.widget.TextView, android.view.View
        public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
            InputConnection inputConnectionOnCreateInputConnection = super.onCreateInputConnection(editorInfo);
            if (this.f705k) {
                removeCallbacks(this.f706l);
                post(this.f706l);
            }
            return inputConnectionOnCreateInputConnection;
        }

        @Override // android.view.View
        protected void onFinishInflate() {
            super.onFinishInflate();
            setMinWidth((int) TypedValue.applyDimension(1, getSearchViewTextMinWidthDp(), getResources().getDisplayMetrics()));
        }

        @Override // android.widget.AutoCompleteTextView, android.widget.TextView, android.view.View
        protected void onFocusChanged(boolean z2, int i3, Rect rect) {
            super.onFocusChanged(z2, i3, rect);
            this.f704j.Z();
        }

        @Override // android.widget.AutoCompleteTextView, android.widget.TextView, android.view.View
        public boolean onKeyPreIme(int i3, KeyEvent keyEvent) {
            if (i3 == 4) {
                if (keyEvent.getAction() == 0 && keyEvent.getRepeatCount() == 0) {
                    KeyEvent.DispatcherState keyDispatcherState = getKeyDispatcherState();
                    if (keyDispatcherState != null) {
                        keyDispatcherState.startTracking(keyEvent, this);
                    }
                    return true;
                }
                if (keyEvent.getAction() == 1) {
                    KeyEvent.DispatcherState keyDispatcherState2 = getKeyDispatcherState();
                    if (keyDispatcherState2 != null) {
                        keyDispatcherState2.handleUpEvent(keyEvent);
                    }
                    if (keyEvent.isTracking() && !keyEvent.isCanceled()) {
                        this.f704j.clearFocus();
                        setImeVisibility(false);
                        return true;
                    }
                }
            }
            return super.onKeyPreIme(i3, keyEvent);
        }

        @Override // android.widget.AutoCompleteTextView, android.widget.TextView, android.view.View
        public void onWindowFocusChanged(boolean z2) {
            super.onWindowFocusChanged(z2);
            if (z2 && this.f704j.hasFocus() && getVisibility() == 0) {
                this.f705k = true;
                if (SearchView.M(getContext())) {
                    b();
                }
            }
        }

        @Override // android.widget.AutoCompleteTextView
        public void performCompletion() {
        }

        @Override // android.widget.AutoCompleteTextView
        protected void replaceText(CharSequence charSequence) {
        }

        void setImeVisibility(boolean z2) {
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService("input_method");
            if (!z2) {
                this.f705k = false;
                removeCallbacks(this.f706l);
                inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
            } else {
                if (!inputMethodManager.isActive(this)) {
                    this.f705k = true;
                    return;
                }
                this.f705k = false;
                removeCallbacks(this.f706l);
                inputMethodManager.showSoftInput(this, 0);
            }
        }

        void setSearchView(SearchView searchView) {
            this.f704j = searchView;
        }

        @Override // android.widget.AutoCompleteTextView
        public void setThreshold(int i3) {
            super.setThreshold(i3);
            this.f703i = i3;
        }

        public SearchAutoComplete(Context context, AttributeSet attributeSet, int i3) {
            super(context, attributeSet, i3);
            this.f706l = new a();
            this.f703i = getThreshold();
        }
    }

    class a implements TextWatcher {
        a() {
        }

        @Override // android.text.TextWatcher
        public void afterTextChanged(Editable editable) {
        }

        @Override // android.text.TextWatcher
        public void beforeTextChanged(CharSequence charSequence, int i3, int i4, int i5) {
        }

        @Override // android.text.TextWatcher
        public void onTextChanged(CharSequence charSequence, int i3, int i4, int i5) {
            SearchView.this.Y(charSequence);
        }
    }

    class b implements Runnable {
        b() {
        }

        @Override // java.lang.Runnable
        public void run() {
            SearchView.this.f0();
        }
    }

    class c implements Runnable {
        c() {
        }

        @Override // java.lang.Runnable
        public void run() {
            v.a aVar = SearchView.this.V;
            if (aVar instanceof u0) {
                aVar.changeCursor(null);
            }
        }
    }

    class d implements View.OnFocusChangeListener {
        d() {
        }

        @Override // android.view.View.OnFocusChangeListener
        public void onFocusChange(View view, boolean z2) {
            SearchView searchView = SearchView.this;
            View.OnFocusChangeListener onFocusChangeListener = searchView.Q;
            if (onFocusChangeListener != null) {
                onFocusChangeListener.onFocusChange(searchView, z2);
            }
        }
    }

    class e implements View.OnLayoutChangeListener {
        e() {
        }

        @Override // android.view.View.OnLayoutChangeListener
        public void onLayoutChange(View view, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10) {
            SearchView.this.B();
        }
    }

    class f implements View.OnClickListener {
        f() {
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View view) {
            SearchView searchView = SearchView.this;
            if (view == searchView.f700x) {
                searchView.V();
                return;
            }
            if (view == searchView.f702z) {
                searchView.R();
                return;
            }
            if (view == searchView.f701y) {
                searchView.W();
            } else if (view == searchView.A) {
                searchView.a0();
            } else if (view == searchView.f695t) {
                searchView.H();
            }
        }
    }

    class g implements View.OnKeyListener {
        g() {
        }

        @Override // android.view.View.OnKeyListener
        public boolean onKey(View view, int i3, KeyEvent keyEvent) {
            SearchView searchView = SearchView.this;
            if (searchView.f685j0 == null) {
                return false;
            }
            if (searchView.f695t.isPopupShowing() && SearchView.this.f695t.getListSelection() != -1) {
                return SearchView.this.X(view, i3, keyEvent);
            }
            if (SearchView.this.f695t.c() || !keyEvent.hasNoModifiers() || keyEvent.getAction() != 1 || i3 != 66) {
                return false;
            }
            view.cancelLongPress();
            SearchView searchView2 = SearchView.this;
            searchView2.P(0, null, searchView2.f695t.getText().toString());
            return true;
        }
    }

    class h implements TextView.OnEditorActionListener {
        h() {
        }

        @Override // android.widget.TextView.OnEditorActionListener
        public boolean onEditorAction(TextView textView, int i3, KeyEvent keyEvent) {
            SearchView.this.W();
            return true;
        }
    }

    class i implements AdapterView.OnItemClickListener {
        i() {
        }

        @Override // android.widget.AdapterView.OnItemClickListener
        public void onItemClick(AdapterView<?> adapterView, View view, int i3, long j3) {
            SearchView.this.S(i3, 0, null);
        }
    }

    class j implements AdapterView.OnItemSelectedListener {
        j() {
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView<?> adapterView, View view, int i3, long j3) {
            SearchView.this.T(i3);
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    static class k {
        static void a(AutoCompleteTextView autoCompleteTextView) {
            autoCompleteTextView.refreshAutoCompleteResults();
        }

        static void b(SearchAutoComplete searchAutoComplete, int i3) {
            searchAutoComplete.setInputMethodMode(i3);
        }
    }

    public interface l {
        boolean a();
    }

    public interface m {
        boolean a(String str);

        boolean b(String str);
    }

    public interface n {
        boolean a(int i3);

        boolean b(int i3);
    }

    private static class o {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private Method f718a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private Method f719b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private Method f720c;

        @SuppressLint({"DiscouragedPrivateApi", "SoonBlockedPrivateApi"})
        o() {
            this.f718a = null;
            this.f719b = null;
            this.f720c = null;
            d();
            try {
                Method declaredMethod = AutoCompleteTextView.class.getDeclaredMethod("doBeforeTextChanged", new Class[0]);
                this.f718a = declaredMethod;
                declaredMethod.setAccessible(true);
            } catch (NoSuchMethodException unused) {
            }
            try {
                Method declaredMethod2 = AutoCompleteTextView.class.getDeclaredMethod("doAfterTextChanged", new Class[0]);
                this.f719b = declaredMethod2;
                declaredMethod2.setAccessible(true);
            } catch (NoSuchMethodException unused2) {
            }
            try {
                Method method = AutoCompleteTextView.class.getMethod("ensureImeVisible", Boolean.TYPE);
                this.f720c = method;
                method.setAccessible(true);
            } catch (NoSuchMethodException unused3) {
            }
        }

        private static void d() {
            if (Build.VERSION.SDK_INT >= 29) {
                throw new UnsupportedClassVersionError("This function can only be used for API Level < 29.");
            }
        }

        void a(AutoCompleteTextView autoCompleteTextView) {
            d();
            Method method = this.f719b;
            if (method != null) {
                try {
                    method.invoke(autoCompleteTextView, new Object[0]);
                } catch (Exception unused) {
                }
            }
        }

        void b(AutoCompleteTextView autoCompleteTextView) {
            d();
            Method method = this.f718a;
            if (method != null) {
                try {
                    method.invoke(autoCompleteTextView, new Object[0]);
                } catch (Exception unused) {
                }
            }
        }

        void c(AutoCompleteTextView autoCompleteTextView) {
            d();
            Method method = this.f720c;
            if (method != null) {
                try {
                    method.invoke(autoCompleteTextView, Boolean.TRUE);
                } catch (Exception unused) {
                }
            }
        }
    }

    static class p extends w.a {
        public static final Parcelable.Creator<p> CREATOR = new a();

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

        class a implements Parcelable.ClassLoaderCreator<p> {
            a() {
            }

            @Override // android.os.Parcelable.Creator
            /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
            public p createFromParcel(Parcel parcel) {
                return new p(parcel, null);
            }

            @Override // android.os.Parcelable.ClassLoaderCreator
            /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
            public p createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return new p(parcel, classLoader);
            }

            @Override // android.os.Parcelable.Creator
            /* JADX INFO: renamed from: c, reason: merged with bridge method [inline-methods] */
            public p[] newArray(int i3) {
                return new p[i3];
            }
        }

        p(Parcelable parcelable) {
            super(parcelable);
        }

        public String toString() {
            return "SearchView.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " isIconified=" + this.f721g + "}";
        }

        @Override // w.a, android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i3) {
            super.writeToParcel(parcel, i3);
            parcel.writeValue(Boolean.valueOf(this.f721g));
        }

        public p(Parcel parcel, ClassLoader classLoader) {
            super(parcel, classLoader);
            this.f721g = ((Boolean) parcel.readValue(null)).booleanValue();
        }
    }

    private static class q extends TouchDelegate {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final View f722a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final Rect f723b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final Rect f724c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private final Rect f725d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private final int f726e;

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

        public q(Rect rect, Rect rect2, View view) {
            super(rect, view);
            this.f726e = ViewConfiguration.get(view.getContext()).getScaledTouchSlop();
            this.f723b = new Rect();
            this.f725d = new Rect();
            this.f724c = new Rect();
            a(rect, rect2);
            this.f722a = view;
        }

        public void a(Rect rect, Rect rect2) {
            this.f723b.set(rect);
            this.f725d.set(rect);
            Rect rect3 = this.f725d;
            int i3 = this.f726e;
            rect3.inset(-i3, -i3);
            this.f724c.set(rect2);
        }

        @Override // android.view.TouchDelegate
        public boolean onTouchEvent(MotionEvent motionEvent) {
            boolean z2;
            boolean z3;
            int x2 = (int) motionEvent.getX();
            int y2 = (int) motionEvent.getY();
            int action = motionEvent.getAction();
            boolean z4 = true;
            if (action != 0) {
                if (action == 1 || action == 2) {
                    z3 = this.f727f;
                    if (z3 && !this.f725d.contains(x2, y2)) {
                        z4 = z3;
                        z2 = false;
                    }
                } else {
                    if (action == 3) {
                        z3 = this.f727f;
                        this.f727f = false;
                    }
                    z2 = true;
                    z4 = false;
                }
                z4 = z3;
                z2 = true;
            } else if (this.f723b.contains(x2, y2)) {
                this.f727f = true;
                z2 = true;
            } else {
                z2 = true;
                z4 = false;
            }
            if (!z4) {
                return false;
            }
            if (!z2 || this.f724c.contains(x2, y2)) {
                Rect rect = this.f724c;
                motionEvent.setLocation(x2 - rect.left, y2 - rect.top);
            } else {
                motionEvent.setLocation(this.f722a.getWidth() / 2, this.f722a.getHeight() / 2);
            }
            return this.f722a.dispatchTouchEvent(motionEvent);
        }
    }

    static {
        f675u0 = Build.VERSION.SDK_INT < 29 ? new o() : null;
    }

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

    private Intent C(String str, Uri uri, String str2, String str3, int i3, String str4) {
        Intent intent = new Intent(str);
        intent.addFlags(268435456);
        if (uri != null) {
            intent.setData(uri);
        }
        intent.putExtra("user_query", this.f682g0);
        if (str3 != null) {
            intent.putExtra("query", str3);
        }
        if (str2 != null) {
            intent.putExtra("intent_extra_data_key", str2);
        }
        Bundle bundle = this.f686k0;
        if (bundle != null) {
            intent.putExtra("app_data", bundle);
        }
        if (i3 != 0) {
            intent.putExtra("action_key", i3);
            intent.putExtra("action_msg", str4);
        }
        intent.setComponent(this.f685j0.getSearchActivity());
        return intent;
    }

    private Intent D(Cursor cursor, int i3, String str) {
        int position;
        String strK;
        try {
            String strK2 = u0.k(cursor, "suggest_intent_action");
            if (strK2 == null) {
                strK2 = this.f685j0.getSuggestIntentAction();
            }
            if (strK2 == null) {
                strK2 = "android.intent.action.SEARCH";
            }
            String str2 = strK2;
            String strK3 = u0.k(cursor, "suggest_intent_data");
            if (strK3 == null) {
                strK3 = this.f685j0.getSuggestIntentData();
            }
            if (strK3 != null && (strK = u0.k(cursor, "suggest_intent_data_id")) != null) {
                strK3 = strK3 + "/" + Uri.encode(strK);
            }
            return C(str2, strK3 == null ? null : Uri.parse(strK3), u0.k(cursor, "suggest_intent_extra_data"), u0.k(cursor, "suggest_intent_query"), i3, str);
        } catch (RuntimeException e3) {
            try {
                position = cursor.getPosition();
            } catch (RuntimeException unused) {
                position = -1;
            }
            Log.w("SearchView", "Search suggestions cursor at row " + position + " returned exception.", e3);
            return null;
        }
    }

    private Intent E(Intent intent, SearchableInfo searchableInfo) {
        ComponentName searchActivity = searchableInfo.getSearchActivity();
        Intent intent2 = new Intent("android.intent.action.SEARCH");
        intent2.setComponent(searchActivity);
        PendingIntent activity = PendingIntent.getActivity(getContext(), 0, intent2, 1107296256);
        Bundle bundle = new Bundle();
        Bundle bundle2 = this.f686k0;
        if (bundle2 != null) {
            bundle.putParcelable("app_data", bundle2);
        }
        Intent intent3 = new Intent(intent);
        Resources resources = getResources();
        String string = searchableInfo.getVoiceLanguageModeId() != 0 ? resources.getString(searchableInfo.getVoiceLanguageModeId()) : "free_form";
        String string2 = searchableInfo.getVoicePromptTextId() != 0 ? resources.getString(searchableInfo.getVoicePromptTextId()) : null;
        String string3 = searchableInfo.getVoiceLanguageId() != 0 ? resources.getString(searchableInfo.getVoiceLanguageId()) : null;
        int voiceMaxResults = searchableInfo.getVoiceMaxResults() != 0 ? searchableInfo.getVoiceMaxResults() : 1;
        intent3.putExtra("android.speech.extra.LANGUAGE_MODEL", string);
        intent3.putExtra("android.speech.extra.PROMPT", string2);
        intent3.putExtra("android.speech.extra.LANGUAGE", string3);
        intent3.putExtra("android.speech.extra.MAX_RESULTS", voiceMaxResults);
        intent3.putExtra("calling_package", searchActivity != null ? searchActivity.flattenToShortString() : null);
        intent3.putExtra("android.speech.extra.RESULTS_PENDINGINTENT", activity);
        intent3.putExtra("android.speech.extra.RESULTS_PENDINGINTENT_BUNDLE", bundle);
        return intent3;
    }

    private Intent F(Intent intent, SearchableInfo searchableInfo) {
        Intent intent2 = new Intent(intent);
        ComponentName searchActivity = searchableInfo.getSearchActivity();
        intent2.putExtra("calling_package", searchActivity == null ? null : searchActivity.flattenToShortString());
        return intent2;
    }

    private void G() {
        this.f695t.dismissDropDown();
    }

    private void I(View view, Rect rect) {
        view.getLocationInWindow(this.F);
        getLocationInWindow(this.G);
        int[] iArr = this.F;
        int i3 = iArr[1];
        int[] iArr2 = this.G;
        int i4 = i3 - iArr2[1];
        int i5 = iArr[0] - iArr2[0];
        rect.set(i5, i4, view.getWidth() + i5, view.getHeight() + i4);
    }

    private CharSequence J(CharSequence charSequence) {
        if (!this.T || this.I == null) {
            return charSequence;
        }
        int textSize = (int) (((double) this.f695t.getTextSize()) * 1.25d);
        this.I.setBounds(0, 0, textSize, textSize);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("   ");
        spannableStringBuilder.setSpan(new ImageSpan(this.I), 1, 2, 33);
        spannableStringBuilder.append(charSequence);
        return spannableStringBuilder;
    }

    private boolean K() {
        SearchableInfo searchableInfo = this.f685j0;
        if (searchableInfo == null || !searchableInfo.getVoiceSearchEnabled()) {
            return false;
        }
        Intent intent = null;
        if (this.f685j0.getVoiceSearchLaunchWebSearch()) {
            intent = this.L;
        } else if (this.f685j0.getVoiceSearchLaunchRecognizer()) {
            intent = this.M;
        }
        return (intent == null || getContext().getPackageManager().resolveActivity(intent, 65536) == null) ? false : true;
    }

    static boolean M(Context context) {
        return context.getResources().getConfiguration().orientation == 2;
    }

    private boolean N() {
        return (this.W || this.f680e0) && !L();
    }

    private void O(Intent intent) {
        if (intent == null) {
            return;
        }
        try {
            getContext().startActivity(intent);
        } catch (RuntimeException e3) {
            Log.e("SearchView", "Failed launch activity: " + intent, e3);
        }
    }

    private boolean Q(int i3, int i4, String str) {
        Cursor cursor = this.V.getCursor();
        if (cursor == null || !cursor.moveToPosition(i3)) {
            return false;
        }
        O(D(cursor, i4, str));
        return true;
    }

    private void b0() {
        post(this.f687l0);
    }

    private void c0(int i3) {
        Editable text = this.f695t.getText();
        Cursor cursor = this.V.getCursor();
        if (cursor == null) {
            return;
        }
        if (!cursor.moveToPosition(i3)) {
            setQuery(text);
            return;
        }
        CharSequence charSequenceConvertToString = this.V.convertToString(cursor);
        if (charSequenceConvertToString != null) {
            setQuery(charSequenceConvertToString);
        } else {
            setQuery(text);
        }
    }

    private void e0() {
        boolean z2 = true;
        boolean z3 = !TextUtils.isEmpty(this.f695t.getText());
        if (!z3 && (!this.T || this.f683h0)) {
            z2 = false;
        }
        this.f702z.setVisibility(z2 ? 0 : 8);
        Drawable drawable = this.f702z.getDrawable();
        if (drawable != null) {
            drawable.setState(z3 ? ViewGroup.ENABLED_STATE_SET : ViewGroup.EMPTY_STATE_SET);
        }
    }

    private void g0() {
        CharSequence queryHint = getQueryHint();
        SearchAutoComplete searchAutoComplete = this.f695t;
        if (queryHint == null) {
            queryHint = "";
        }
        searchAutoComplete.setHint(J(queryHint));
    }

    private int getPreferredHeight() {
        return getContext().getResources().getDimensionPixelSize(c.d.f3191g);
    }

    private int getPreferredWidth() {
        return getContext().getResources().getDimensionPixelSize(c.d.f3192h);
    }

    private void h0() {
        this.f695t.setThreshold(this.f685j0.getSuggestThreshold());
        this.f695t.setImeOptions(this.f685j0.getImeOptions());
        int inputType = this.f685j0.getInputType();
        if ((inputType & 15) == 1) {
            inputType &= -65537;
            if (this.f685j0.getSuggestAuthority() != null) {
                inputType = inputType | 65536 | 524288;
            }
        }
        this.f695t.setInputType(inputType);
        v.a aVar = this.V;
        if (aVar != null) {
            aVar.changeCursor(null);
        }
        if (this.f685j0.getSuggestAuthority() != null) {
            u0 u0Var = new u0(getContext(), this, this.f685j0, this.f689n0);
            this.V = u0Var;
            this.f695t.setAdapter(u0Var);
            ((u0) this.V).t(this.f677b0 ? 2 : 1);
        }
    }

    private void i0() {
        this.f699w.setVisibility((N() && (this.f701y.getVisibility() == 0 || this.A.getVisibility() == 0)) ? 0 : 8);
    }

    private void j0(boolean z2) {
        this.f701y.setVisibility((this.W && N() && hasFocus() && (z2 || !this.f680e0)) ? 0 : 8);
    }

    private void k0(boolean z2) {
        this.U = z2;
        int i3 = z2 ? 0 : 8;
        boolean z3 = !TextUtils.isEmpty(this.f695t.getText());
        this.f700x.setVisibility(i3);
        j0(z3);
        this.f697u.setVisibility(z2 ? 8 : 0);
        this.H.setVisibility((this.H.getDrawable() == null || this.T) ? 8 : 0);
        e0();
        l0(!z3);
        i0();
    }

    private void l0(boolean z2) {
        int i3 = 8;
        if (this.f680e0 && !L() && z2) {
            this.f701y.setVisibility(8);
            i3 = 0;
        }
        this.A.setVisibility(i3);
    }

    private void setQuery(CharSequence charSequence) {
        this.f695t.setText(charSequence);
        this.f695t.setSelection(TextUtils.isEmpty(charSequence) ? 0 : charSequence.length());
    }

    void B() {
        if (this.B.getWidth() > 1) {
            Resources resources = getContext().getResources();
            int paddingLeft = this.f698v.getPaddingLeft();
            Rect rect = new Rect();
            boolean zB = j1.b(this);
            int dimensionPixelSize = this.T ? resources.getDimensionPixelSize(c.d.f3189e) + resources.getDimensionPixelSize(c.d.f3190f) : 0;
            this.f695t.getDropDownBackground().getPadding(rect);
            this.f695t.setDropDownHorizontalOffset(zB ? -rect.left : paddingLeft - (rect.left + dimensionPixelSize));
            this.f695t.setDropDownWidth((((this.B.getWidth() + rect.left) + rect.right) + dimensionPixelSize) - paddingLeft);
        }
    }

    void H() {
        if (Build.VERSION.SDK_INT >= 29) {
            k.a(this.f695t);
            return;
        }
        o oVar = f675u0;
        oVar.b(this.f695t);
        oVar.a(this.f695t);
    }

    public boolean L() {
        return this.U;
    }

    void P(int i3, String str, String str2) {
        getContext().startActivity(C("android.intent.action.SEARCH", null, null, str2, i3, str));
    }

    void R() {
        if (!TextUtils.isEmpty(this.f695t.getText())) {
            this.f695t.setText("");
            this.f695t.requestFocus();
            this.f695t.setImeVisibility(true);
        } else if (this.T) {
            l lVar = this.P;
            if (lVar == null || !lVar.a()) {
                clearFocus();
                k0(true);
            }
        }
    }

    boolean S(int i3, int i4, String str) {
        n nVar = this.R;
        if (nVar != null && nVar.b(i3)) {
            return false;
        }
        Q(i3, 0, null);
        this.f695t.setImeVisibility(false);
        G();
        return true;
    }

    boolean T(int i3) {
        n nVar = this.R;
        if (nVar != null && nVar.a(i3)) {
            return false;
        }
        c0(i3);
        return true;
    }

    protected void U(CharSequence charSequence) {
        setQuery(charSequence);
    }

    void V() {
        k0(false);
        this.f695t.requestFocus();
        this.f695t.setImeVisibility(true);
        View.OnClickListener onClickListener = this.S;
        if (onClickListener != null) {
            onClickListener.onClick(this);
        }
    }

    void W() {
        Editable text = this.f695t.getText();
        if (text == null || TextUtils.getTrimmedLength(text) <= 0) {
            return;
        }
        m mVar = this.O;
        if (mVar == null || !mVar.b(text.toString())) {
            if (this.f685j0 != null) {
                P(0, null, text.toString());
            }
            this.f695t.setImeVisibility(false);
            G();
        }
    }

    boolean X(View view, int i3, KeyEvent keyEvent) {
        if (this.f685j0 != null && this.V != null && keyEvent.getAction() == 0 && keyEvent.hasNoModifiers()) {
            if (i3 == 66 || i3 == 84 || i3 == 61) {
                return S(this.f695t.getListSelection(), 0, null);
            }
            if (i3 == 21 || i3 == 22) {
                this.f695t.setSelection(i3 == 21 ? 0 : this.f695t.length());
                this.f695t.setListSelection(0);
                this.f695t.clearListSelection();
                this.f695t.b();
                return true;
            }
            if (i3 == 19) {
                this.f695t.getListSelection();
                return false;
            }
        }
        return false;
    }

    void Y(CharSequence charSequence) {
        Editable text = this.f695t.getText();
        this.f682g0 = text;
        boolean z2 = !TextUtils.isEmpty(text);
        j0(z2);
        l0(!z2);
        e0();
        i0();
        if (this.O != null && !TextUtils.equals(charSequence, this.f681f0)) {
            this.O.a(charSequence.toString());
        }
        this.f681f0 = charSequence.toString();
    }

    void Z() {
        k0(L());
        b0();
        if (this.f695t.hasFocus()) {
            H();
        }
    }

    void a0() {
        SearchableInfo searchableInfo = this.f685j0;
        if (searchableInfo == null) {
            return;
        }
        try {
            if (searchableInfo.getVoiceSearchLaunchWebSearch()) {
                getContext().startActivity(F(this.L, searchableInfo));
            } else if (searchableInfo.getVoiceSearchLaunchRecognizer()) {
                getContext().startActivity(E(this.M, searchableInfo));
            }
        } catch (ActivityNotFoundException unused) {
            Log.w("SearchView", "Could not find voice search activity");
        }
    }

    @Override // androidx.appcompat.view.c
    public void c() {
        if (this.f683h0) {
            return;
        }
        this.f683h0 = true;
        int imeOptions = this.f695t.getImeOptions();
        this.f684i0 = imeOptions;
        this.f695t.setImeOptions(imeOptions | 33554432);
        this.f695t.setText("");
        setIconified(false);
    }

    @Override // android.view.ViewGroup, android.view.View
    public void clearFocus() {
        this.f678c0 = true;
        super.clearFocus();
        this.f695t.clearFocus();
        this.f695t.setImeVisibility(false);
        this.f678c0 = false;
    }

    public void d0(CharSequence charSequence, boolean z2) {
        this.f695t.setText(charSequence);
        if (charSequence != null) {
            SearchAutoComplete searchAutoComplete = this.f695t;
            searchAutoComplete.setSelection(searchAutoComplete.length());
            this.f682g0 = charSequence;
        }
        if (!z2 || TextUtils.isEmpty(charSequence)) {
            return;
        }
        W();
    }

    @Override // androidx.appcompat.view.c
    public void f() {
        d0("", false);
        clearFocus();
        k0(true);
        this.f695t.setImeOptions(this.f684i0);
        this.f683h0 = false;
    }

    void f0() {
        int[] iArr = this.f695t.hasFocus() ? ViewGroup.FOCUSED_STATE_SET : ViewGroup.EMPTY_STATE_SET;
        Drawable background = this.f698v.getBackground();
        if (background != null) {
            background.setState(iArr);
        }
        Drawable background2 = this.f699w.getBackground();
        if (background2 != null) {
            background2.setState(iArr);
        }
        invalidate();
    }

    public int getImeOptions() {
        return this.f695t.getImeOptions();
    }

    public int getInputType() {
        return this.f695t.getInputType();
    }

    public int getMaxWidth() {
        return this.f679d0;
    }

    public CharSequence getQuery() {
        return this.f695t.getText();
    }

    public CharSequence getQueryHint() {
        CharSequence charSequence = this.f676a0;
        if (charSequence != null) {
            return charSequence;
        }
        SearchableInfo searchableInfo = this.f685j0;
        return (searchableInfo == null || searchableInfo.getHintId() == 0) ? this.N : getContext().getText(this.f685j0.getHintId());
    }

    int getSuggestionCommitIconResId() {
        return this.K;
    }

    int getSuggestionRowLayout() {
        return this.J;
    }

    public v.a getSuggestionsAdapter() {
        return this.V;
    }

    @Override // android.view.ViewGroup, android.view.View
    protected void onDetachedFromWindow() {
        removeCallbacks(this.f687l0);
        post(this.f688m0);
        super.onDetachedFromWindow();
    }

    @Override // androidx.appcompat.widget.m0, 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);
        if (z2) {
            I(this.f695t, this.D);
            Rect rect = this.E;
            Rect rect2 = this.D;
            rect.set(rect2.left, 0, rect2.right, i6 - i4);
            q qVar = this.C;
            if (qVar != null) {
                qVar.a(this.E, this.D);
                return;
            }
            q qVar2 = new q(this.E, this.D, this.f695t);
            this.C = qVar2;
            setTouchDelegate(qVar2);
        }
    }

    @Override // androidx.appcompat.widget.m0, android.view.View
    protected void onMeasure(int i3, int i4) {
        int i5;
        if (L()) {
            super.onMeasure(i3, i4);
            return;
        }
        int mode = View.MeasureSpec.getMode(i3);
        int size = View.MeasureSpec.getSize(i3);
        if (mode == Integer.MIN_VALUE) {
            int i6 = this.f679d0;
            size = i6 > 0 ? Math.min(i6, size) : Math.min(getPreferredWidth(), size);
        } else if (mode == 0) {
            size = this.f679d0;
            if (size <= 0) {
                size = getPreferredWidth();
            }
        } else if (mode == 1073741824 && (i5 = this.f679d0) > 0) {
            size = Math.min(i5, size);
        }
        int mode2 = View.MeasureSpec.getMode(i4);
        int size2 = View.MeasureSpec.getSize(i4);
        if (mode2 == Integer.MIN_VALUE) {
            size2 = Math.min(getPreferredHeight(), size2);
        } else if (mode2 == 0) {
            size2 = getPreferredHeight();
        }
        super.onMeasure(View.MeasureSpec.makeMeasureSpec(size, 1073741824), View.MeasureSpec.makeMeasureSpec(size2, 1073741824));
    }

    @Override // android.view.View
    protected void onRestoreInstanceState(Parcelable parcelable) {
        if (!(parcelable instanceof p)) {
            super.onRestoreInstanceState(parcelable);
            return;
        }
        p pVar = (p) parcelable;
        super.onRestoreInstanceState(pVar.j());
        k0(pVar.f721g);
        requestLayout();
    }

    @Override // android.view.View
    protected Parcelable onSaveInstanceState() {
        p pVar = new p(super.onSaveInstanceState());
        pVar.f721g = L();
        return pVar;
    }

    @Override // android.view.View
    public void onWindowFocusChanged(boolean z2) {
        super.onWindowFocusChanged(z2);
        b0();
    }

    @Override // android.view.ViewGroup, android.view.View
    public boolean requestFocus(int i3, Rect rect) {
        if (this.f678c0 || !isFocusable()) {
            return false;
        }
        if (L()) {
            return super.requestFocus(i3, rect);
        }
        boolean zRequestFocus = this.f695t.requestFocus(i3, rect);
        if (zRequestFocus) {
            k0(false);
        }
        return zRequestFocus;
    }

    public void setAppSearchData(Bundle bundle) {
        this.f686k0 = bundle;
    }

    public void setIconified(boolean z2) {
        if (z2) {
            R();
        } else {
            V();
        }
    }

    public void setIconifiedByDefault(boolean z2) {
        if (this.T == z2) {
            return;
        }
        this.T = z2;
        k0(z2);
        g0();
    }

    public void setImeOptions(int i3) {
        this.f695t.setImeOptions(i3);
    }

    public void setInputType(int i3) {
        this.f695t.setInputType(i3);
    }

    public void setMaxWidth(int i3) {
        this.f679d0 = i3;
        requestLayout();
    }

    public void setOnCloseListener(l lVar) {
        this.P = lVar;
    }

    public void setOnQueryTextFocusChangeListener(View.OnFocusChangeListener onFocusChangeListener) {
        this.Q = onFocusChangeListener;
    }

    public void setOnQueryTextListener(m mVar) {
        this.O = mVar;
    }

    public void setOnSearchClickListener(View.OnClickListener onClickListener) {
        this.S = onClickListener;
    }

    public void setOnSuggestionListener(n nVar) {
        this.R = nVar;
    }

    public void setQueryHint(CharSequence charSequence) {
        this.f676a0 = charSequence;
        g0();
    }

    public void setQueryRefinementEnabled(boolean z2) {
        this.f677b0 = z2;
        v.a aVar = this.V;
        if (aVar instanceof u0) {
            ((u0) aVar).t(z2 ? 2 : 1);
        }
    }

    public void setSearchableInfo(SearchableInfo searchableInfo) {
        this.f685j0 = searchableInfo;
        if (searchableInfo != null) {
            h0();
            g0();
        }
        boolean zK = K();
        this.f680e0 = zK;
        if (zK) {
            this.f695t.setPrivateImeOptions("nm");
        }
        k0(L());
    }

    public void setSubmitButtonEnabled(boolean z2) {
        this.W = z2;
        k0(L());
    }

    public void setSuggestionsAdapter(v.a aVar) {
        this.V = aVar;
        this.f695t.setAdapter(aVar);
    }

    public SearchView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, c.a.I);
    }

    public SearchView(Context context, AttributeSet attributeSet, int i3) {
        super(context, attributeSet, i3);
        this.D = new Rect();
        this.E = new Rect();
        this.F = new int[2];
        this.G = new int[2];
        this.f687l0 = new b();
        this.f688m0 = new c();
        this.f689n0 = new WeakHashMap<>();
        f fVar = new f();
        this.f690o0 = fVar;
        this.f691p0 = new g();
        h hVar = new h();
        this.f692q0 = hVar;
        i iVar = new i();
        this.f693r0 = iVar;
        j jVar = new j();
        this.f694s0 = jVar;
        this.f696t0 = new a();
        int[] iArr = c.j.Z1;
        a1 a1VarV = a1.v(context, attributeSet, iArr, i3, 0);
        androidx.core.view.y.p0(this, context, iArr, attributeSet, a1VarV.r(), i3, 0);
        LayoutInflater.from(context).inflate(a1VarV.n(c.j.f3327j2, c.g.f3269r), (ViewGroup) this, true);
        SearchAutoComplete searchAutoComplete = (SearchAutoComplete) findViewById(c.f.D);
        this.f695t = searchAutoComplete;
        searchAutoComplete.setSearchView(this);
        this.f697u = findViewById(c.f.f3251z);
        View viewFindViewById = findViewById(c.f.C);
        this.f698v = viewFindViewById;
        View viewFindViewById2 = findViewById(c.f.J);
        this.f699w = viewFindViewById2;
        ImageView imageView = (ImageView) findViewById(c.f.f3249x);
        this.f700x = imageView;
        ImageView imageView2 = (ImageView) findViewById(c.f.A);
        this.f701y = imageView2;
        ImageView imageView3 = (ImageView) findViewById(c.f.f3250y);
        this.f702z = imageView3;
        ImageView imageView4 = (ImageView) findViewById(c.f.E);
        this.A = imageView4;
        ImageView imageView5 = (ImageView) findViewById(c.f.B);
        this.H = imageView5;
        androidx.core.view.y.v0(viewFindViewById, a1VarV.g(c.j.f3331k2));
        androidx.core.view.y.v0(viewFindViewById2, a1VarV.g(c.j.o2));
        int i4 = c.j.n2;
        imageView.setImageDrawable(a1VarV.g(i4));
        imageView2.setImageDrawable(a1VarV.g(c.j.f3319h2));
        imageView3.setImageDrawable(a1VarV.g(c.j.f3307e2));
        imageView4.setImageDrawable(a1VarV.g(c.j.q2));
        imageView5.setImageDrawable(a1VarV.g(i4));
        this.I = a1VarV.g(c.j.m2);
        d1.a(imageView, getResources().getString(c.h.f3283l));
        this.J = a1VarV.n(c.j.p2, c.g.f3268q);
        this.K = a1VarV.n(c.j.f3311f2, 0);
        imageView.setOnClickListener(fVar);
        imageView3.setOnClickListener(fVar);
        imageView2.setOnClickListener(fVar);
        imageView4.setOnClickListener(fVar);
        searchAutoComplete.setOnClickListener(fVar);
        searchAutoComplete.addTextChangedListener(this.f696t0);
        searchAutoComplete.setOnEditorActionListener(hVar);
        searchAutoComplete.setOnItemClickListener(iVar);
        searchAutoComplete.setOnItemSelectedListener(jVar);
        searchAutoComplete.setOnKeyListener(this.f691p0);
        searchAutoComplete.setOnFocusChangeListener(new d());
        setIconifiedByDefault(a1VarV.a(c.j.f3323i2, true));
        int iF = a1VarV.f(c.j.f3295b2, -1);
        if (iF != -1) {
            setMaxWidth(iF);
        }
        this.N = a1VarV.p(c.j.f3315g2);
        this.f676a0 = a1VarV.p(c.j.l2);
        int iK = a1VarV.k(c.j.f3303d2, -1);
        if (iK != -1) {
            setImeOptions(iK);
        }
        int iK2 = a1VarV.k(c.j.f3299c2, -1);
        if (iK2 != -1) {
            setInputType(iK2);
        }
        setFocusable(a1VarV.a(c.j.f3291a2, true));
        a1VarV.w();
        Intent intent = new Intent("android.speech.action.WEB_SEARCH");
        this.L = intent;
        intent.addFlags(268435456);
        intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "web_search");
        Intent intent2 = new Intent("android.speech.action.RECOGNIZE_SPEECH");
        this.M = intent2;
        intent2.addFlags(268435456);
        View viewFindViewById3 = findViewById(searchAutoComplete.getDropDownAnchor());
        this.B = viewFindViewById3;
        if (viewFindViewById3 != null) {
            viewFindViewById3.addOnLayoutChangeListener(new e());
        }
        k0(this.T);
        g0();
    }
}
