package X6FRs_4.KQgRr5e_0.LGe8pk_15;

import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;
import com.wang.avi.BuildConfig;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.WeakHashMap;
/* compiled from: SuggestionsAdapter.java */
/* loaded from: classes.dex */
public class SI64pbG_23 extends X6FRs_4.FSaI4_7.KQgRr5e_0.PGPF6hQ_2 implements View.OnClickListener {

    /* renamed from: FSaI4_7  reason: collision with root package name */
    public int f3171FSaI4_7;

    /* renamed from: IFxWkV_5  reason: collision with root package name */
    public int f3172IFxWkV_5;

    /* renamed from: IMuAHx_6  reason: collision with root package name */
    public int f3173IMuAHx_6;
    public final SearchableInfo KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public ColorStateList f752KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final SearchView f753KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final WeakHashMap<String, Drawable.ConstantState> f754KQgRr5e_0;

    /* renamed from: Mqv2r_3  reason: collision with root package name */
    public final int f3174Mqv2r_3;

    /* renamed from: Mqv2r_3  reason: collision with other field name */
    public boolean f755Mqv2r_3;

    /* renamed from: Nm6q_10  reason: collision with root package name */
    public int f3175Nm6q_10;
    public final Context QxJ9d_1;

    /* renamed from: X6FRs_4  reason: collision with root package name */
    public int f3176X6FRs_4;

    /* renamed from: Xgtqvk_8  reason: collision with root package name */
    public int f3177Xgtqvk_8;

    /* renamed from: XnFJ5_9  reason: collision with root package name */
    public int f3178XnFJ5_9;

    /* compiled from: SuggestionsAdapter.java */
    /* loaded from: classes.dex */
    public static final class KQgRr5e_0 {
        public final ImageView KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final TextView f756KQgRr5e_0;
        public final ImageView PGPF6hQ_2;
        public final ImageView QxJ9d_1;

        /* renamed from: QxJ9d_1  reason: collision with other field name */
        public final TextView f757QxJ9d_1;

        public KQgRr5e_0(View view) {
            this.f756KQgRr5e_0 = (TextView) view.findViewById(16908308);
            this.f757QxJ9d_1 = (TextView) view.findViewById(16908309);
            this.KQgRr5e_0 = (ImageView) view.findViewById(16908295);
            this.QxJ9d_1 = (ImageView) view.findViewById(16908296);
            this.PGPF6hQ_2 = (ImageView) view.findViewById(X6FRs_4.KQgRr5e_0.IFxWkV_5.edit_query);
        }
    }

    public SI64pbG_23(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap<String, Drawable.ConstantState> weakHashMap) {
        super(context, searchView.getSuggestionRowLayout(), null, true);
        this.f755Mqv2r_3 = false;
        this.f3176X6FRs_4 = 1;
        this.f3172IFxWkV_5 = -1;
        this.f3173IMuAHx_6 = -1;
        this.f3171FSaI4_7 = -1;
        this.f3177Xgtqvk_8 = -1;
        this.f3178XnFJ5_9 = -1;
        this.f3175Nm6q_10 = -1;
        SearchManager searchManager = (SearchManager) ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getSystemService("search");
        this.f753KQgRr5e_0 = searchView;
        this.KQgRr5e_0 = searchableInfo;
        this.f3174Mqv2r_3 = searchView.getSuggestionCommitIconResId();
        this.QxJ9d_1 = context;
        this.f754KQgRr5e_0 = weakHashMap;
    }

    public void KQgRr5e_0(int i) {
        this.f3176X6FRs_4 = i;
    }

    public final Drawable PGPF6hQ_2(Cursor cursor) {
        int i = this.f3178XnFJ5_9;
        if (i == -1) {
            return null;
        }
        return QxJ9d_1(cursor.getString(i));
    }

