package n;

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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.widget.SearchView;
import com.martsnew.tv.R;
import d5.AbstractC0844a;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class F0 extends Q.c implements View.OnClickListener {

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

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    public final SearchView f14709A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    public final SearchableInfo f14710B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public final Context f14711C;
    public final WeakHashMap D;

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

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    public int f14713F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    public ColorStateList f14714G;
    public int H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    public int f14715I;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    public int f14716J;

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    public int f14717K;

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    public int f14718L;

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    public int f14719M;

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

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

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

    public F0(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap weakHashMap) {
        int suggestionRowLayout = searchView.getSuggestionRowLayout();
        this.f5542r = true;
        this.f5543s = null;
        this.f5541q = false;
        this.f5544t = -1;
        this.f5545u = new Q.a(this);
        this.f5546v = new Q.b(0, this);
        this.f14721y = suggestionRowLayout;
        this.f14720x = suggestionRowLayout;
        this.f14722z = (LayoutInflater) context.getSystemService("layout_inflater");
        this.f14713F = 1;
        this.H = -1;
        this.f14715I = -1;
        this.f14716J = -1;
        this.f14717K = -1;
        this.f14718L = -1;
        this.f14719M = -1;
        this.f14709A = searchView;
        this.f14710B = searchableInfo;
        this.f14712E = searchView.getSuggestionCommitIconResId();
        this.f14711C = context;
        this.D = weakHashMap;
    }

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

    /* JADX WARN: Removed duplicated region for block: B:57:0x012f  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x0131  */
    @Override // Q.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(View view, Cursor cursor) {
        int i;
        Drawable drawableF;
        ActivityInfo activityInfo;
        int iconResource;
        CharSequence charSequenceH;
        E0 e02 = (E0) view.getTag();
        int i5 = this.f14719M;
        int i8 = i5 != -1 ? cursor.getInt(i5) : 0;
        TextView textView = e02.f14703a;
        if (textView != null) {
            String strH = h(cursor, this.H);
            textView.setText(strH);
            if (TextUtils.isEmpty(strH)) {
                textView.setVisibility(8);
            } else {
                textView.setVisibility(0);
            }
        }
        Context context = this.f14711C;
        TextView textView2 = e02.f14704b;
        if (textView2 != null) {
            String strH2 = h(cursor, this.f14716J);
            if (strH2 != null) {
                if (this.f14714G == null) {
                    TypedValue typedValue = new TypedValue();
                    context.getTheme().resolveAttribute(R.attr.textColorSearchUrl, typedValue, true);
                    this.f14714G = context.getResources().getColorStateList(typedValue.resourceId);
                }
                SpannableString spannableString = new SpannableString(strH2);
                spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.f14714G, null), 0, strH2.length(), 33);
                charSequenceH = spannableString;
            } else {
                charSequenceH = h(cursor, this.f14715I);
            }
            if (TextUtils.isEmpty(charSequenceH)) {
                if (textView != null) {
                    textView.setSingleLine(false);
                    textView.setMaxLines(2);
                }
            } else if (textView != null) {
                textView.setSingleLine(true);
                textView.setMaxLines(1);
            }
            textView2.setText(charSequenceH);
            if (TextUtils.isEmpty(charSequenceH)) {
                textView2.setVisibility(8);
            } else {
                textView2.setVisibility(0);
            }
        }
        ImageView imageView = e02.f14705c;
        if (imageView != null) {
            int i9 = this.f14717K;
            if (i9 == -1) {
                drawableF = null;
            } else {
                drawableF = f(cursor.getString(i9));
                if (drawableF == null) {
                    ComponentName searchActivity = this.f14710B.getSearchActivity();
                    String strFlattenToShortString = searchActivity.flattenToShortString();
                    WeakHashMap weakHashMap = this.D;
                    if (weakHashMap.containsKey(strFlattenToShortString)) {
                        Drawable.ConstantState constantState = (Drawable.ConstantState) weakHashMap.get(strFlattenToShortString);
                        drawableF = constantState == null ? null : constantState.newDrawable(context.getResources());
                    } else {
                        PackageManager packageManager = context.getPackageManager();
                        try {
                            activityInfo = packageManager.getActivityInfo(searchActivity, 128);
                            iconResource = activityInfo.getIconResource();
                        } catch (PackageManager.NameNotFoundException e8) {
                            Log.w("SuggestionsAdapter", e8.toString());
                        }
                        if (iconResource != 0) {
                            Drawable drawable = packageManager.getDrawable(searchActivity.getPackageName(), iconResource, activityInfo.applicationInfo);
                            if (drawable == null) {
                                StringBuilder sbO = AbstractC0844a.o(iconResource, "Invalid icon resource ", " for ");
                                sbO.append(searchActivity.flattenToShortString());
                                Log.w("SuggestionsAdapter", sbO.toString());
                                drawableF = null;
                                weakHashMap.put(strFlattenToShortString, drawableF == null ? null : drawableF.getConstantState());
                            } else {
                                drawableF = drawable;
                                weakHashMap.put(strFlattenToShortString, drawableF == null ? null : drawableF.getConstantState());
                            }
                        } else {
                            drawableF = null;
                            weakHashMap.put(strFlattenToShortString, drawableF == null ? null : drawableF.getConstantState());
                        }
                    }
                    if (drawableF == null) {
                        drawableF = context.getPackageManager().getDefaultActivityIcon();
                    }
                }
            }
            imageView.setImageDrawable(drawableF);
            if (drawableF == null) {
                imageView.setVisibility(4);
            } else {
                imageView.setVisibility(0);
                drawableF.setVisible(false, false);
                drawableF.setVisible(true, false);
            }
        }
        ImageView imageView2 = e02.f14706d;
        if (imageView2 == null) {
            i = 1;
        } else {
            int i10 = this.f14718L;
            Drawable drawableF2 = i10 == -1 ? null : f(cursor.getString(i10));
            imageView2.setImageDrawable(drawableF2);
            if (drawableF2 == null) {
                imageView2.setVisibility(8);
                i = 1;
            } else {
                imageView2.setVisibility(0);
                drawableF2.setVisible(false, false);
                i = 1;
                drawableF2.setVisible(true, false);
            }
        }
        int i11 = this.f14713F;
        ImageView imageView3 = e02.f14707e;
        if (i11 != 2 && (i11 != i || (i8 & 1) == 0)) {
            imageView3.setVisibility(8);
            return;
        }
        imageView3.setVisibility(0);
        imageView3.setTag(textView.getText());
        imageView3.setOnClickListener(this);
    }

    @Override // Q.c
    public final void b(Cursor cursor) {
        try {
            super.b(cursor);
            if (cursor != null) {
                this.H = cursor.getColumnIndex("suggest_text_1");
                this.f14715I = cursor.getColumnIndex("suggest_text_2");
                this.f14716J = cursor.getColumnIndex("suggest_text_2_url");
                this.f14717K = cursor.getColumnIndex("suggest_icon_1");
                this.f14718L = cursor.getColumnIndex("suggest_icon_2");
                this.f14719M = cursor.getColumnIndex("suggest_flags");
            }
        } catch (Exception e8) {
            Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e8);
        }
    }

    @Override // Q.c
    public final String c(Cursor cursor) {
        String strH;
        String strH2;
        if (cursor == null) {
            return null;
        }
        String strH3 = h(cursor, cursor.getColumnIndex("suggest_intent_query"));
        if (strH3 != null) {
            return strH3;
        }
        SearchableInfo searchableInfo = this.f14710B;
        if (searchableInfo.shouldRewriteQueryFromData() && (strH2 = h(cursor, cursor.getColumnIndex("suggest_intent_data"))) != null) {
            return strH2;
        }
        if (!searchableInfo.shouldRewriteQueryFromText() || (strH = h(cursor, cursor.getColumnIndex("suggest_text_1"))) == null) {
            return null;
        }
        return strH;
    }

    @Override // Q.c
    public final View d(ViewGroup viewGroup) {
        View viewInflate = this.f14722z.inflate(this.f14720x, viewGroup, false);
        viewInflate.setTag(new E0(viewInflate));
        ((ImageView) viewInflate.findViewById(R.id.edit_query)).setImageResource(this.f14712E);
        return viewInflate;
    }

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

    /* JADX WARN: Removed duplicated region for block: B:54:0x010c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Drawable f(String str) {
        WeakHashMap weakHashMap = this.D;
        Context context = this.f14711C;
        Drawable drawableE = null;
        if (str != null && !str.isEmpty() && !"0".equals(str)) {
            try {
                int i = Integer.parseInt(str);
                String str2 = "android.resource://" + context.getPackageName() + "/" + i;
                Drawable.ConstantState constantState = (Drawable.ConstantState) weakHashMap.get(str2);
                Drawable drawableNewDrawable = constantState == null ? null : constantState.newDrawable();
                if (drawableNewDrawable != null) {
                    return drawableNewDrawable;
                }
                Drawable drawable = context.getDrawable(i);
                if (drawable != null) {
                    weakHashMap.put(str2, drawable.getConstantState());
                }
                return drawable;
            } catch (Resources.NotFoundException unused) {
                Log.w("SuggestionsAdapter", "Icon resource not found: ".concat(str));
                return null;
            } catch (NumberFormatException unused2) {
                Drawable.ConstantState constantState2 = (Drawable.ConstantState) weakHashMap.get(str);
                Drawable drawableNewDrawable2 = constantState2 == null ? null : constantState2.newDrawable();
                if (drawableNewDrawable2 != null) {
                    return drawableNewDrawable2;
                }
                Uri uri = Uri.parse(str);
                try {
                    if ("android.resource".equals(uri.getScheme())) {
                        try {
                            drawableE = e(uri);
                        } catch (Resources.NotFoundException unused3) {
                            throw new FileNotFoundException("Resource does not exist: " + uri);
                        }
                    } else {
                        InputStream inputStreamOpenInputStream = context.getContentResolver().openInputStream(uri);
                        if (inputStreamOpenInputStream == null) {
                            throw new FileNotFoundException("Failed to open " + uri);
                        }
                        try {
                            Drawable drawableCreateFromStream = Drawable.createFromStream(inputStreamOpenInputStream, null);
                            try {
                                inputStreamOpenInputStream.close();
                            } catch (IOException e8) {
                                Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e8);
                            }
                            drawableE = drawableCreateFromStream;
                        } finally {
                        }
                    }
                } catch (FileNotFoundException e9) {
                    Log.w("SuggestionsAdapter", "Icon not found: " + uri + ", " + e9.getMessage());
                    if (drawableE != null) {
                        weakHashMap.put(str, drawableE.getConstantState());
                    }
                    return drawableE;
                }
                if (drawableE != null) {
                }
            }
        }
        return drawableE;
    }

    public final Cursor g(SearchableInfo searchableInfo, String str) {
        String suggestAuthority;
        String[] strArr = null;
        if (searchableInfo == null || (suggestAuthority = searchableInfo.getSuggestAuthority()) == null) {
            return null;
        }
        Uri.Builder builderFragment = new Uri.Builder().scheme("content").authority(suggestAuthority).query("").fragment("");
        String suggestPath = searchableInfo.getSuggestPath();
        if (suggestPath != null) {
            builderFragment.appendEncodedPath(suggestPath);
        }
        builderFragment.appendPath("search_suggest_query");
        String suggestSelection = searchableInfo.getSuggestSelection();
        if (suggestSelection != null) {
            strArr = new String[]{str};
        } else {
            builderFragment.appendPath(str);
        }
        String[] strArr2 = strArr;
        builderFragment.appendQueryParameter("limit", String.valueOf(50));
        return this.f14711C.getContentResolver().query(builderFragment.build(), null, suggestSelection, strArr2, null);
    }

    @Override // Q.c, android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public final View getDropDownView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getDropDownView(i, view, viewGroup);
        } catch (RuntimeException e8) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e8);
            View viewInflate = this.f14722z.inflate(this.f14721y, viewGroup, false);
            if (viewInflate != null) {
                ((E0) viewInflate.getTag()).f14703a.setText(e8.toString());
            }
            return viewInflate;
        }
    }

    @Override // Q.c, android.widget.Adapter
    public final View getView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getView(i, view, viewGroup);
        } catch (RuntimeException e8) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e8);
            View viewD = d(viewGroup);
            ((E0) viewD.getTag()).f14703a.setText(e8.toString());
            return viewD;
        }
    }

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

    @Override // android.widget.BaseAdapter
    public final void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        Cursor cursor = this.f5543s;
        Bundle extras = cursor != null ? cursor.getExtras() : null;
        if (extras != null) {
            extras.getBoolean("in_progress");
        }
    }

    @Override // android.widget.BaseAdapter
    public final void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        Cursor cursor = this.f5543s;
        Bundle extras = cursor != null ? cursor.getExtras() : null;
        if (extras != null) {
            extras.getBoolean("in_progress");
        }
    }

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