From b9ead4a91599ca63e947f74f83b67a58bda64a82 Mon Sep 17 00:00:00 2001
From: Alan Viverette
Date: Wed, 14 Jan 2015 10:43:31 -0800
Subject: Add popup theme for Spinner, use for actionBarPopupTheme default
Also adds methods for setting the context used to inflate drop-down
views in several adapters.
Bug: 17625714
Change-Id: Id267afa4901c1d46ceb3bc3b10fc642cea1799fe
---
core/java/android/widget/ArrayAdapter.java | 71 +++++++++----
core/java/android/widget/CursorAdapter.java | 46 +++++++-
.../java/android/widget/ResourceCursorAdapter.java | 41 +++++++-
core/java/android/widget/SimpleAdapter.java | 55 ++++++++--
core/java/android/widget/Spinner.java | 116 +++++++++++++++++----
core/java/android/widget/SpinnerAdapter.java | 13 +--
core/java/android/widget/SuggestionsAdapter.java | 39 +++++--
7 files changed, 307 insertions(+), 74 deletions(-)
(limited to 'core/java/android/widget')
diff --git a/core/java/android/widget/ArrayAdapter.java b/core/java/android/widget/ArrayAdapter.java
index 97926a7..aff5e29 100644
--- a/core/java/android/widget/ArrayAdapter.java
+++ b/core/java/android/widget/ArrayAdapter.java
@@ -17,7 +17,9 @@
package android.widget;
import android.content.Context;
+import android.content.res.Resources;
import android.util.Log;
+import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -44,7 +46,8 @@ import java.util.List;
* or to have some of data besides toString() results fill the views,
* override {@link #getView(int, View, ViewGroup)} to return the type of view you want.
*/
-public class ArrayAdapter extends BaseAdapter implements Filterable {
+public class ArrayAdapter extends BaseAdapter implements Filterable,
+ Spinner.ThemedSpinnerAdapter {
/**
* Contains the list of objects that represent the data of this ArrayAdapter.
* The content of this list is referred to as "the array" in the documentation.
@@ -93,6 +96,9 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
+ /** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */
+ private LayoutInflater mDropDownInflater;
+
/**
* Constructor
*
@@ -101,7 +107,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* instantiating views.
*/
public ArrayAdapter(Context context, int resource) {
- init(context, resource, 0, new ArrayList());
+ this(context, resource, 0, new ArrayList());
}
/**
@@ -113,7 +119,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* @param textViewResourceId The id of the TextView within the layout resource to be populated
*/
public ArrayAdapter(Context context, int resource, int textViewResourceId) {
- init(context, resource, textViewResourceId, new ArrayList());
+ this(context, resource, textViewResourceId, new ArrayList());
}
/**
@@ -125,7 +131,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, T[] objects) {
- init(context, resource, 0, Arrays.asList(objects));
+ this(context, resource, 0, Arrays.asList(objects));
}
/**
@@ -138,7 +144,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
- init(context, resource, textViewResourceId, Arrays.asList(objects));
+ this(context, resource, textViewResourceId, Arrays.asList(objects));
}
/**
@@ -150,7 +156,7 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, List objects) {
- init(context, resource, 0, objects);
+ this(context, resource, 0, objects);
}
/**
@@ -163,7 +169,11 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, int textViewResourceId, List objects) {
- init(context, resource, textViewResourceId, objects);
+ mContext = context;
+ mInflater = LayoutInflater.from(context);
+ mResource = mDropDownResource = resource;
+ mObjects = objects;
+ mFieldId = textViewResourceId;
}
/**
@@ -305,14 +315,6 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
mNotifyOnChange = notifyOnChange;
}
- private void init(Context context, int resource, int textViewResourceId, List objects) {
- mContext = context;
- mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mResource = mDropDownResource = resource;
- mObjects = objects;
- mFieldId = textViewResourceId;
- }
-
/**
* Returns the context associated with this array adapter. The context is used
* to create views from the resource passed to the constructor.
@@ -359,16 +361,16 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
* {@inheritDoc}
*/
public View getView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mResource);
+ return createViewFromResource(mInflater, position, convertView, parent, mResource);
}
- private View createViewFromResource(int position, View convertView, ViewGroup parent,
- int resource) {
+ private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
+ ViewGroup parent, int resource) {
View view;
TextView text;
if (convertView == null) {
- view = mInflater.inflate(resource, parent, false);
+ view = inflater.inflate(resource, parent, false);
} else {
view = convertView;
}
@@ -408,11 +410,40 @@ public class ArrayAdapter extends BaseAdapter implements Filterable {
}
/**
+ * Sets the {@link Resources.Theme} against which drop-down views are
+ * inflated.
+ *
+ * By default, drop-down views are inflated against the theme of the
+ * {@link Context} passed to the adapter's constructor.
+ *
+ * @param theme the theme against which to inflate drop-down views or
+ * {@code null} to use the theme from the adapter's context
+ * @see #getDropDownView(int, View, ViewGroup)
+ */
+ @Override
+ public void setDropDownViewTheme(Resources.Theme theme) {
+ if (theme == null) {
+ mDropDownInflater = null;
+ } else if (theme == mInflater.getContext().getTheme()) {
+ mDropDownInflater = mInflater;
+ } else {
+ final Context context = new ContextThemeWrapper(mContext, theme);
+ mDropDownInflater = LayoutInflater.from(context);
+ }
+ }
+
+ @Override
+ public Resources.Theme getDropDownViewTheme() {
+ return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme();
+ }
+
+ /**
* {@inheritDoc}
*/
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
- return createViewFromResource(position, convertView, parent, mDropDownResource);
+ final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater;
+ return createViewFromResource(inflater, position, convertView, parent, mDropDownResource);
}
/**
diff --git a/core/java/android/widget/CursorAdapter.java b/core/java/android/widget/CursorAdapter.java
index d4c5be0..8e318fd 100644
--- a/core/java/android/widget/CursorAdapter.java
+++ b/core/java/android/widget/CursorAdapter.java
@@ -17,11 +17,14 @@
package android.widget;
import android.content.Context;
+import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.util.Log;
+import android.view.ContextThemeWrapper;
+import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -35,7 +38,7 @@ import android.view.ViewGroup;
* columns.
*/
public abstract class CursorAdapter extends BaseAdapter implements Filterable,
- CursorFilter.CursorFilterClient {
+ CursorFilter.CursorFilterClient, Spinner.ThemedSpinnerAdapter {
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
@@ -57,6 +60,11 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable,
*/
protected Context mContext;
/**
+ * Context used for {@link #getDropDownView(int, View, ViewGroup)}.
+ * {@hide}
+ */
+ protected Context mDropDownContext;
+ /**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
@@ -185,6 +193,33 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable,
}
/**
+ * Sets the {@link Resources.Theme} against which drop-down views are
+ * inflated.
+ *
+ * By default, drop-down views are inflated against the theme of the
+ * {@link Context} passed to the adapter's constructor.
+ *
+ * @param theme the theme against which to inflate drop-down views or
+ * {@code null} to use the theme from the adapter's context
+ * @see #newDropDownView(Context, Cursor, ViewGroup)
+ */
+ @Override
+ public void setDropDownViewTheme(Resources.Theme theme) {
+ if (theme == null) {
+ mDropDownContext = null;
+ } else if (theme == mContext.getTheme()) {
+ mDropDownContext = mContext;
+ } else {
+ mDropDownContext = new ContextThemeWrapper(mContext, theme);
+ }
+ }
+
+ @Override
+ public Resources.Theme getDropDownViewTheme() {
+ return mDropDownContext == null ? null : mDropDownContext.getTheme();
+ }
+
+ /**
* Returns the cursor.
* @return the cursor.
*/
@@ -258,20 +293,21 @@ public abstract class CursorAdapter extends BaseAdapter implements Filterable,
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (mDataValid) {
+ final Context context = mDropDownContext == null ? mContext : mDropDownContext;
mCursor.moveToPosition(position);
- View v;
+ final View v;
if (convertView == null) {
- v = newDropDownView(mContext, mCursor, parent);
+ v = newDropDownView(context, mCursor, parent);
} else {
v = convertView;
}
- bindView(v, mContext, mCursor);
+ bindView(v, context, mCursor);
return v;
} else {
return null;
}
}
-
+
/**
* Makes a new view to hold the data pointed to by cursor.
* @param context Interface to application's global information
diff --git a/core/java/android/widget/ResourceCursorAdapter.java b/core/java/android/widget/ResourceCursorAdapter.java
index 7341c2c..100f919 100644
--- a/core/java/android/widget/ResourceCursorAdapter.java
+++ b/core/java/android/widget/ResourceCursorAdapter.java
@@ -17,7 +17,9 @@
package android.widget;
import android.content.Context;
+import android.content.res.Resources;
import android.database.Cursor;
+import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
@@ -31,9 +33,10 @@ public abstract class ResourceCursorAdapter extends CursorAdapter {
private int mLayout;
private int mDropDownLayout;
-
+
private LayoutInflater mInflater;
-
+ private LayoutInflater mDropDownInflater;
+
/**
* Constructor the enables auto-requery.
*
@@ -52,8 +55,9 @@ public abstract class ResourceCursorAdapter extends CursorAdapter {
super(context, c);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ mDropDownInflater = mInflater;
}
-
+
/**
* Constructor with default behavior as per
* {@link CursorAdapter#CursorAdapter(Context, Cursor, boolean)}; it is recommended
@@ -74,6 +78,7 @@ public abstract class ResourceCursorAdapter extends CursorAdapter {
super(context, c, autoRequery);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ mDropDownInflater = mInflater;
}
/**
@@ -91,11 +96,37 @@ public abstract class ResourceCursorAdapter extends CursorAdapter {
super(context, c, flags);
mLayout = mDropDownLayout = layout;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ mDropDownInflater = mInflater;
+ }
+
+ /**
+ * Sets the {@link android.content.res.Resources.Theme} against which drop-down views are
+ * inflated.
+ *
+ * By default, drop-down views are inflated against the theme of the
+ * {@link Context} passed to the adapter's constructor.
+ *
+ * @param theme the theme against which to inflate drop-down views or
+ * {@code null} to use the theme from the adapter's context
+ * @see #newDropDownView(Context, Cursor, ViewGroup)
+ */
+ @Override
+ public void setDropDownViewTheme(Resources.Theme theme) {
+ super.setDropDownViewTheme(theme);
+
+ if (theme == null) {
+ mDropDownInflater = null;
+ } else if (theme == mInflater.getContext().getTheme()) {
+ mDropDownInflater = mInflater;
+ } else {
+ final Context context = new ContextThemeWrapper(mContext, theme);
+ mDropDownInflater = LayoutInflater.from(context);
+ }
}
/**
* Inflates view(s) from the specified XML file.
- *
+ *
* @see android.widget.CursorAdapter#newView(android.content.Context,
* android.database.Cursor, ViewGroup)
*/
@@ -106,7 +137,7 @@ public abstract class ResourceCursorAdapter extends CursorAdapter {
@Override
public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
- return mInflater.inflate(mDropDownLayout, parent, false);
+ return mDropDownInflater.inflate(mDropDownLayout, parent, false);
}
/**
diff --git a/core/java/android/widget/SimpleAdapter.java b/core/java/android/widget/SimpleAdapter.java
index 98bcfff..a656712 100644
--- a/core/java/android/widget/SimpleAdapter.java
+++ b/core/java/android/widget/SimpleAdapter.java
@@ -17,6 +17,8 @@
package android.widget;
import android.content.Context;
+import android.content.res.Resources;
+import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
@@ -40,14 +42,14 @@ import java.util.Map;
* If the returned value is false, the following views are then tried in order:
*
* - A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
- *
- TextView. The expected bind value is a string and {@link #setViewText(TextView, String)}
+ *
- TextView. The expected bind value is a string and {@link #setViewText(TextView, String)}
* is invoked.
- *
- ImageView. The expected bind value is a resource id or a string and
- * {@link #setViewImage(ImageView, int)} or {@link #setViewImage(ImageView, String)} is invoked.
+ *
- ImageView. The expected bind value is a resource id or a string and
+ * {@link #setViewImage(ImageView, int)} or {@link #setViewImage(ImageView, String)} is invoked.
*
* If no appropriate binding can be found, an {@link IllegalStateException} is thrown.
*/
-public class SimpleAdapter extends BaseAdapter implements Filterable {
+public class SimpleAdapter extends BaseAdapter implements Filterable, Spinner.ThemedSpinnerAdapter {
private int[] mTo;
private String[] mFrom;
private ViewBinder mViewBinder;
@@ -58,12 +60,15 @@ public class SimpleAdapter extends BaseAdapter implements Filterable {
private int mDropDownResource;
private LayoutInflater mInflater;
+ /** Layout inflater used for {@link #getDropDownView(int, View, ViewGroup)}. */
+ private LayoutInflater mDropDownInflater;
+
private SimpleFilter mFilter;
private ArrayList
+ * define two different views: one that shows the data in the spinner itself
+ * and one that shows the data in the drop down list when the spinner is
+ * pressed.
*/
public interface SpinnerAdapter extends Adapter {
/**
- * Get a {@link android.view.View} that displays in the drop down popup
- * the data at the specified position in the data set.
+ * Gets a {@link android.view.View} that displays in the drop down popup
+ * the data at the specified position in the data set.
*
- * @param position index of the item whose view we want.
- * @param convertView the old view to reuse, if possible. Note: You should
+ * @param position index of the item whose view we want.
+ * @param convertView the old view to reuse, if possible. Note: You should
* check that this view is non-null and of an appropriate type before
* using. If it is not possible to convert this view to display the
* correct data, this method can create a new view.
diff --git a/core/java/android/widget/SuggestionsAdapter.java b/core/java/android/widget/SuggestionsAdapter.java
index 6349347..4323851 100644
--- a/core/java/android/widget/SuggestionsAdapter.java
+++ b/core/java/android/widget/SuggestionsAdapter.java
@@ -145,8 +145,9 @@ class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListene
* copied to the query text field.
*
*
- * @param refine which queries to refine. Possible values are {@link #REFINE_NONE},
- * {@link #REFINE_BY_ENTRY}, and {@link #REFINE_ALL}.
+ * @param refineWhat which queries to refine. Possible values are
+ * {@link #REFINE_NONE}, {@link #REFINE_BY_ENTRY}, and
+ * {@link #REFINE_ALL}.
*/
public void setQueryRefinement(int refineWhat) {
mQueryRefinement = refineWhat;
@@ -327,7 +328,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListene
// First check TEXT_2_URL
CharSequence text2 = getStringOrNull(cursor, mText2UrlCol);
if (text2 != null) {
- text2 = formatUrl(text2);
+ text2 = formatUrl(context, text2);
} else {
text2 = getStringOrNull(cursor, mText2Col);
}
@@ -372,12 +373,12 @@ class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListene
}
}
- private CharSequence formatUrl(CharSequence url) {
+ private CharSequence formatUrl(Context context, CharSequence url) {
if (mUrlColor == null) {
// Lazily get the URL color from the current theme.
TypedValue colorValue = new TypedValue();
- mContext.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true);
- mUrlColor = mContext.getResources().getColorStateList(colorValue.resourceId);
+ context.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true);
+ mUrlColor = context.getResources().getColorStateList(colorValue.resourceId);
}
SpannableString text = new SpannableString(url);
@@ -502,6 +503,30 @@ class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListene
}
/**
+ * This method is overridden purely to provide a bit of protection against
+ * flaky content providers.
+ *
+ * @see android.widget.CursorAdapter#getDropDownView(int, View, ViewGroup)
+ */
+ @Override
+ public View getDropDownView(int position, View convertView, ViewGroup parent) {
+ try {
+ return super.getDropDownView(position, convertView, parent);
+ } catch (RuntimeException e) {
+ Log.w(LOG_TAG, "Search suggestions cursor threw exception.", e);
+ // Put exception string in item title
+ final Context context = mDropDownContext == null ? mContext : mDropDownContext;
+ final View v = newDropDownView(context, mCursor, parent);
+ if (v != null) {
+ final ChildViewCache views = (ChildViewCache) v.getTag();
+ final TextView tv = views.mText1;
+ tv.setText(e.toString());
+ }
+ return v;
+ }
+ }
+
+ /**
* Gets a drawable given a value provided by a suggestion provider.
*
* This value could be just the string value of a resource id
@@ -570,7 +595,7 @@ class SuggestionsAdapter extends ResourceCursorAdapter implements OnClickListene
OpenResourceIdResult r =
mProviderContext.getContentResolver().getResourceId(uri);
try {
- return r.r.getDrawable(r.id, mContext.getTheme());
+ return r.r.getDrawable(r.id, mProviderContext.getTheme());
} catch (Resources.NotFoundException ex) {
throw new FileNotFoundException("Resource does not exist: " + uri);
}
--
cgit v1.1