    /* renamed from: QxJ9d_1  reason: collision with other method in class */
    public final void m430QxJ9d_1(Cursor cursor) {
        Bundle extras = cursor != null ? cursor.getExtras() : null;
        if (extras == null || extras.getBoolean("in_progress")) {
        }
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0, android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getDropDownView(i, view, viewGroup);
        } catch (RuntimeException e) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e);
            View KQgRr5e_02 = KQgRr5e_0(((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0, ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f364KQgRr5e_0, viewGroup);
            if (KQgRr5e_02 != null) {
                ((KQgRr5e_0) KQgRr5e_02.getTag()).f756KQgRr5e_0.setText(e.toString());
            }
            return KQgRr5e_02;
        }
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0, android.widget.Adapter
    public View getView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getView(i, view, viewGroup);
        } catch (RuntimeException e) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e);
            View QxJ9d_1 = QxJ9d_1(((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0, ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f364KQgRr5e_0, viewGroup);
            if (QxJ9d_1 != null) {
                ((KQgRr5e_0) QxJ9d_1.getTag()).f756KQgRr5e_0.setText(e.toString());
            }
            return QxJ9d_1;
        }
    }

    @Override // android.widget.BaseAdapter, android.widget.Adapter
    public boolean hasStableIds() {
        return false;
    }

    @Override // android.widget.BaseAdapter
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        m430QxJ9d_1(KQgRr5e_0());
    }

    @Override // android.widget.BaseAdapter
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        m430QxJ9d_1(KQgRr5e_0());
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        Object tag = view.getTag();
        if (tag instanceof CharSequence) {
            this.f753KQgRr5e_0.m850KQgRr5e_0((CharSequence) tag);
        }
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.QxJ9d_1.KQgRr5e_0
    public Cursor KQgRr5e_0(CharSequence charSequence) {
        String charSequence2 = charSequence == null ? BuildConfig.FLAVOR : charSequence.toString();
        if (this.f753KQgRr5e_0.getVisibility() == 0 && this.f753KQgRr5e_0.getWindowVisibility() == 0) {
            try {
                Cursor KQgRr5e_02 = KQgRr5e_0(this.KQgRr5e_0, charSequence2, 50);
                if (KQgRr5e_02 != null) {
                    KQgRr5e_02.getCount();
                    return KQgRr5e_02;
                }
            } catch (RuntimeException e) {
                Log.w("SuggestionsAdapter", "Search suggestions query threw an exception.", e);
            }
        }
        return null;
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.PGPF6hQ_2, X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0
    public View QxJ9d_1(Context context, Cursor cursor, ViewGroup viewGroup) {
        View QxJ9d_1 = super.QxJ9d_1(context, cursor, viewGroup);
        QxJ9d_1.setTag(new KQgRr5e_0(QxJ9d_1));
        ((ImageView) QxJ9d_1.findViewById(X6FRs_4.KQgRr5e_0.IFxWkV_5.edit_query)).setImageResource(this.f3174Mqv2r_3);
        return QxJ9d_1;
    }

    public final Drawable QxJ9d_1(Cursor cursor) {
        int i = this.f3177Xgtqvk_8;
        if (i == -1) {
            return null;
        }
        Drawable QxJ9d_1 = QxJ9d_1(cursor.getString(i));
        return QxJ9d_1 != null ? QxJ9d_1 : KQgRr5e_0(cursor);
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0, X6FRs_4.FSaI4_7.KQgRr5e_0.QxJ9d_1.KQgRr5e_0
    public void KQgRr5e_0(Cursor cursor) {
        if (this.f755Mqv2r_3) {
            Log.w("SuggestionsAdapter", "Tried to change cursor after adapter was closed.");
            if (cursor != null) {
                cursor.close();
                return;
            }
            return;
        }
        try {
            super.KQgRr5e_0(cursor);
            if (cursor != null) {
                this.f3172IFxWkV_5 = cursor.getColumnIndex("suggest_text_1");
                this.f3173IMuAHx_6 = cursor.getColumnIndex("suggest_text_2");
                this.f3171FSaI4_7 = cursor.getColumnIndex("suggest_text_2_url");
                this.f3177Xgtqvk_8 = cursor.getColumnIndex("suggest_icon_1");
                this.f3178XnFJ5_9 = cursor.getColumnIndex("suggest_icon_2");
                this.f3175Nm6q_10 = cursor.getColumnIndex("suggest_flags");
            }
        } catch (Exception e) {
            Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e);
        }
    }

    public final Drawable QxJ9d_1(String str) {
        if (str == null || str.isEmpty() || "0".equals(str)) {
            return null;
        }
        try {
            int parseInt = Integer.parseInt(str);
            String str2 = "android.resource://" + this.QxJ9d_1.getPackageName() + "/" + parseInt;
            Drawable KQgRr5e_02 = KQgRr5e_0(str2);
            if (KQgRr5e_02 != null) {
                return KQgRr5e_02;
            }
            Drawable m332KQgRr5e_0 = X6FRs_4.IMuAHx_6.X6FRs_4.KQgRr5e_0.m332KQgRr5e_0(this.QxJ9d_1, parseInt);
            KQgRr5e_0(str2, m332KQgRr5e_0);
            return m332KQgRr5e_0;
        } catch (Resources.NotFoundException unused) {
            Log.w("SuggestionsAdapter", "Icon resource not found: " + str);
            return null;
        } catch (NumberFormatException unused2) {
            Drawable KQgRr5e_03 = KQgRr5e_0(str);
            if (KQgRr5e_03 != null) {
                return KQgRr5e_03;
            }
            Drawable KQgRr5e_04 = KQgRr5e_0(Uri.parse(str));
            KQgRr5e_0(str, KQgRr5e_04);
            return KQgRr5e_04;
        }
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0
    public void KQgRr5e_0(View view, Context context, Cursor cursor) {
        CharSequence KQgRr5e_02;
        KQgRr5e_0 kQgRr5e_0 = (KQgRr5e_0) view.getTag();
        int i = this.f3175Nm6q_10;
        int i2 = i != -1 ? cursor.getInt(i) : 0;
        if (kQgRr5e_0.f756KQgRr5e_0 != null) {
            KQgRr5e_0(kQgRr5e_0.f756KQgRr5e_0, KQgRr5e_0(cursor, this.f3172IFxWkV_5));
        }
        if (kQgRr5e_0.f757QxJ9d_1 != null) {
            String KQgRr5e_03 = KQgRr5e_0(cursor, this.f3171FSaI4_7);
            if (KQgRr5e_03 != null) {
                KQgRr5e_02 = KQgRr5e_0((CharSequence) KQgRr5e_03);
            } else {
                KQgRr5e_02 = KQgRr5e_0(cursor, this.f3173IMuAHx_6);
            }
            if (TextUtils.isEmpty(KQgRr5e_02)) {
                TextView textView = kQgRr5e_0.f756KQgRr5e_0;
                if (textView != null) {
                    textView.setSingleLine(false);
                    kQgRr5e_0.f756KQgRr5e_0.setMaxLines(2);
                }
            } else {
                TextView textView2 = kQgRr5e_0.f756KQgRr5e_0;
                if (textView2 != null) {
                    textView2.setSingleLine(true);
                    kQgRr5e_0.f756KQgRr5e_0.setMaxLines(1);
                }
            }
            KQgRr5e_0(kQgRr5e_0.f757QxJ9d_1, KQgRr5e_02);
        }
        ImageView imageView = kQgRr5e_0.KQgRr5e_0;
        if (imageView != null) {
            KQgRr5e_0(imageView, QxJ9d_1(cursor), 4);
        }
        ImageView imageView2 = kQgRr5e_0.QxJ9d_1;
        if (imageView2 != null) {
            KQgRr5e_0(imageView2, PGPF6hQ_2(cursor), 8);
        }
        int i3 = this.f3176X6FRs_4;
        if (i3 != 2 && (i3 != 1 || (i2 & 1) == 0)) {
            kQgRr5e_0.PGPF6hQ_2.setVisibility(8);
            return;
        }
        kQgRr5e_0.PGPF6hQ_2.setVisibility(0);
        kQgRr5e_0.PGPF6hQ_2.setTag(kQgRr5e_0.f756KQgRr5e_0.getText());
        kQgRr5e_0.PGPF6hQ_2.setOnClickListener(this);
    }

    public final Drawable QxJ9d_1(ComponentName componentName) {
        String flattenToShortString = componentName.flattenToShortString();
        if (this.f754KQgRr5e_0.containsKey(flattenToShortString)) {
            Drawable.ConstantState constantState = this.f754KQgRr5e_0.get(flattenToShortString);
            if (constantState == null) {
                return null;
            }
            return constantState.newDrawable(this.QxJ9d_1.getResources());
        }
        Drawable KQgRr5e_02 = KQgRr5e_0(componentName);
        this.f754KQgRr5e_0.put(flattenToShortString, KQgRr5e_02 != null ? KQgRr5e_02.getConstantState() : null);
        return KQgRr5e_02;
    }

    public Drawable QxJ9d_1(Uri uri) throws FileNotFoundException {
        int parseInt;
        String authority = uri.getAuthority();
        if (!TextUtils.isEmpty(authority)) {
            try {
                Resources resourcesForApplication = ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getPackageManager().getResourcesForApplication(authority);
                List<String> pathSegments = uri.getPathSegments();
                if (pathSegments != null) {
                    int size = pathSegments.size();
                    if (size == 1) {
                        try {
                            parseInt = Integer.parseInt(pathSegments.get(0));
                        } catch (NumberFormatException unused) {
                            throw new FileNotFoundException("Single path segment is not a resource ID: " + uri);
                        }
                    } else if (size == 2) {
                        parseInt = resourcesForApplication.getIdentifier(pathSegments.get(1), pathSegments.get(0), authority);
                    } else {
                        throw new FileNotFoundException("More than two path segments: " + uri);
                    }
                    if (parseInt != 0) {
                        return resourcesForApplication.getDrawable(parseInt);
                    }
                    throw new FileNotFoundException("No resource found for: " + uri);
                }
                throw new FileNotFoundException("No path: " + uri);
            } catch (PackageManager.NameNotFoundException unused2) {
                throw new FileNotFoundException("No package found for authority: " + uri);
            }
        }
        throw new FileNotFoundException("No authority: " + uri);
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.QxJ9d_1.KQgRr5e_0
    public final CharSequence KQgRr5e_0(CharSequence charSequence) {
        if (this.f752KQgRr5e_0 == null) {
            TypedValue typedValue = new TypedValue();
            ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getTheme().resolveAttribute(X6FRs_4.KQgRr5e_0.KQgRr5e_0.textColorSearchUrl, typedValue, true);
            this.f752KQgRr5e_0 = ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getResources().getColorStateList(typedValue.resourceId);
        }
        SpannableString spannableString = new SpannableString(charSequence);
        spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.f752KQgRr5e_0, null), 0, charSequence.length(), 33);
        return spannableString;
    }

    public final void KQgRr5e_0(TextView textView, CharSequence charSequence) {
        textView.setText(charSequence);
        if (TextUtils.isEmpty(charSequence)) {
            textView.setVisibility(8);
        } else {
            textView.setVisibility(0);
        }
    }

    public final void KQgRr5e_0(ImageView imageView, Drawable drawable, int i) {
        imageView.setImageDrawable(drawable);
        if (drawable == null) {
            imageView.setVisibility(i);
            return;
        }
        imageView.setVisibility(0);
        drawable.setVisible(false, false);
        drawable.setVisible(true, false);
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0, X6FRs_4.FSaI4_7.KQgRr5e_0.QxJ9d_1.KQgRr5e_0
    public CharSequence KQgRr5e_0(Cursor cursor) {
        String KQgRr5e_02;
        String KQgRr5e_03;
        if (cursor == null) {
            return null;
        }
        String KQgRr5e_04 = KQgRr5e_0(cursor, "suggest_intent_query");
        if (KQgRr5e_04 != null) {
            return KQgRr5e_04;
        }
        if (!this.KQgRr5e_0.shouldRewriteQueryFromData() || (KQgRr5e_03 = KQgRr5e_0(cursor, "suggest_intent_data")) == null) {
            if (!this.KQgRr5e_0.shouldRewriteQueryFromText() || (KQgRr5e_02 = KQgRr5e_0(cursor, "suggest_text_1")) == null) {
                return null;
            }
            return KQgRr5e_02;
        }
        return KQgRr5e_03;
    }

    public final Drawable KQgRr5e_0(Uri uri) {
        try {
            if ("android.resource".equals(uri.getScheme())) {
                try {
                    return QxJ9d_1(uri);
                } catch (Resources.NotFoundException unused) {
                    throw new FileNotFoundException("Resource does not exist: " + uri);
                }
            }
            InputStream openInputStream = this.QxJ9d_1.getContentResolver().openInputStream(uri);
            if (openInputStream != null) {
                Drawable createFromStream = Drawable.createFromStream(openInputStream, null);
                try {
                    openInputStream.close();
                } catch (IOException e) {
                    Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e);
                }
                return createFromStream;
            }
            throw new FileNotFoundException("Failed to open " + uri);
        } catch (FileNotFoundException e2) {
            Log.w("SuggestionsAdapter", "Icon not found: " + uri + ", " + e2.getMessage());
            return null;
        }
        Log.w("SuggestionsAdapter", "Icon not found: " + uri + ", " + e2.getMessage());
        return null;
    }

    public final Drawable KQgRr5e_0(String str) {
        Drawable.ConstantState constantState = this.f754KQgRr5e_0.get(str);
        if (constantState == null) {
            return null;
        }
        return constantState.newDrawable();
    }

    public final void KQgRr5e_0(String str, Drawable drawable) {
        if (drawable != null) {
            this.f754KQgRr5e_0.put(str, drawable.getConstantState());
        }
    }

    @Override // X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0, X6FRs_4.FSaI4_7.KQgRr5e_0.QxJ9d_1.KQgRr5e_0
    public final Drawable KQgRr5e_0(Cursor cursor) {
        Drawable QxJ9d_1 = QxJ9d_1(this.KQgRr5e_0.getSearchActivity());
        return QxJ9d_1 != null ? QxJ9d_1 : ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getPackageManager().getDefaultActivityIcon();
    }

    public final Drawable KQgRr5e_0(ComponentName componentName) {
        PackageManager packageManager = ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getPackageManager();
        try {
            ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, RecyclerView.WGk6X_27.FLAG_IGNORE);
            int iconResource = activityInfo.getIconResource();
            if (iconResource == 0) {
                return null;
            }
            Drawable drawable = packageManager.getDrawable(componentName.getPackageName(), iconResource, activityInfo.applicationInfo);
            if (drawable == null) {
                Log.w("SuggestionsAdapter", "Invalid icon resource " + iconResource + " for " + componentName.flattenToShortString());
                return null;
            }
            return drawable;
        } catch (PackageManager.NameNotFoundException e) {
            Log.w("SuggestionsAdapter", e.toString());
            return null;
        }
    }

    public static String KQgRr5e_0(Cursor cursor, String str) {
        return KQgRr5e_0(cursor, cursor.getColumnIndex(str));
    }

    public static String KQgRr5e_0(Cursor cursor, int i) {
        if (i == -1) {
            return null;
        }
        try {
            return cursor.getString(i);
        } catch (Exception e) {
            Log.e("SuggestionsAdapter", "unexpected error retrieving valid column from cursor, did the remote process die?", e);
            return null;
        }
    }

    public Cursor KQgRr5e_0(SearchableInfo searchableInfo, String str, int i) {
        String suggestAuthority;
        String[] strArr = null;
        if (searchableInfo == null || (suggestAuthority = searchableInfo.getSuggestAuthority()) == null) {
            return null;
        }
        Uri.Builder fragment = new Uri.Builder().scheme("content").authority(suggestAuthority).query(BuildConfig.FLAVOR).fragment(BuildConfig.FLAVOR);
        String suggestPath = searchableInfo.getSuggestPath();
        if (suggestPath != null) {
            fragment.appendEncodedPath(suggestPath);
        }
        fragment.appendPath("search_suggest_query");
        String suggestSelection = searchableInfo.getSuggestSelection();
        if (suggestSelection != null) {
            strArr = new String[]{str};
        } else {
            fragment.appendPath(str);
        }
        String[] strArr2 = strArr;
        if (i > 0) {
            fragment.appendQueryParameter("limit", String.valueOf(i));
        }
        return ((X6FRs_4.FSaI4_7.KQgRr5e_0.KQgRr5e_0) this).f363KQgRr5e_0.getContentResolver().query(fragment.build(), null, suggestSelection, strArr2, null);
    }
}
