package androidx.leanback.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.media.SoundPool;
import android.os.Handler;
import android.speech.SpeechRecognizer;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import androidx.leanback.widget.SearchOrbView;
import com.strmr.ps.R;
import defpackage.qz8;

/* JADX INFO: loaded from: classes.dex */
public class SearchBar extends RelativeLayout {
    public static final /* synthetic */ int ac = 0;
    public a a;
    public final Context aa;
    public b ab;
    public SearchEditText b;
    public SpeechOrbView c;
    public ImageView d;
    public String e;
    public String f;
    public String i;
    public Drawable j;
    public final Handler k;
    public final InputMethodManager l;
    public boolean m;
    public Drawable n;
    public final int o;
    public final int p;
    public final int q;
    public final int r;
    public final int s;
    public final int t;
    public SpeechRecognizer u;
    public qz8 v;
    public boolean w;
    public SoundPool x;
    public final SparseIntArray y;
    public boolean z;

    public interface a {
        void a();

        void b();

        void c(String str);
    }

    public interface b {
        void a();
    }

    public SearchBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0);
        this.k = new Handler();
        this.m = false;
        this.y = new SparseIntArray();
        this.z = false;
        this.aa = context;
        Resources resources = getResources();
        LayoutInflater.from(getContext()).inflate(R.layout.lb_search_bar, (ViewGroup) this, true);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1, getResources().getDimensionPixelSize(R.dimen.lb_search_bar_height));
        layoutParams.addRule(10, -1);
        setLayoutParams(layoutParams);
        setBackgroundColor(0);
        setClipChildren(false);
        this.e = "";
        this.l = (InputMethodManager) context.getSystemService("input_method");
        this.p = resources.getColor(R.color.lb_search_bar_text_speech_mode);
        this.o = resources.getColor(R.color.lb_search_bar_text);
        this.t = resources.getInteger(R.integer.lb_search_bar_speech_mode_background_alpha);
        this.s = resources.getInteger(R.integer.lb_search_bar_text_mode_background_alpha);
        this.r = resources.getColor(R.color.lb_search_bar_hint_speech_mode);
        this.q = resources.getColor(R.color.lb_search_bar_hint);
    }

    public final void a() {
        this.l.hideSoftInputFromWindow(this.b.getWindowToken(), 0);
    }

    public final void b() {
        if (this.z) {
            return;
        }
        if (!hasFocus()) {
            requestFocus();
        }
        if (this.v != null) {
            this.b.setText("");
            this.b.setHint("");
            this.v.a();
            this.z = true;
            return;
        }
        if (this.u == null) {
            return;
        }
        if (getContext().checkCallingOrSelfPermission("android.permission.RECORD_AUDIO") != 0) {
            b bVar = this.ab;
            if (bVar == null) {
                throw new IllegalStateException("android.permission.RECORD_AUDIO required for search");
            }
            bVar.a();
            return;
        }
        this.z = true;
        this.b.setText("");
        Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
        intent.putExtra("android.speech.extra.LANGUAGE_MODEL", "free_form");
        intent.putExtra("android.speech.extra.PARTIAL_RESULTS", true);
        this.u.setRecognitionListener(new c0(this));
        this.w = true;
        this.u.startListening(intent);
    }

    public final void c() {
        if (this.z) {
            this.b.setText(this.e);
            this.b.setHint(this.f);
            this.z = false;
            if (this.v != null || this.u == null) {
                return;
            }
            this.c.c();
            if (this.w) {
                this.u.cancel();
                this.w = false;
            }
            this.u.setRecognitionListener(null);
        }
    }

    public final void d() {
        String string = getResources().getString(R.string.lb_search_bar_hint);
        if (!TextUtils.isEmpty(this.i)) {
            string = this.c.isFocused() ? getResources().getString(R.string.lb_search_bar_hint_with_title_speech, this.i) : getResources().getString(R.string.lb_search_bar_hint_with_title, this.i);
        } else if (this.c.isFocused()) {
            string = getResources().getString(R.string.lb_search_bar_hint_speech);
        }
        this.f = string;
        SearchEditText searchEditText = this.b;
        if (searchEditText != null) {
            searchEditText.setHint(string);
        }
    }

    public final void e(boolean z) {
        if (z) {
            this.n.setAlpha(this.t);
            boolean zIsFocused = this.c.isFocused();
            int i = this.r;
            if (zIsFocused) {
                this.b.setTextColor(i);
                this.b.setHintTextColor(i);
            } else {
                this.b.setTextColor(this.p);
                this.b.setHintTextColor(i);
            }
        } else {
            this.n.setAlpha(this.s);
            this.b.setTextColor(this.o);
            this.b.setHintTextColor(this.q);
        }
        d();
    }

    public Drawable getBadgeDrawable() {
        return this.j;
    }

    public CharSequence getHint() {
        return this.f;
    }

    public String getTitle() {
        return this.i;
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.x = new SoundPool(2, 1, 0);
        int[] iArr = {R.raw.lb_voice_failure, R.raw.lb_voice_open, R.raw.lb_voice_no_input, R.raw.lb_voice_success};
        for (int i = 0; i < 4; i++) {
            int i2 = iArr[i];
            this.y.put(i2, this.x.load(this.aa, i2, 1));
        }
    }

    @Override // android.view.ViewGroup, android.view.View
    public final void onDetachedFromWindow() {
        c();
        this.x.release();
        super.onDetachedFromWindow();
    }

    @Override // android.view.View
    public final void onFinishInflate() {
        super.onFinishInflate();
        this.n = ((RelativeLayout) findViewById(R.id.lb_search_bar_items)).getBackground();
        this.b = (SearchEditText) findViewById(R.id.lb_search_text_editor);
        ImageView imageView = (ImageView) findViewById(R.id.lb_search_bar_badge);
        this.d = imageView;
        Drawable drawable = this.j;
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
        this.b.setOnFocusChangeListener(new cr(this));
        this.b.addTextChangedListener(new ct(this, new cs(this)));
        this.b.setOnKeyboardDismissListener(new cu(this));
        this.b.setOnEditorActionListener(new cy(this));
        this.b.setPrivateImeOptions("escapeNorth,voiceDismiss");
        SpeechOrbView speechOrbView = (SpeechOrbView) findViewById(R.id.lb_search_bar_speech_orb);
        this.c = speechOrbView;
        speechOrbView.setOnOrbClickedListener(new cz(this));
        this.c.setOnFocusChangeListener(new a0(this));
        e(hasFocus());
        d();
    }

    public void setBadgeDrawable(Drawable drawable) {
        this.j = drawable;
        ImageView imageView = this.d;
        if (imageView != null) {
            imageView.setImageDrawable(drawable);
            if (drawable != null) {
                this.d.setVisibility(0);
            } else {
                this.d.setVisibility(8);
            }
        }
    }

    @Override // android.view.View
    public void setNextFocusDownId(int i) {
        this.c.setNextFocusDownId(i);
        this.b.setNextFocusDownId(i);
    }

    public void setPermissionListener(b bVar) {
        this.ab = bVar;
    }

    public void setSearchAffordanceColors(SearchOrbView.a aVar) {
        SpeechOrbView speechOrbView = this.c;
        if (speechOrbView != null) {
            speechOrbView.setNotListeningOrbColors(aVar);
        }
    }

    public void setSearchAffordanceColorsInListening(SearchOrbView.a aVar) {
        SpeechOrbView speechOrbView = this.c;
        if (speechOrbView != null) {
            speechOrbView.setListeningOrbColors(aVar);
        }
    }

    public void setSearchBarListener(a aVar) {
        this.a = aVar;
    }

    public void setSearchQuery(String str) {
        c();
        this.b.setText(str);
        setSearchQueryInternal(str);
    }

    public void setSearchQueryInternal(String str) {
        if (TextUtils.equals(this.e, str)) {
            return;
        }
        this.e = str;
        a aVar = this.a;
        if (aVar != null) {
            aVar.c(str);
        }
    }

    @Deprecated
    public void setSpeechRecognitionCallback(qz8 qz8Var) {
        this.v = qz8Var;
        if (qz8Var != null && this.u != null) {
            throw new IllegalStateException("Can't have speech recognizer and request");
        }
    }

    public void setSpeechRecognizer(SpeechRecognizer speechRecognizer) {
        c();
        SpeechRecognizer speechRecognizer2 = this.u;
        if (speechRecognizer2 != null) {
            speechRecognizer2.setRecognitionListener(null);
            if (this.w) {
                this.u.cancel();
                this.w = false;
            }
        }
        this.u = speechRecognizer;
        if (this.v != null && speechRecognizer != null) {
            throw new IllegalStateException("Can't have speech recognizer and request");
        }
    }

    public void setTitle(String str) {
        this.i = str;
        d();
    }
}
