package androidx.appcompat.widget; 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.R$attr; import androidx.appcompat.R$id; import androidx.core.content.ContextCompat; import androidx.cursoradapter.widget.ResourceCursorAdapter; import com.ubtrobot.jimu.robotapi.PeripheralType; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.WeakHashMap; /* loaded from: classes.dex */ class SuggestionsAdapter extends ResourceCursorAdapter implements View.OnClickListener { private final SearchView l; private final SearchableInfo m; private final Context n; private final WeakHashMap o; private final int p; private boolean q; private int r; private ColorStateList s; private int t; private int u; private int v; private int x; private int y; private int z; private static final class ChildViewCache { public final TextView a; public final TextView b; public final ImageView c; public final ImageView d; public final ImageView e; public ChildViewCache(View view) { this.a = (TextView) view.findViewById(R.id.text1); this.b = (TextView) view.findViewById(R.id.text2); this.c = (ImageView) view.findViewById(R.id.icon1); this.d = (ImageView) view.findViewById(R.id.icon2); this.e = (ImageView) view.findViewById(R$id.edit_query); } } public SuggestionsAdapter(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap weakHashMap) { super(context, searchView.getSuggestionRowLayout(), null, true); this.q = false; this.r = 1; this.t = -1; this.u = -1; this.v = -1; this.x = -1; this.y = -1; this.z = -1; this.l = searchView; this.m = searchableInfo; this.p = searchView.getSuggestionCommitIconResId(); this.n = context; this.o = weakHashMap; } private Drawable d(Cursor cursor) { Drawable b = b(this.m.getSearchActivity()); return b != null ? b : this.d.getPackageManager().getDefaultActivityIcon(); } private Drawable e(Cursor cursor) { int i = this.x; if (i == -1) { return null; } Drawable b = b(cursor.getString(i)); return b != null ? b : d(cursor); } private Drawable f(Cursor cursor) { int i = this.y; if (i == -1) { return null; } return b(cursor.getString(i)); } private void g(Cursor cursor) { Bundle extras = cursor != null ? cursor.getExtras() : null; if (extras == null || extras.getBoolean("in_progress")) { } } public void a(int i) { this.r = i; } @Override // androidx.cursoradapter.widget.ResourceCursorAdapter, androidx.cursoradapter.widget.CursorAdapter public View b(Context context, Cursor cursor, ViewGroup viewGroup) { View b = super.b(context, cursor, viewGroup); b.setTag(new ChildViewCache(b)); ((ImageView) b.findViewById(R$id.edit_query)).setImageResource(this.p); return b; } @Override // androidx.cursoradapter.widget.CursorAdapter, 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 a = a(this.d, this.c, viewGroup); if (a != null) { ((ChildViewCache) a.getTag()).a.setText(e.toString()); } return a; } } @Override // androidx.cursoradapter.widget.CursorAdapter, 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 b = b(this.d, this.c, viewGroup); if (b != null) { ((ChildViewCache) b.getTag()).a.setText(e.toString()); } return b; } } @Override // android.widget.BaseAdapter, android.widget.Adapter public boolean hasStableIds() { return false; } @Override // android.widget.BaseAdapter public void notifyDataSetChanged() { super.notifyDataSetChanged(); g(a()); } @Override // android.widget.BaseAdapter public void notifyDataSetInvalidated() { super.notifyDataSetInvalidated(); g(a()); } @Override // android.view.View.OnClickListener public void onClick(View view) { Object tag = view.getTag(); if (tag instanceof CharSequence) { this.l.a((CharSequence) tag); } } @Override // androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public Cursor a(CharSequence charSequence) { String charSequence2 = charSequence == null ? "" : charSequence.toString(); if (this.l.getVisibility() == 0 && this.l.getWindowVisibility() == 0) { try { Cursor a = a(this.m, charSequence2, 50); if (a != null) { a.getCount(); return a; } } catch (RuntimeException e) { Log.w("SuggestionsAdapter", "Search suggestions query threw an exception.", e); } } return null; } private CharSequence b(CharSequence charSequence) { if (this.s == null) { TypedValue typedValue = new TypedValue(); this.d.getTheme().resolveAttribute(R$attr.textColorSearchUrl, typedValue, true); this.s = this.d.getResources().getColorStateList(typedValue.resourceId); } SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.s, null), 0, charSequence.length(), 33); return spannableString; } @Override // androidx.cursoradapter.widget.CursorAdapter, androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public void a(Cursor cursor) { if (this.q) { 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.t = cursor.getColumnIndex("suggest_text_1"); this.u = cursor.getColumnIndex("suggest_text_2"); this.v = 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 e) { Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e); } } @Override // androidx.cursoradapter.widget.CursorAdapter, androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public CharSequence b(Cursor cursor) { String a; String a2; if (cursor == null) { return null; } String a3 = a(cursor, "suggest_intent_query"); if (a3 != null) { return a3; } if (this.m.shouldRewriteQueryFromData() && (a2 = a(cursor, "suggest_intent_data")) != null) { return a2; } if (!this.m.shouldRewriteQueryFromText() || (a = a(cursor, "suggest_text_1")) == null) { return null; } return a; } private Drawable b(String str) { if (str == null || str.isEmpty() || "0".equals(str)) { return null; } try { int parseInt = Integer.parseInt(str); String str2 = "android.resource://" + this.n.getPackageName() + "/" + parseInt; Drawable a = a(str2); if (a != null) { return a; } Drawable c = ContextCompat.c(this.n, parseInt); a(str2, c); return c; } catch (Resources.NotFoundException unused) { Log.w("SuggestionsAdapter", "Icon resource not found: " + str); return null; } catch (NumberFormatException unused2) { Drawable a2 = a(str); if (a2 != null) { return a2; } Drawable b = b(Uri.parse(str)); a(str, b); return b; } } @Override // androidx.cursoradapter.widget.CursorAdapter public void a(View view, Context context, Cursor cursor) { CharSequence a; ChildViewCache childViewCache = (ChildViewCache) view.getTag(); int i = this.z; int i2 = i != -1 ? cursor.getInt(i) : 0; if (childViewCache.a != null) { a(childViewCache.a, a(cursor, this.t)); } if (childViewCache.b != null) { String a2 = a(cursor, this.v); if (a2 != null) { a = b((CharSequence) a2); } else { a = a(cursor, this.u); } if (TextUtils.isEmpty(a)) { TextView textView = childViewCache.a; if (textView != null) { textView.setSingleLine(false); childViewCache.a.setMaxLines(2); } } else { TextView textView2 = childViewCache.a; if (textView2 != null) { textView2.setSingleLine(true); childViewCache.a.setMaxLines(1); } } a(childViewCache.b, a); } ImageView imageView = childViewCache.c; if (imageView != null) { a(imageView, e(cursor), 4); } ImageView imageView2 = childViewCache.d; if (imageView2 != null) { a(imageView2, f(cursor), 8); } int i3 = this.r; if (i3 != 2 && (i3 != 1 || (i2 & 1) == 0)) { childViewCache.e.setVisibility(8); return; } childViewCache.e.setVisibility(0); childViewCache.e.setTag(childViewCache.a.getText()); childViewCache.e.setOnClickListener(this); } private Drawable b(Uri uri) { try { if ("android.resource".equals(uri.getScheme())) { try { return a(uri); } catch (Resources.NotFoundException unused) { throw new FileNotFoundException("Resource does not exist: " + uri); } } InputStream openInputStream = this.n.getContentResolver().openInputStream(uri); if (openInputStream != null) { try { return Drawable.createFromStream(openInputStream, null); } finally { try { openInputStream.close(); } catch (IOException e) { Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e); } } } 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; } private Drawable b(ComponentName componentName) { String flattenToShortString = componentName.flattenToShortString(); if (this.o.containsKey(flattenToShortString)) { Drawable.ConstantState constantState = this.o.get(flattenToShortString); if (constantState == null) { return null; } return constantState.newDrawable(this.n.getResources()); } Drawable a = a(componentName); this.o.put(flattenToShortString, a != null ? a.getConstantState() : null); return a; } private void a(TextView textView, CharSequence charSequence) { textView.setText(charSequence); if (TextUtils.isEmpty(charSequence)) { textView.setVisibility(8); } else { textView.setVisibility(0); } } 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 Drawable a(String str) { Drawable.ConstantState constantState = this.o.get(str); if (constantState == null) { return null; } return constantState.newDrawable(); } private void a(String str, Drawable drawable) { if (drawable != null) { this.o.put(str, drawable.getConstantState()); } } private Drawable a(ComponentName componentName) { PackageManager packageManager = this.d.getPackageManager(); try { ActivityInfo activityInfo = packageManager.getActivityInfo(componentName, PeripheralType.SERVO); 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 e) { Log.w("SuggestionsAdapter", e.toString()); return null; } } public static String a(Cursor cursor, String str) { return a(cursor, cursor.getColumnIndex(str)); } private static String a(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; } } Drawable a(Uri uri) throws FileNotFoundException { int parseInt; String authority = uri.getAuthority(); if (!TextUtils.isEmpty(authority)) { try { Resources resourcesForApplication = this.d.getPackageManager().getResourcesForApplication(authority); List 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); } Cursor a(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); } String[] strArr2 = strArr; if (i > 0) { fragment.appendQueryParameter("limit", String.valueOf(i)); } return this.d.getContentResolver().query(fragment.build(), null, suggestSelection, strArr2, null); } }