package ῲ.ῲ.㴹;

import android.annotation.SuppressLint;
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.R$attr;
import androidx.appcompat.R$id;
import androidx.appcompat.widget.SearchView;
import com.appsflyer.share.Constants;
import com.facebook.appevents.AppEventsConstants;
import com.facebook.places.PlaceManager;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.WeakHashMap;
import ඓ.K.K.K.C1293;
import ῲ.ᨨ.ῲ.C1806;
import ῲ.㳳.K.AbstractC2247;

@SuppressLint({"RestrictedAPI"})
/* renamed from: ῲ.ῲ.㴹.㮔  reason: contains not printable characters */
/* compiled from: SuggestionsAdapter */
public class View$OnClickListenerC2038 extends AbstractC2247 implements View.OnClickListener {

    /* renamed from: Ԕ  reason: contains not printable characters */
    public final SearchableInfo f6905;

    /* renamed from: Ԛ  reason: contains not printable characters */
    public int f6906 = -1;

    /* renamed from: ؿ  reason: contains not printable characters */
    public final WeakHashMap<String, Drawable.ConstantState> f6907;

    /* renamed from: ݜ  reason: contains not printable characters */
    public int f6908 = 1;

    /* renamed from: ᑓ  reason: contains not printable characters */
    public int f6909 = -1;

    /* renamed from: ᓆ  reason: contains not printable characters */
    public int f6910 = -1;

    /* renamed from: ᓣ  reason: contains not printable characters */
    public final int f6911;

    /* renamed from: ゎ  reason: contains not printable characters */
    public int f6912 = -1;

    /* renamed from: 㗗  reason: contains not printable characters */
    public final Context f6913;

    /* renamed from: 㗽  reason: contains not printable characters */
    public boolean f6914 = false;

    /* renamed from: 㴪  reason: contains not printable characters */
    public int f6915 = -1;

    /* renamed from: 㶋  reason: contains not printable characters */
    public int f6916 = -1;

    /* renamed from: 䀟  reason: contains not printable characters */
    public ColorStateList f6917;

    /* renamed from: 䁗  reason: contains not printable characters */
    public final SearchView f6918;

    /* renamed from: ῲ.ῲ.㴹.㮔$K  reason: contains not printable characters */
    /* compiled from: SuggestionsAdapter */
    public static final class C2039 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final ImageView f6919;

        /* renamed from: ඓ  reason: contains not printable characters */
        public final ImageView f6920;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final TextView f6921;

        /* renamed from: K  reason: contains not printable characters */
        public final TextView f6922;

        /* renamed from: 㸾  reason: contains not printable characters */
        public final ImageView f6923;

