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 com.debridstream.tv.R;

/* JADX INFO: loaded from: classes.dex */
public class SearchBar extends RelativeLayout {

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public static final /* synthetic */ int f11883A = 0;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public SearchEditText f11884a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public SpeechOrbView f11885b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public ImageView f11886c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public String f11887d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public String f11888e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public String f11889f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public Drawable f11890j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final Handler f11891k;
    public final InputMethodManager l;

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

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public Drawable f11893n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final int f11894o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final int f11895p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final int f11896q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final int f11897r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final int f11898s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final int f11899t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public SpeechRecognizer f11900u;

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public SoundPool f11902w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final SparseIntArray f11903x;

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

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public final Context f11905z;

    public SearchBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0);
        this.f11891k = new Handler();
        this.f11892m = false;
        this.f11903x = new SparseIntArray();
        this.f11904y = false;
        this.f11905z = 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.f11887d = "";
        this.l = (InputMethodManager) context.getSystemService("input_method");
        this.f11895p = resources.getColor(R.color.lb_search_bar_text_speech_mode);
        this.f11894o = resources.getColor(R.color.lb_search_bar_text);
        this.f11899t = resources.getInteger(R.integer.lb_search_bar_speech_mode_background_alpha);
        this.f11898s = resources.getInteger(R.integer.lb_search_bar_text_mode_background_alpha);
        this.f11897r = resources.getColor(R.color.lb_search_bar_hint_speech_mode);
        this.f11896q = resources.getColor(R.color.lb_search_bar_hint);
    }

    public final void a() {
        if (this.f11904y) {
            return;
        }
        if (!hasFocus()) {
            requestFocus();
        }
        if (this.f11900u == null) {
            return;
        }
        if (getContext().checkCallingOrSelfPermission("android.permission.RECORD_AUDIO") != 0) {
            throw new IllegalStateException("android.permission.RECORD_AUDIO required for search");
        }
        this.f11904y = true;
        this.f11884a.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.f11900u.setRecognitionListener(new H(this));
        this.f11901v = true;
        this.f11900u.startListening(intent);
    }

    public final void b() {
        if (this.f11904y) {
            this.f11884a.setText(this.f11887d);
            this.f11884a.setHint(this.f11888e);
            this.f11904y = false;
            if (this.f11900u == null) {
                return;
            }
            this.f11885b.c();
            if (this.f11901v) {
                this.f11900u.cancel();
                this.f11901v = false;
            }
            this.f11900u.setRecognitionListener(null);
        }
    }

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

    public final void d(boolean z6) {
        if (z6) {
            this.f11893n.setAlpha(this.f11899t);
            boolean zIsFocused = this.f11885b.isFocused();
            int i6 = this.f11897r;
            if (zIsFocused) {
                this.f11884a.setTextColor(i6);
                this.f11884a.setHintTextColor(i6);
            } else {
                this.f11884a.setTextColor(this.f11895p);
                this.f11884a.setHintTextColor(i6);
            }
        } else {
            this.f11893n.setAlpha(this.f11898s);
            this.f11884a.setTextColor(this.f11894o);
            this.f11884a.setHintTextColor(this.f11896q);
        }
        c();
    }

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

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

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

    @Override // android.view.ViewGroup, android.view.View
    public final void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.f11902w = 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 i6 = 0; i6 < 4; i6++) {
            int i7 = iArr[i6];
            this.f11903x.put(i7, this.f11902w.load(this.f11905z, i7, 1));
        }
    }

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

    @Override // android.view.View
    public final void onFinishInflate() {
        super.onFinishInflate();
        this.f11893n = ((RelativeLayout) findViewById(R.id.lb_search_bar_items)).getBackground();
        this.f11884a = (SearchEditText) findViewById(R.id.lb_search_text_editor);
        ImageView imageView = (ImageView) findViewById(R.id.lb_search_bar_badge);
        this.f11886c = imageView;
        Drawable drawable = this.f11890j;
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
        this.f11884a.setOnFocusChangeListener(new C(this, 0));
        this.f11884a.addTextChangedListener(new E(this, new D(this, 0)));
        this.f11884a.setOnKeyboardDismissListener(new C0.B(this, 25));
        this.f11884a.setOnEditorActionListener(new F(this));
        this.f11884a.setPrivateImeOptions("escapeNorth,voiceDismiss");
        SpeechOrbView speechOrbView = (SpeechOrbView) findViewById(R.id.lb_search_bar_speech_orb);
        this.f11885b = speechOrbView;
        speechOrbView.setOnOrbClickedListener(new G(this));
        this.f11885b.setOnFocusChangeListener(new C(this, 1));
        d(hasFocus());
        c();
    }

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

    @Override // android.view.View
    public void setNextFocusDownId(int i6) {
        this.f11885b.setNextFocusDownId(i6);
        this.f11884a.setNextFocusDownId(i6);
    }

    public void setPermissionListener(J j4) {
    }

    public void setSearchAffordanceColors(M m6) {
        SpeechOrbView speechOrbView = this.f11885b;
        if (speechOrbView != null) {
            speechOrbView.setNotListeningOrbColors(m6);
        }
    }

    public void setSearchAffordanceColorsInListening(M m6) {
        SpeechOrbView speechOrbView = this.f11885b;
        if (speechOrbView != null) {
            speechOrbView.setListeningOrbColors(m6);
        }
    }

    public void setSearchBarListener(I i6) {
    }

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

    public void setSearchQueryInternal(String str) {
        if (TextUtils.equals(this.f11887d, str)) {
            return;
        }
        this.f11887d = str;
    }

    @Deprecated
    public void setSpeechRecognitionCallback(P p6) {
    }

    public void setSpeechRecognizer(SpeechRecognizer speechRecognizer) {
        b();
        SpeechRecognizer speechRecognizer2 = this.f11900u;
        if (speechRecognizer2 != null) {
            speechRecognizer2.setRecognitionListener(null);
            if (this.f11901v) {
                this.f11900u.cancel();
                this.f11901v = false;
            }
        }
        this.f11900u = speechRecognizer;
    }

    public void setTitle(String str) {
        this.f11889f = str;
        c();
    }
}
