package c.b.g;

import android.R;
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 c.e.c.a;
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: loaded from: classes.dex */
public class g0 extends c.f.a.c implements View.OnClickListener {
    public static final /* synthetic */ int m = 0;
    public final SearchView n;
    public final SearchableInfo o;
    public final Context p;
    public final WeakHashMap<String, Drawable.ConstantState> q;
    public final int r;
    public int s;
    public ColorStateList t;
    public int u;
    public int v;
    public int w;
    public int x;
    public int y;
    public int z;

    public static final class a {

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

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

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

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

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

        public a(View view) {
            this.f4961a = (TextView) view.findViewById(R.id.text1);
            this.f4962b = (TextView) view.findViewById(R.id.text2);
            this.f4963c = (ImageView) view.findViewById(R.id.icon1);
            this.f4964d = (ImageView) view.findViewById(R.id.icon2);
            this.f4965e = (ImageView) view.findViewById(com.ifpdos.factory.R.id.edit_query);
        }
    }

    public g0(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap<String, Drawable.ConstantState> weakHashMap) {
        super(context, searchView.getSuggestionRowLayout(), null, true);
        this.s = 1;
        this.u = -1;
        this.v = -1;
        this.w = -1;
        this.x = -1;
        this.y = -1;
        this.z = -1;
        this.n = searchView;
        this.o = searchableInfo;
        this.r = searchView.getSuggestionCommitIconResId();
        this.p = context;
        this.q = weakHashMap;
    }

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