        public C2039(View view) {
            this.f6922 = (TextView) view.findViewById(16908308);
            this.f6921 = (TextView) view.findViewById(16908309);
            this.f6919 = (ImageView) view.findViewById(16908295);
            this.f6923 = (ImageView) view.findViewById(16908296);
            this.f6920 = (ImageView) view.findViewById(R$id.edit_query);
        }
    }

    public View$OnClickListenerC2038(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap<String, Drawable.ConstantState> weakHashMap) {
        super(context, searchView.getSuggestionRowLayout(), null, true);
        this.f6918 = searchView;
        this.f6905 = searchableInfo;
        this.f6911 = searchView.getSuggestionCommitIconResId();
        this.f6913 = context;
        this.f6907 = weakHashMap;
    }

    @Override // ῲ.㳳.K.AbstractC2250
    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 inflate = this.f7747.inflate(this.f7746, viewGroup, false);
            if (inflate != null) {
                ((C2039) inflate.getTag()).f6922.setText(e.toString());
            }
            return inflate;
        }
    }

    @Override // ῲ.㳳.K.AbstractC2250
    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 r3 = m5287(this.f7751, this.f7756, viewGroup);
            if (r3 != null) {
                ((C2039) r3.getTag()).f6922.setText(e.toString());
            }
            return r3;
        }
    }

    public boolean hasStableIds() {
        return false;
    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        m5281(this.f7756);
    }

    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        m5281(this.f7756);
    }

    public void onClick(View view) {
        Object tag = view.getTag();
        if (tag instanceof CharSequence) {
            this.f6918.m352((CharSequence) tag);
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public final void m5281(Cursor cursor) {
        Bundle extras = cursor != null ? cursor.getExtras() : null;
        if (extras == null || extras.getBoolean("in_progress")) {
        }
    }

    @Override // ῲ.㳳.K.AbstractC2250
    /* renamed from: ῲ  reason: contains not printable characters */
    public CharSequence m5282(Cursor cursor) {
        String r3;
        String r1;
        if (cursor == null) {
            return null;
        }
        String r12 = m5279(cursor, cursor.getColumnIndex("suggest_intent_query"));
        if (r12 != null) {
            return r12;
        }
        if (this.f6905.shouldRewriteQueryFromData() && (r1 = m5279(cursor, cursor.getColumnIndex("suggest_intent_data"))) != null) {
            return r1;
        }
        if (!this.f6905.shouldRewriteQueryFromText() || (r3 = m5279(cursor, cursor.getColumnIndex("suggest_text_1"))) == null) {
            return null;
        }
        return r3;
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m5288(int i) {
        this.f6908 = i;
    }

    /* renamed from: K  reason: contains not printable characters */
    public Cursor m5284(CharSequence charSequence) {
        String charSequence2 = charSequence == null ? "" : charSequence.toString();
        if (this.f6918.getVisibility() == 0 && this.f6918.getWindowVisibility() == 0) {
            try {
                Cursor r4 = m5283(this.f6905, charSequence2, 50);
                if (r4 != null) {
                    r4.getCount();
                    return r4;
                }
            } catch (RuntimeException e) {
                Log.w("SuggestionsAdapter", "Search suggestions query threw an exception.", e);
            }
        }
        return null;
    }

    @Override // ῲ.㳳.K.AbstractC2250
    /* renamed from: K  reason: contains not printable characters */
    public void m5289(Cursor cursor) {
        if (this.f6914) {
            Log.w("SuggestionsAdapter", "Tried to change cursor after adapter was closed.");
            if (cursor != null) {
                cursor.close();
                return;
            }
            return;
        }
        try {
            super.m5874(cursor);
            if (cursor != null) {
                this.f6906 = cursor.getColumnIndex("suggest_text_1");
                this.f6916 = cursor.getColumnIndex("suggest_text_2");
                this.f6910 = cursor.getColumnIndex("suggest_text_2_url");
                this.f6915 = cursor.getColumnIndex("suggest_icon_1");
                this.f6909 = cursor.getColumnIndex("suggest_icon_2");
                this.f6912 = cursor.getColumnIndex("suggest_flags");
            }
        } catch (Exception e) {
            Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e);
        }
    }

    @Override // ῲ.㳳.K.AbstractC2247, ῲ.㳳.K.AbstractC2250
    /* renamed from: K  reason: contains not printable characters */
    public View m5287(Context context, Cursor cursor, ViewGroup viewGroup) {
        View r1 = super.m5871(context, cursor, viewGroup);
        r1.setTag(new C2039(r1));
        ((ImageView) r1.findViewById(R$id.edit_query)).setImageResource(this.f6911);
        return r1;
    }

    /* JADX DEBUG: Multi-variable search result rejected for r10v19, resolved type: android.text.SpannableString */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARNING: Removed duplicated region for block: B:57:0x014f  */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x0151  */
    @Override // ῲ.㳳.K.AbstractC2250
    /* renamed from: K  reason: contains not printable characters */
    public void m5290(View view, Context context, Cursor cursor) {
        Drawable drawable;
        Drawable drawable2;
        Drawable.ConstantState constantState;
        String str;
        C2039 k = (C2039) view.getTag();
        int i = this.f6912;
        int i2 = i != -1 ? cursor.getInt(i) : 0;
        if (k.f6922 != null) {
            String r0 = m5279(cursor, this.f6906);
            TextView textView = k.f6922;
            textView.setText(r0);
            if (TextUtils.isEmpty(r0)) {
                textView.setVisibility(8);
            } else {
                textView.setVisibility(0);
            }
        }
        if (k.f6921 != null) {
            String r02 = m5279(cursor, this.f6910);
            if (r02 != null) {
                if (this.f6917 == null) {
                    TypedValue typedValue = new TypedValue();
                    this.f7751.getTheme().resolveAttribute(R$attr.textColorSearchUrl, typedValue, true);
                    this.f6917 = this.f7751.getResources().getColorStateList(typedValue.resourceId);
                }
                SpannableString spannableString = new SpannableString(r02);
                spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.f6917, null), 0, r02.length(), 33);
                str = spannableString;
            } else {
                str = m5279(cursor, this.f6916);
            }
            if (TextUtils.isEmpty(str)) {
                TextView textView2 = k.f6922;
                if (textView2 != null) {
                    textView2.setSingleLine(false);
                    k.f6922.setMaxLines(2);
                }
            } else {
                TextView textView3 = k.f6922;
                if (textView3 != null) {
                    textView3.setSingleLine(true);
                    k.f6922.setMaxLines(1);
                }
            }
            TextView textView4 = k.f6921;
            textView4.setText(str);
            if (TextUtils.isEmpty(str)) {
                textView4.setVisibility(8);
            } else {
                textView4.setVisibility(0);
            }
        }
        ImageView imageView = k.f6919;
        if (imageView != null) {
            int i3 = this.f6915;
            if (i3 == -1) {
                drawable2 = null;
            } else {
                Drawable r03 = m5286(cursor.getString(i3));
                if (r03 == null) {
                    ComponentName searchActivity = this.f6905.getSearchActivity();
                    String flattenToShortString = searchActivity.flattenToShortString();
                    if (this.f6907.containsKey(flattenToShortString)) {
                        Drawable.ConstantState constantState2 = this.f6907.get(flattenToShortString);
                        if (constantState2 == null) {
                            drawable2 = null;
                        } else {
                            drawable2 = constantState2.newDrawable(this.f6913.getResources());
                        }
                    } else {
                        PackageManager packageManager = this.f7751.getPackageManager();
                        try {
                            ActivityInfo activityInfo = packageManager.getActivityInfo(searchActivity, 128);
                            int iconResource = activityInfo.getIconResource();
                            if (iconResource != 0) {
                                drawable2 = packageManager.getDrawable(searchActivity.getPackageName(), iconResource, activityInfo.applicationInfo);
                                if (drawable2 == null) {
                                    Log.w("SuggestionsAdapter", "Invalid icon resource " + iconResource + " for " + searchActivity.flattenToShortString());
                                }
                                if (drawable2 != null) {
                                    constantState = null;
                                } else {
                                    constantState = drawable2.getConstantState();
                                }
                                this.f6907.put(flattenToShortString, constantState);
                            }
                        } catch (PackageManager.NameNotFoundException e) {
                            Log.w("SuggestionsAdapter", e.toString());
                        }
                        drawable2 = null;
                        if (drawable2 != null) {
                        }
                        this.f6907.put(flattenToShortString, constantState);
                    }
                    if (drawable2 == null) {
                        r03 = this.f7751.getPackageManager().getDefaultActivityIcon();
                    }
                }
                drawable2 = r03;
            }
            imageView.setImageDrawable(drawable2);
            if (drawable2 == null) {
                imageView.setVisibility(4);
            } else {
                imageView.setVisibility(0);
                drawable2.setVisible(false, false);
                drawable2.setVisible(true, false);
            }
        }
        ImageView imageView2 = k.f6923;
        if (imageView2 != null) {
            int i4 = this.f6909;
            if (i4 == -1) {
                drawable = null;
            } else {
                drawable = m5286(cursor.getString(i4));
            }
            imageView2.setImageDrawable(drawable);
            if (drawable == null) {
                imageView2.setVisibility(8);
            } else {
                imageView2.setVisibility(0);
                drawable.setVisible(false, false);
                drawable.setVisible(true, false);
            }
        }
        int i5 = this.f6908;
        if (i5 == 2 || (i5 == 1 && (i2 & 1) != 0)) {
            k.f6920.setVisibility(0);
            k.f6920.setTag(k.f6922.getText());
            k.f6920.setOnClickListener(this);
            return;
        }
        k.f6920.setVisibility(8);
    }

    /* renamed from: K  reason: contains not printable characters */
    public final Drawable m5286(String str) {
        Drawable drawable;
        Uri parse;
        InputStream openInputStream;
        Drawable drawable2;
        Drawable drawable3 = null;
        if (str != null && !str.isEmpty() && !AppEventsConstants.EVENT_PARAM_VALUE_NO.equals(str)) {
            try {
                int parseInt = Integer.parseInt(str);
                String str2 = "android.resource://" + this.f6913.getPackageName() + Constants.URL_PATH_DELIMITER + parseInt;
                Drawable.ConstantState constantState = this.f6907.get(str2);
                if (constantState == null) {
                    drawable2 = null;
                } else {
                    drawable2 = constantState.newDrawable();
                }
                if (drawable2 != null) {
                    return drawable2;
                }
                Drawable r2 = C1806.m4683(this.f6913, parseInt);
                if (r2 != null) {
                    this.f6907.put(str2, r2.getConstantState());
                }
                return r2;
            } catch (NumberFormatException unused) {
                Drawable.ConstantState constantState2 = this.f6907.get(str);
                if (constantState2 == null) {
                    drawable = null;
                } else {
                    drawable = constantState2.newDrawable();
                }
                if (drawable != null) {
                    return drawable;
                }
                parse = Uri.parse(str);
                try {
                    if ("android.resource".equals(parse.getScheme())) {
                        try {
                            drawable3 = m5285(parse);
                        } catch (Resources.NotFoundException unused2) {
                            throw new FileNotFoundException("Resource does not exist: " + parse);
                        }
                    } else {
                        openInputStream = this.f6913.getContentResolver().openInputStream(parse);
                        if (openInputStream != null) {
                            Drawable createFromStream = Drawable.createFromStream(openInputStream, null);
                            try {
                                openInputStream.close();
                            } catch (IOException e) {
                                Log.e("SuggestionsAdapter", "Error closing icon stream for " + parse, e);
                            }
                            drawable3 = createFromStream;
                        } else {
                            throw new FileNotFoundException("Failed to open " + parse);
                        }
                    }
                } catch (FileNotFoundException e2) {
                    Log.w("SuggestionsAdapter", "Icon not found: " + parse + ", " + e2.getMessage());
                }
                if (drawable3 != null) {
                    this.f6907.put(str, drawable3.getConstantState());
                }
            } catch (Resources.NotFoundException unused3) {
                Log.w("SuggestionsAdapter", "Icon resource not found: " + str);
                return null;
            } catch (Throwable th) {
                try {
                    openInputStream.close();
                } catch (IOException e3) {
                    Log.e("SuggestionsAdapter", "Error closing icon stream for " + parse, e3);
                }
                throw th;
            }
        }
        return drawable3;
    }

    /* renamed from: K  reason: contains not printable characters */
    public static String m5280(Cursor cursor, String str) {
        return m5279(cursor, cursor.getColumnIndex(str));
    }

    /* renamed from: K  reason: contains not printable characters */
    public static String m5279(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;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public Drawable m5285(Uri uri) throws FileNotFoundException {
        int i;
        String authority = uri.getAuthority();
        if (!TextUtils.isEmpty(authority)) {
            try {
                Resources resourcesForApplication = this.f7751.getPackageManager().getResourcesForApplication(authority);
                List<String> pathSegments = uri.getPathSegments();
                if (pathSegments != null) {
                    int size = pathSegments.size();
                    if (size == 1) {
                        try {
                            i = Integer.parseInt(pathSegments.get(0));
                        } catch (NumberFormatException unused) {
                            throw new FileNotFoundException(C1293.m3410("Single path segment is not a resource ID: ", uri));
                        }
                    } else if (size == 2) {
                        i = resourcesForApplication.getIdentifier(pathSegments.get(1), pathSegments.get(0), authority);
                    } else {
                        throw new FileNotFoundException(C1293.m3410("More than two path segments: ", uri));
                    }
                    if (i != 0) {
                        return resourcesForApplication.getDrawable(i);
                    }
                    throw new FileNotFoundException(C1293.m3410("No resource found for: ", uri));
                }
                throw new FileNotFoundException(C1293.m3410("No path: ", uri));
            } catch (PackageManager.NameNotFoundException unused2) {
                throw new FileNotFoundException(C1293.m3410("No package found for authority: ", uri));
            }
        } else {
            throw new FileNotFoundException(C1293.m3410("No authority: ", uri));
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public Cursor m5283(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("").fragment("");
        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);
        }
        if (i > 0) {
            fragment.appendQueryParameter(PlaceManager.PARAM_LIMIT, String.valueOf(i));
        }
        return this.f7751.getContentResolver().query(fragment.build(), null, suggestSelection, strArr, null);
    }
}
