package androidx.appcompat.widget; 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; import androidx.core.content.ContextCompat; import androidx.cursoradapter.widget.ResourceCursorAdapter; import c.d.b.a.a; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.WeakHashMap; @SuppressLint({"RestrictedAPI"}) public class SuggestionsAdapter extends ResourceCursorAdapter implements View.OnClickListener { private static final boolean DBG = false; public static final int INVALID_INDEX = -1; private static final String LOG_TAG = "SuggestionsAdapter"; private static final int QUERY_LIMIT = 50; public static final int REFINE_ALL = 2; public static final int REFINE_BY_ENTRY = 1; public static final int REFINE_NONE = 0; private boolean mClosed = false; private final int mCommitIconResId; private int mFlagsCol = -1; private int mIconName1Col = -1; private int mIconName2Col = -1; private final WeakHashMap mOutsideDrawablesCache; private final Context mProviderContext; private int mQueryRefinement = 1; private final SearchView mSearchView; private final SearchableInfo mSearchable; private int mText1Col = -1; private int mText2Col = -1; private int mText2UrlCol = -1; private ColorStateList mUrlColor; public static final class ChildViewCache { public final ImageView mIcon1; public final ImageView mIcon2; public final ImageView mIconRefine; public final TextView mText1; public final TextView mText2; public ChildViewCache(View view) { this.mText1 = (TextView) view.findViewById(16908308); this.mText2 = (TextView) view.findViewById(16908309); this.mIcon1 = (ImageView) view.findViewById(16908295); this.mIcon2 = (ImageView) view.findViewById(16908296); this.mIconRefine = (ImageView) view.findViewById(R.id.edit_query); } } public SuggestionsAdapter(Context context, SearchView searchView, SearchableInfo searchableInfo, WeakHashMap weakHashMap) { super(context, searchView.getSuggestionRowLayout(), (Cursor) null, true); this.mSearchView = searchView; this.mSearchable = searchableInfo; this.mCommitIconResId = searchView.getSuggestionCommitIconResId(); this.mProviderContext = context; this.mOutsideDrawablesCache = weakHashMap; } private Drawable checkIconCache(String str) { Drawable.ConstantState constantState = this.mOutsideDrawablesCache.get(str); if (constantState == null) { return null; } return constantState.newDrawable(); } private CharSequence formatUrl(CharSequence charSequence) { if (this.mUrlColor == null) { TypedValue typedValue = new TypedValue(); this.mContext.getTheme().resolveAttribute(R.attr.textColorSearchUrl, typedValue, true); this.mUrlColor = this.mContext.getResources().getColorStateList(typedValue.resourceId); } SpannableString spannableString = new SpannableString(charSequence); spannableString.setSpan(new TextAppearanceSpan(null, 0, 0, this.mUrlColor, null), 0, charSequence.length(), 33); return spannableString; } private Drawable getActivityIcon(ComponentName componentName) { PackageManager packageManager = this.mContext.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; } StringBuilder L = a.L("Invalid icon resource ", iconResource, " for "); L.append(componentName.flattenToShortString()); Log.w("SuggestionsAdapter", L.toString()); return null; } catch (PackageManager.NameNotFoundException e) { Log.w("SuggestionsAdapter", e.toString()); return null; } } private Drawable getActivityIconWithCache(ComponentName componentName) { String flattenToShortString = componentName.flattenToShortString(); Drawable.ConstantState constantState = null; if (this.mOutsideDrawablesCache.containsKey(flattenToShortString)) { Drawable.ConstantState constantState2 = this.mOutsideDrawablesCache.get(flattenToShortString); if (constantState2 == null) { return null; } return constantState2.newDrawable(this.mProviderContext.getResources()); } Drawable activityIcon = getActivityIcon(componentName); if (activityIcon != null) { constantState = activityIcon.getConstantState(); } this.mOutsideDrawablesCache.put(flattenToShortString, constantState); return activityIcon; } public static String getColumnString(Cursor cursor, String str) { return getStringOrNull(cursor, cursor.getColumnIndex(str)); } private Drawable getDefaultIcon1() { Drawable activityIconWithCache = getActivityIconWithCache(this.mSearchable.getSearchActivity()); return activityIconWithCache != null ? activityIconWithCache : this.mContext.getPackageManager().getDefaultActivityIcon(); } private Drawable getDrawable(Uri uri) { try { if ("android.resource".equals(uri.getScheme())) { try { return getDrawableFromResourceUri(uri); } catch (Resources.NotFoundException unused) { throw new FileNotFoundException("Resource does not exist: " + uri); } } else { InputStream openInputStream = this.mProviderContext.getContentResolver().openInputStream(uri); if (openInputStream != null) { try { Drawable createFromStream = Drawable.createFromStream(openInputStream, null); try { } catch (IOException e) { Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e); } return createFromStream; } finally { try { openInputStream.close(); } catch (IOException e2) { Log.e("SuggestionsAdapter", "Error closing icon stream for " + uri, e2); } } } else { throw new FileNotFoundException("Failed to open " + uri); } } } catch (FileNotFoundException e3) { Log.w("SuggestionsAdapter", "Icon not found: " + uri + ", " + e3.getMessage()); return null; } } private Drawable getDrawableFromResourceValue(String str) { if (str == null || str.isEmpty() || "0".equals(str)) { return null; } try { int parseInt = Integer.parseInt(str); String str2 = "android.resource://" + this.mProviderContext.getPackageName() + "/" + parseInt; Drawable checkIconCache = checkIconCache(str2); if (checkIconCache != null) { return checkIconCache; } Drawable drawable = ContextCompat.getDrawable(this.mProviderContext, parseInt); storeInIconCache(str2, drawable); return drawable; } catch (NumberFormatException unused) { Drawable checkIconCache2 = checkIconCache(str); if (checkIconCache2 != null) { return checkIconCache2; } Drawable drawable2 = getDrawable(Uri.parse(str)); storeInIconCache(str, drawable2); return drawable2; } catch (Resources.NotFoundException unused2) { a.g0("Icon resource not found: ", str, "SuggestionsAdapter"); return null; } } private Drawable getIcon1(Cursor cursor) { int i = this.mIconName1Col; if (i == -1) { return null; } Drawable drawableFromResourceValue = getDrawableFromResourceValue(cursor.getString(i)); return drawableFromResourceValue != null ? drawableFromResourceValue : getDefaultIcon1(); } private Drawable getIcon2(Cursor cursor) { int i = this.mIconName2Col; if (i == -1) { return null; } return getDrawableFromResourceValue(cursor.getString(i)); } private static String getStringOrNull(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; } } private void setViewDrawable(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 setViewText(TextView textView, CharSequence charSequence) { textView.setText(charSequence); if (TextUtils.isEmpty(charSequence)) { textView.setVisibility(8); } else { textView.setVisibility(0); } } private void storeInIconCache(String str, Drawable drawable) { if (drawable != null) { this.mOutsideDrawablesCache.put(str, drawable.getConstantState()); } } private void updateSpinnerState(Cursor cursor) { Bundle extras = cursor != null ? cursor.getExtras() : null; if (extras == null || extras.getBoolean("in_progress")) { } } @Override // androidx.cursoradapter.widget.CursorAdapter public void bindView(View view, Context context, Cursor cursor) { ChildViewCache childViewCache = (ChildViewCache) view.getTag(); int i = this.mFlagsCol; int i2 = i != -1 ? cursor.getInt(i) : 0; if (childViewCache.mText1 != null) { setViewText(childViewCache.mText1, getStringOrNull(cursor, this.mText1Col)); } if (childViewCache.mText2 != null) { String stringOrNull = getStringOrNull(cursor, this.mText2UrlCol); CharSequence formatUrl = stringOrNull != null ? formatUrl(stringOrNull) : getStringOrNull(cursor, this.mText2Col); if (TextUtils.isEmpty(formatUrl)) { TextView textView = childViewCache.mText1; if (textView != null) { textView.setSingleLine(false); childViewCache.mText1.setMaxLines(2); } } else { TextView textView2 = childViewCache.mText1; if (textView2 != null) { textView2.setSingleLine(true); childViewCache.mText1.setMaxLines(1); } } setViewText(childViewCache.mText2, formatUrl); } ImageView imageView = childViewCache.mIcon1; if (imageView != null) { setViewDrawable(imageView, getIcon1(cursor), 4); } ImageView imageView2 = childViewCache.mIcon2; if (imageView2 != null) { setViewDrawable(imageView2, getIcon2(cursor), 8); } int i3 = this.mQueryRefinement; if (i3 == 2 || (i3 == 1 && (i2 & 1) != 0)) { childViewCache.mIconRefine.setVisibility(0); childViewCache.mIconRefine.setTag(childViewCache.mText1.getText()); childViewCache.mIconRefine.setOnClickListener(this); return; } childViewCache.mIconRefine.setVisibility(8); } @Override // androidx.cursoradapter.widget.CursorAdapter, androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public void changeCursor(Cursor cursor) { if (this.mClosed) { Log.w("SuggestionsAdapter", "Tried to change cursor after adapter was closed."); if (cursor != null) { cursor.close(); return; } return; } try { super.changeCursor(cursor); if (cursor != null) { this.mText1Col = cursor.getColumnIndex("suggest_text_1"); this.mText2Col = cursor.getColumnIndex("suggest_text_2"); this.mText2UrlCol = cursor.getColumnIndex("suggest_text_2_url"); this.mIconName1Col = cursor.getColumnIndex("suggest_icon_1"); this.mIconName2Col = cursor.getColumnIndex("suggest_icon_2"); this.mFlagsCol = cursor.getColumnIndex("suggest_flags"); } } catch (Exception e) { Log.e("SuggestionsAdapter", "error changing cursor and caching columns", e); } } public void close() { changeCursor(null); this.mClosed = true; } @Override // androidx.cursoradapter.widget.CursorAdapter, androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public CharSequence convertToString(Cursor cursor) { String columnString; String columnString2; if (cursor == null) { return null; } String columnString3 = getColumnString(cursor, "suggest_intent_query"); if (columnString3 != null) { return columnString3; } if (this.mSearchable.shouldRewriteQueryFromData() && (columnString2 = getColumnString(cursor, "suggest_intent_data")) != null) { return columnString2; } if (!this.mSearchable.shouldRewriteQueryFromText() || (columnString = getColumnString(cursor, "suggest_text_1")) == null) { return null; } return columnString; } public Drawable getDrawableFromResourceUri(Uri uri) throws FileNotFoundException { int i; String authority = uri.getAuthority(); if (!TextUtils.isEmpty(authority)) { try { Resources resourcesForApplication = this.mContext.getPackageManager().getResourcesForApplication(authority); List 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("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("More than two path segments: " + uri); } if (i != 0) { return resourcesForApplication.getDrawable(i); } 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); } } else { throw new FileNotFoundException("No authority: " + uri); } } @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 newDropDownView = newDropDownView(this.mContext, this.mCursor, viewGroup); if (newDropDownView != null) { ((ChildViewCache) newDropDownView.getTag()).mText1.setText(e.toString()); } return newDropDownView; } } public int getQueryRefinement() { return this.mQueryRefinement; } public Cursor getSearchManagerSuggestions(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("limit", String.valueOf(i)); } return this.mContext.getContentResolver().query(fragment.build(), null, suggestSelection, strArr, null); } @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 newView = newView(this.mContext, this.mCursor, viewGroup); if (newView != null) { ((ChildViewCache) newView.getTag()).mText1.setText(e.toString()); } return newView; } } @Override // androidx.cursoradapter.widget.CursorAdapter, android.widget.BaseAdapter, android.widget.Adapter public boolean hasStableIds() { return false; } @Override // androidx.cursoradapter.widget.ResourceCursorAdapter, androidx.cursoradapter.widget.CursorAdapter public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { View newView = super.newView(context, cursor, viewGroup); newView.setTag(new ChildViewCache(newView)); ((ImageView) newView.findViewById(R.id.edit_query)).setImageResource(this.mCommitIconResId); return newView; } @Override // android.widget.BaseAdapter public void notifyDataSetChanged() { super.notifyDataSetChanged(); updateSpinnerState(getCursor()); } @Override // android.widget.BaseAdapter public void notifyDataSetInvalidated() { super.notifyDataSetInvalidated(); updateSpinnerState(getCursor()); } @Override // android.view.View.OnClickListener public void onClick(View view) { Object tag = view.getTag(); if (tag instanceof CharSequence) { this.mSearchView.onQueryRefine((CharSequence) tag); } } @Override // androidx.cursoradapter.widget.CursorAdapter, androidx.cursoradapter.widget.CursorFilter.CursorFilterClient public Cursor runQueryOnBackgroundThread(CharSequence charSequence) { String charSequence2 = charSequence == null ? "" : charSequence.toString(); if (this.mSearchView.getVisibility() == 0 && this.mSearchView.getWindowVisibility() == 0) { try { Cursor searchManagerSuggestions = getSearchManagerSuggestions(this.mSearchable, charSequence2, 50); if (searchManagerSuggestions != null) { searchManagerSuggestions.getCount(); return searchManagerSuggestions; } } catch (RuntimeException e) { Log.w("SuggestionsAdapter", "Search suggestions query threw an exception.", e); } } return null; } public void setQueryRefinement(int i) { this.mQueryRefinement = i; } }