    @Override // c.f.a.a
    public void a(View view, Context context, Cursor cursor) throws FileNotFoundException {
        Drawable drawableF;
        String string;
        Drawable drawable;
        ActivityInfo activityInfo;
        int iconResource;
        CharSequence charSequenceH;
        a aVar = (a) view.getTag();
        int i2 = this.z;
        int i3 = i2 != -1 ? cursor.getInt(i2) : 0;
        if (aVar.f4961a != null) {
            String strH = h(cursor, this.u);
            TextView textView = aVar.f4961a;
            textView.setText(strH);
            textView.setVisibility(TextUtils.isEmpty(strH) ? 8 : 0);
        }
        if (aVar.f4962b != null) {
            String strH2 = h(cursor, this.w);
            if (strH2 != null) {
                if (this.t == null) {
                    TypedValue typedValue = new TypedValue();
                    this.p.getTheme().resolveAttribute(com.ifpdos.factory.R.attr.textColorSearchUrl, typedValue, true);
                    this.t = this.p.getResources().getColorStateList(typedValue.resourceId);
                }
                SpannableString spannableString = new SpannableString(strH2);
                spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.t, null), 0, strH2.length(), 33);
                charSequenceH = spannableString;
            } else {
                charSequenceH = h(cursor, this.v);
            }
            if (TextUtils.isEmpty(charSequenceH)) {
                TextView textView2 = aVar.f4961a;
                if (textView2 != null) {
                    textView2.setSingleLine(false);
                    aVar.f4961a.setMaxLines(2);
                }
            } else {
                TextView textView3 = aVar.f4961a;
                if (textView3 != null) {
                    textView3.setSingleLine(true);
                    aVar.f4961a.setMaxLines(1);
                }
            }
            TextView textView4 = aVar.f4962b;
            textView4.setText(charSequenceH);
            textView4.setVisibility(TextUtils.isEmpty(charSequenceH) ? 8 : 0);
        }
        ImageView imageView = aVar.f4963c;
        if (imageView != null) {
            int i4 = this.x;
            if (i4 == -1) {
                drawableF = null;
            } else {
                drawableF = f(cursor.getString(i4));
                if (drawableF == null) {
                    ComponentName searchActivity = this.o.getSearchActivity();
                    String strFlattenToShortString = searchActivity.flattenToShortString();
                    if (this.q.containsKey(strFlattenToShortString)) {
                        Drawable.ConstantState constantState = this.q.get(strFlattenToShortString);
                        drawableF = constantState == null ? null : constantState.newDrawable(this.p.getResources());
                    } else {
                        PackageManager packageManager = this.p.getPackageManager();
                        try {
                            activityInfo = packageManager.getActivityInfo(searchActivity, 128);
                            iconResource = activityInfo.getIconResource();
                        } catch (PackageManager.NameNotFoundException e2) {
                            string = e2.toString();
                        }
                        if (iconResource != 0) {
                            drawable = packageManager.getDrawable(searchActivity.getPackageName(), iconResource, activityInfo.applicationInfo);
                            if (drawable == null) {
                                StringBuilder sbD = a.b.c.a.a.D("Invalid icon resource ", iconResource, " for ");
                                sbD.append(searchActivity.flattenToShortString());
                                string = sbD.toString();
                                Log.w("SuggestionsAdapter", string);
                                drawable = null;
                            }
                        } else {
                            drawable = null;
                        }
                        this.q.put(strFlattenToShortString, drawable == null ? null : drawable.getConstantState());
                        drawableF = drawable;
                    }
                    if (drawableF == null) {
                        drawableF = this.p.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 = aVar.f4964d;
        if (imageView2 != null) {
            int i5 = this.y;
            Drawable drawableF2 = i5 != -1 ? f(cursor.getString(i5)) : null;
            imageView2.setImageDrawable(drawableF2);
            if (drawableF2 == null) {
                imageView2.setVisibility(8);
            } else {
                imageView2.setVisibility(0);
                drawableF2.setVisible(false, false);
                drawableF2.setVisible(true, false);
            }
        }
        int i6 = this.s;
        if (i6 != 2 && (i6 != 1 || (i3 & 1) == 0)) {
            aVar.f4965e.setVisibility(8);
            return;
        }
        aVar.f4965e.setVisibility(0);
        aVar.f4965e.setTag(aVar.f4961a.getText());
        aVar.f4965e.setOnClickListener(this);
    }

    @Override // c.f.a.a
    public void b(Cursor cursor) {
        try {
            super.b(cursor);
            if (cursor != null) {
                this.u = cursor.getColumnIndex("suggest_text_1");
                this.v = cursor.getColumnIndex("suggest_text_2");
                this.w = cursor.getColumnIndex("suggest_text_2_url");
                this.x = cursor.getColumnIndex("suggest_icon_1");
                this.y = cursor.getColumnIndex("suggest_icon_2");
                this.z = cursor.getColumnIndex("suggest_flags");
            }
        } catch (Exception e2) {
            Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e2);
        }
    }

    @Override // c.f.a.a
    public CharSequence 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;
        }
        if (this.o.shouldRewriteQueryFromData() && (strH2 = h(cursor, cursor.getColumnIndex("suggest_intent_data"))) != null) {
            return strH2;
        }
        if (!this.o.shouldRewriteQueryFromText() || (strH = h(cursor, cursor.getColumnIndex("suggest_text_1"))) == null) {
            return null;
        }
        return strH;
    }

    @Override // c.f.a.c, c.f.a.a
    public View d(Context context, Cursor cursor, ViewGroup viewGroup) {
        View viewInflate = this.l.inflate(this.f5370j, viewGroup, false);
        viewInflate.setTag(new a(viewInflate));
        ((ImageView) viewInflate.findViewById(com.ifpdos.factory.R.id.edit_query)).setImageResource(this.r);
        return viewInflate;
    }

    public 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.p.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);
        }
    }

    public final Drawable f(String str) throws FileNotFoundException {
        Drawable drawableE = null;
        if (str != null && !str.isEmpty() && !"0".equals(str)) {
            try {
                int i2 = Integer.parseInt(str);
                String str2 = "android.resource://" + this.p.getPackageName() + "/" + i2;
                Drawable.ConstantState constantState = this.q.get(str2);
                Drawable drawableNewDrawable = constantState == null ? null : constantState.newDrawable();
                if (drawableNewDrawable != null) {
                    return drawableNewDrawable;
                }
                Context context = this.p;
                Object obj = c.e.c.a.f5166a;
                Drawable drawableB = a.b.b(context, i2);
                if (drawableB != null) {
                    this.q.put(str2, drawableB.getConstantState());
                }
                return drawableB;
            } catch (Resources.NotFoundException unused) {
                Log.w("SuggestionsAdapter", "Icon resource not found: " + str);
                return null;
            } catch (NumberFormatException unused2) {
                Drawable.ConstantState constantState2 = this.q.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 = this.p.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 e2) {
                                Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e2);
                            }
                            drawableE = drawableCreateFromStream;
                        } finally {
                        }
                    }
                } catch (FileNotFoundException e3) {
                    Log.w("SuggestionsAdapter", "Icon not found: " + uri + ", " + e3.getMessage());
                }
                if (drawableE != null) {
                    this.q.put(str, drawableE.getConstantState());
                }
            }
        }
        return drawableE;
    }

    public Cursor g(SearchableInfo searchableInfo, String str, int i2) {
        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 (i2 > 0) {
            builderFragment.appendQueryParameter("limit", String.valueOf(i2));
        }
        return this.p.getContentResolver().query(builderFragment.build(), null, suggestSelection, strArr2, null);
    }

    @Override // c.f.a.a, android.widget.BaseAdapter, android.widget.SpinnerAdapter
    public View getDropDownView(int i2, View view, ViewGroup viewGroup) {
        try {
            return super.getDropDownView(i2, view, viewGroup);
        } catch (RuntimeException e2) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e2);
            View viewInflate = this.l.inflate(this.k, viewGroup, false);
            if (viewInflate != null) {
                ((a) viewInflate.getTag()).f4961a.setText(e2.toString());
            }
            return viewInflate;
        }
    }

    @Override // c.f.a.a, android.widget.Adapter
    public View getView(int i2, View view, ViewGroup viewGroup) {
        try {
            return super.getView(i2, view, viewGroup);
        } catch (RuntimeException e2) {
            Log.w("SuggestionsAdapter", "Search suggestions cursor threw exception.", e2);
            View viewD = d(this.p, this.f5361d, viewGroup);
            ((a) viewD.getTag()).f4961a.setText(e2.toString());
            return viewD;
        }
    }

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

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

    @Override // android.widget.BaseAdapter
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        i(this.f5361d);
    }

    @Override // android.widget.BaseAdapter
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        i(this.f5361d);
    }

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