package androidx.appcompat.widget;

import android.R;
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 com.seewo.libmcuservice.constant.IRemoteConstants;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.WeakHashMap;

/* JADX INFO: compiled from: SuggestionsAdapter.java */
/* JADX INFO: loaded from: classes.dex */
class c0 extends a.d.a.c implements View.OnClickListener {
    private int A;
    private int B;
    private final SearchManager n;
    private final SearchView o;
    private final SearchableInfo p;
    private final Context q;
    private final WeakHashMap<String, Drawable.ConstantState> r;
    private final int s;
    private boolean t;
    private int u;
    private ColorStateList v;
    private int w;
    private int x;
    private int y;
    private int z;

    /* JADX INFO: compiled from: SuggestionsAdapter.java */
    private static final class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final TextView f564a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final TextView f565b;

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

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        public final ImageView f567d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final ImageView f568e;

        public a(View view) {
            this.f564a = (TextView) view.findViewById(R.id.text1);
            this.f565b = (TextView) view.findViewById(R.id.text2);
            this.f566c = (ImageView) view.findViewById(R.id.icon1);
            this.f567d = (ImageView) view.findViewById(R.id.icon2);
            this.f568e = (ImageView) view.findViewById(a.a.f.k);
        }
    }

    public c0(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap<String, Drawable.ConstantState> weakHashMap) {
        super(context, searchView.getSuggestionRowLayout(), null, true);
        this.t = false;
        this.u = 1;
        this.w = -1;
        this.x = -1;
        this.y = -1;
        this.z = -1;
        this.A = -1;
        this.B = -1;
        this.n = (SearchManager) this.f281f.getSystemService("search");
        this.o = searchView;
        this.p = searchableInfo;
        this.s = searchView.getSuggestionCommitIconResId();
        this.q = context;
        this.r = weakHashMap;
    }

    private void A(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);
    }

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

    private void C(String str, Drawable drawable) {
        if (drawable != null) {
            this.r.put(str, drawable.getConstantState());
        }
    }

    private void D(Cursor cursor) {
        Bundle extras = cursor != null ? cursor.getExtras() : null;
        if (extras == null || extras.getBoolean("in_progress")) {
        }
    }

    private Drawable m(String str) {
        Drawable.ConstantState constantState = this.r.get(str);
        if (constantState == null) {
            return null;
        }
        return constantState.newDrawable();
    }

    private CharSequence n(CharSequence charSequence) {
        if (this.v == null) {
            TypedValue typedValue = new TypedValue();
            this.f281f.getTheme().resolveAttribute(a.a.a.t, typedValue, true);
            this.v = this.f281f.getResources().getColorStateList(typedValue.resourceId);
        }
        SpannableString spannableString = new SpannableString(charSequence);
        spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.v, null), 0, charSequence.length(), 33);
        return spannableString;
    }

    private Drawable o(ComponentName componentName) {
        PackageManager packageManager = this.f281f.getPackageManager();
        try {
            ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, 128);
            int iconResource = activityInfo.getIconResource();
            if (iconResource == 0) {
                return null;
            }
            Drawable drawable = packageManager.getDrawable(componentName.getPackageName(), iconResource, activityInfo.applicationInfo);
            if (drawable != null) {
                return drawable;
            }
            Log.w("SuggestionsAdapter", "Invalid icon resource " + iconResource + " for " + componentName.flattenToShortString());
            return null;
        } catch (PackageManager.NameNotFoundException e2) {
            Log.w("SuggestionsAdapter", e2.toString());
            return null;
        }
    }

    private Drawable p(ComponentName componentName) {
        String strFlattenToShortString = componentName.flattenToShortString();
        if (!this.r.containsKey(strFlattenToShortString)) {
            Drawable drawableO = o(componentName);
            this.r.put(strFlattenToShortString, drawableO != null ? drawableO.getConstantState() : null);
            return drawableO;
        }
        Drawable.ConstantState constantState = this.r.get(strFlattenToShortString);
        if (constantState == null) {
            return null;
        }
        return constantState.newDrawable(this.q.getResources());
    }

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

    private Drawable r(Cursor cursor) {
        Drawable drawableP = p(this.p.getSearchActivity());
        return drawableP != null ? drawableP : this.f281f.getPackageManager().getDefaultActivityIcon();
    }

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

    private Drawable u(String str) {
        if (str == null || str.isEmpty() || "0".equals(str)) {
            return null;
        }
        try {
            int i = Integer.parseInt(str);
            String str2 = "android.resource://" + this.q.getPackageName() + "/" + i;
            Drawable drawableM = m(str2);
            if (drawableM != null) {
                return drawableM;
            }
            Drawable drawableB = a.c.c.a.b(this.q, i);
            C(str2, drawableB);
            return drawableB;
        } catch (Resources.NotFoundException unused) {
            Log.w("SuggestionsAdapter", "Icon resource not found: " + str);
            return null;
        } catch (NumberFormatException unused2) {
            Drawable drawableM2 = m(str);
            if (drawableM2 != null) {
                return drawableM2;
            }
            Drawable drawableS = s(Uri.parse(str));
            C(str, drawableS);
            return drawableS;
        }
    }

    private Drawable v(Cursor cursor) {
        int i = this.z;
        if (i == -1) {
            return null;
        }
        Drawable drawableU = u(cursor.getString(i));
        return drawableU != null ? drawableU : r(cursor);
    }

    private Drawable w(Cursor cursor) {
        int i = this.A;
        if (i == -1) {
            return null;
        }
        return u(cursor.getString(i));
    }

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

    @Override // a.d.a.a, a.d.a.b.a
    public void a(Cursor cursor) {
        if (this.t) {
            Log.w("SuggestionsAdapter", "Tried to change cursor after adapter was closed.");
            if (cursor != null) {
                cursor.close();
                return;
            }
            return;
        }
        try {
            super.a(cursor);
            if (cursor != null) {
                this.w = cursor.getColumnIndex("suggest_text_1");
                this.x = cursor.getColumnIndex("suggest_text_2");
                this.y = cursor.getColumnIndex("suggest_text_2_url");
                this.z = cursor.getColumnIndex("suggest_icon_1");
                this.A = cursor.getColumnIndex("suggest_icon_2");
                this.B = cursor.getColumnIndex("suggest_flags");
            }
        } catch (Exception e2) {
            Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e2);
        }
    }

    @Override // a.d.a.a, a.d.a.b.a
    public CharSequence e(Cursor cursor) {
        String strQ;
        String strQ2;
        if (cursor == null) {
            return null;
        }
        String strQ3 = q(cursor, "suggest_intent_query");
        if (strQ3 != null) {
            return strQ3;
        }
        if (this.p.shouldRewriteQueryFromData() && (strQ2 = q(cursor, "suggest_intent_data")) != null) {
            return strQ2;
        }
        if (!this.p.shouldRewriteQueryFromText() || (strQ = q(cursor, "suggest_text_1")) == null) {
            return null;
        }
        return strQ;
    }

    @Override // a.d.a.b.a
    public Cursor f(CharSequence charSequence) {
        String string = charSequence == null ? "" : charSequence.toString();
        if (this.o.getVisibility() == 0 && this.o.getWindowVisibility() == 0) {
            try {
                Cursor cursorX = x(this.p, string, 50);
                if (cursorX != null) {
                    cursorX.getCount();
                    return cursorX;
                }
            } catch (RuntimeException e2) {
                Log.w("SuggestionsAdapter", "Search suggestions query threw an exception.", e2);
            }
        }
        return null;
    }

    @Override // a.d.a.a
    public void g(View view, Context context, Cursor cursor) {
        a aVar = (a) view.getTag();
        int i = this.B;
        int i2 = i != -1 ? cursor.getInt(i) : 0;
        if (aVar.f564a != null) {
            B(aVar.f564a, y(cursor, this.w));
        }
        if (aVar.f565b != null) {
            String strY = y(cursor, this.y);
            CharSequence charSequenceN = strY != null ? n(strY) : y(cursor, this.x);
            if (TextUtils.isEmpty(charSequenceN)) {
                TextView textView = aVar.f564a;
                if (textView != null) {
                    textView.setSingleLine(false);
                    aVar.f564a.setMaxLines(2);
                }
            } else {
                TextView textView2 = aVar.f564a;
                if (textView2 != null) {
                    textView2.setSingleLine(true);
                    aVar.f564a.setMaxLines(1);
                }
            }
            B(aVar.f565b, charSequenceN);
        }
        ImageView imageView = aVar.f566c;
        if (imageView != null) {
            A(imageView, v(cursor), 4);
        }
        ImageView imageView2 = aVar.f567d;
        if (imageView2 != null) {
            A(imageView2, w(cursor), 8);
        }
        int i3 = this.u;
        if (i3 != 2 && (i3 != 1 || (i2 & 1) == 0)) {
            aVar.f568e.setVisibility(8);
            return;
        }
        aVar.f568e.setVisibility(0);
        aVar.f568e.setTag(aVar.f564a.getText());
        aVar.f568e.setOnClickListener(this);
    }

    @Override // a.d.a.a, android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getDropDownView(i, view, viewGroup);
        } catch (RuntimeException e2) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e2);
            View viewI = i(this.f281f, this.f280e, viewGroup);
            if (viewI != null) {
                ((a) viewI.getTag()).f564a.setText(e2.toString());
            }
            return viewI;
        }
    }

    @Override // a.d.a.a, android.widget.Adapter
    public View getView(int i, View view, ViewGroup viewGroup) {
        try {
            return super.getView(i, view, viewGroup);
        } catch (RuntimeException e2) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e2);
            View viewJ = j(this.f281f, this.f280e, viewGroup);
            if (viewJ != null) {
                ((a) viewJ.getTag()).f564a.setText(e2.toString());
            }
            return viewJ;
        }
    }

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

    @Override // a.d.a.c, a.d.a.a
    public View j(Context context, Cursor cursor, ViewGroup viewGroup) {
        View viewJ = super.j(context, cursor, viewGroup);
        viewJ.setTag(new a(viewJ));
        ((ImageView) viewJ.findViewById(a.a.f.k)).setImageResource(this.s);
        return viewJ;
    }

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

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

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

    Drawable t(Uri uri) throws FileNotFoundException {
        int identifier;
        String authority = uri.getAuthority();
        if (TextUtils.isEmpty(authority)) {
            throw new FileNotFoundException("No authority: " + uri);
        }
        try {
            Resources resourcesForApplication = this.f281f.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);
        }
    }

    Cursor x(SearchableInfo searchableInfo, String str, int i) {
        String suggestAuthority;
        String[] strArr = null;
        if (searchableInfo == null || (suggestAuthority = searchableInfo.getSuggestAuthority()) == null) {
            return null;
        }
        Uri.Builder builderFragment = new Uri.Builder().scheme(IRemoteConstants.KEY_INF_PUBLISH_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;
        if (i > 0) {
            builderFragment.appendQueryParameter("limit", String.valueOf(i));
        }
        return this.f281f.getContentResolver().query(builderFragment.build(), null, suggestSelection, strArr2, null);
    }

    public void z(int i) {
        this.u = i;
    }
}
