Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a1009c28 authored by Alan Viverette's avatar Alan Viverette Committed by Android (Google) Code Review
Browse files

Merge "Clean up ArrayAdapter lint warnings and annotations" into nyc-dev

parents 9d5aadd5 9e623b64
Loading
Loading
Loading
Loading
+77 −73
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.ArrayRes;
import android.annotation.IdRes;
import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources;
import android.util.Log;
import android.util.Log;
@@ -61,17 +62,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp


    private final LayoutInflater mInflater;
    private final LayoutInflater mInflater;


    /**
    private final Context mContext;
     * 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.
     */
    private List<T> mObjects;


    /**
    /**
     * The resource indicating what views to inflate to display the content of this
     * The resource indicating what views to inflate to display the content of this
     * array adapter.
     * array adapter.
     */
     */
    private int mResource;
    private final int mResource;


    /**
    /**
     * The resource indicating what views to inflate to display the content of this
     * The resource indicating what views to inflate to display the content of this
@@ -80,7 +77,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
    private int mDropDownResource;
    private int mDropDownResource;


    /**
    /**
     * If the inflated resource is not a TextView, {@link #mFieldId} is used to find
     * 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.
     */
    private List<T> mObjects;

    /**
     * If the inflated resource is not a TextView, {@code mFieldId} is used to find
     * a TextView inside the inflated views hierarchy. This field must contain the
     * a TextView inside the inflated views hierarchy. This field must contain the
     * identifier that matches the one defined in the resource file.
     * identifier that matches the one defined in the resource file.
     */
     */
@@ -92,8 +95,6 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     */
     */
    private boolean mNotifyOnChange = true;
    private boolean mNotifyOnChange = true;


    private Context mContext;

    // A copy of the original mObjects array, initialized from and then used instead as soon as
    // A copy of the original mObjects array, initialized from and then used instead as soon as
    // the mFilter ArrayFilter is used. mObjects will then only contain the filtered values.
    // the mFilter ArrayFilter is used. mObjects will then only contain the filtered values.
    private ArrayList<T> mOriginalValues;
    private ArrayList<T> mOriginalValues;
@@ -109,8 +110,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @param resource The resource ID for a layout file containing a TextView to use when
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     *                 instantiating views.
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource) {
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource) {
        this(context, resource, 0, new ArrayList<T>());
        this(context, resource, 0, new ArrayList<>());
    }
    }


    /**
    /**
@@ -121,8 +122,9 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *                 instantiating views.
     *                 instantiating views.
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId) {
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
        this(context, resource, textViewResourceId, new ArrayList<T>());
            @IdRes int textViewResourceId) {
        this(context, resource, textViewResourceId, new ArrayList<>());
    }
    }


    /**
    /**
@@ -133,7 +135,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *                 instantiating views.
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     * @param objects The objects to represent in the ListView.
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull T[] objects) {
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
        this(context, resource, 0, Arrays.asList(objects));
        this(context, resource, 0, Arrays.asList(objects));
    }
    }


@@ -146,8 +148,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param objects The objects to represent in the ListView.
     * @param objects The objects to represent in the ListView.
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
            @NonNull T[] objects) {
            @IdRes int textViewResourceId, @NonNull T[] objects) {
        this(context, resource, textViewResourceId, Arrays.asList(objects));
        this(context, resource, textViewResourceId, Arrays.asList(objects));
    }
    }


@@ -159,7 +161,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *                 instantiating views.
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     * @param objects The objects to represent in the ListView.
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull List<T> objects) {
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
            @NonNull List<T> objects) {
        this(context, resource, 0, objects);
        this(context, resource, 0, objects);
    }
    }


@@ -172,8 +175,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param objects The objects to represent in the ListView.
     * @param objects The objects to represent in the ListView.
     */
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
    public ArrayAdapter(@NonNull Context context, @LayoutRes int resource,
            @NonNull List<T> objects) {
            @IdRes int textViewResourceId, @NonNull List<T> objects) {
        mContext = context;
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mInflater = LayoutInflater.from(context);
        mResource = mDropDownResource = resource;
        mResource = mDropDownResource = resource;
@@ -186,7 +189,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *
     *
     * @param object The object to add at the end of the array.
     * @param object The object to add at the end of the array.
     */
     */
    public void add(T object) {
    public void add(@Nullable T object) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
            if (mOriginalValues != null) {
                mOriginalValues.add(object);
                mOriginalValues.add(object);
@@ -201,8 +204,17 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * Adds the specified Collection at the end of the array.
     * Adds the specified Collection at the end of the array.
     *
     *
     * @param collection The Collection to add at the end of the array.
     * @param collection The Collection to add at the end of the array.
     */
     * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
    public void addAll(Collection<? extends T> collection) {
     *         is not supported by this list
     * @throws ClassCastException if the class of an element of the specified
     *         collection prevents it from being added to this list
     * @throws NullPointerException if the specified collection contains one
     *         or more null elements and this list does not permit null
     *         elements, or if the specified collection is null
     * @throws IllegalArgumentException if some property of an element of the
     *         specified collection prevents it from being added to this list
     */
    public void addAll(@NonNull Collection<? extends T> collection) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
            if (mOriginalValues != null) {
                mOriginalValues.addAll(collection);
                mOriginalValues.addAll(collection);
@@ -235,7 +247,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @param object The object to insert into the array.
     * @param object The object to insert into the array.
     * @param index The index at which the object must be inserted.
     * @param index The index at which the object must be inserted.
     */
     */
    public void insert(T object, int index) {
    public void insert(@Nullable T object, int index) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
            if (mOriginalValues != null) {
                mOriginalValues.add(index, object);
                mOriginalValues.add(index, object);
@@ -251,7 +263,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *
     *
     * @param object The object to remove.
     * @param object The object to remove.
     */
     */
    public void remove(T object) {
    public void remove(@Nullable T object) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
            if (mOriginalValues != null) {
                mOriginalValues.remove(object);
                mOriginalValues.remove(object);
@@ -282,7 +294,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @param comparator The comparator used to sort the objects contained
     * @param comparator The comparator used to sort the objects contained
     *        in this adapter.
     *        in this adapter.
     */
     */
    public void sort(Comparator<? super T> comparator) {
    public void sort(@NonNull Comparator<? super T> comparator) {
        synchronized (mLock) {
        synchronized (mLock) {
            if (mOriginalValues != null) {
            if (mOriginalValues != null) {
                Collections.sort(mOriginalValues, comparator);
                Collections.sort(mOriginalValues, comparator);
@@ -293,9 +305,6 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
        if (mNotifyOnChange) notifyDataSetChanged();
        if (mNotifyOnChange) notifyDataSetChanged();
    }
    }


    /**
     * {@inheritDoc}
     */
    @Override
    @Override
    public void notifyDataSetChanged() {
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        super.notifyDataSetChanged();
@@ -326,21 +335,17 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *
     *
     * @return The Context associated with this adapter.
     * @return The Context associated with this adapter.
     */
     */
    public Context getContext() {
    public @NonNull Context getContext() {
        return mContext;
        return mContext;
    }
    }


    /**
    @Override
     * {@inheritDoc}
     */
    public int getCount() {
    public int getCount() {
        return mObjects.size();
        return mObjects.size();
    }
    }


    /**
    @Override
     * {@inheritDoc}
    public @Nullable T getItem(int position) {
     */
    public T getItem(int position) {
        return mObjects.get(position);
        return mObjects.get(position);
    }
    }


@@ -351,28 +356,25 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *
     *
     * @return The position of the specified item.
     * @return The position of the specified item.
     */
     */
    public int getPosition(T item) {
    public int getPosition(@Nullable T item) {
        return mObjects.indexOf(item);
        return mObjects.indexOf(item);
    }
    }


    /**
    @Override
     * {@inheritDoc}
     */
    public long getItemId(int position) {
    public long getItemId(int position) {
        return position;
        return position;
    }
    }


    /**
    @Override
     * {@inheritDoc}
    public @NonNull View getView(int position, @Nullable View convertView,
     */
            @NonNull ViewGroup parent) {
    public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(mInflater, position, convertView, parent, mResource);
        return createViewFromResource(mInflater, position, convertView, parent, mResource);
    }
    }


    private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
    private @NonNull View createViewFromResource(@NonNull LayoutInflater inflater, int position,
            ViewGroup parent, int resource) {
            @Nullable View convertView, @NonNull ViewGroup parent, int resource) {
        View view;
        final View view;
        TextView text;
        final TextView text;


        if (convertView == null) {
        if (convertView == null) {
            view = inflater.inflate(resource, parent, false);
            view = inflater.inflate(resource, parent, false);
@@ -387,6 +389,12 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
            } else {
            } else {
                //  Otherwise, find the TextView field within the layout
                //  Otherwise, find the TextView field within the layout
                text = (TextView) view.findViewById(mFieldId);
                text = (TextView) view.findViewById(mFieldId);

                if (text == null) {
                    throw new RuntimeException("Failed to find view with ID "
                            + mContext.getResources().getResourceName(mFieldId)
                            + " in item layout");
                }
            }
            }
        } catch (ClassCastException e) {
        } catch (ClassCastException e) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
@@ -394,7 +402,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
                    "ArrayAdapter requires the resource ID to be a TextView", e);
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        }
        }


        T item = getItem(position);
        final T item = getItem(position);
        if (item instanceof CharSequence) {
        if (item instanceof CharSequence) {
            text.setText((CharSequence) item);
            text.setText((CharSequence) item);
        } else {
        } else {
@@ -426,7 +434,7 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     * @see #getDropDownView(int, View, ViewGroup)
     * @see #getDropDownView(int, View, ViewGroup)
     */
     */
    @Override
    @Override
    public void setDropDownViewTheme(Resources.Theme theme) {
    public void setDropDownViewTheme(@Nullable Resources.Theme theme) {
        if (theme == null) {
        if (theme == null) {
            mDropDownInflater = null;
            mDropDownInflater = null;
        } else if (theme == mInflater.getContext().getTheme()) {
        } else if (theme == mInflater.getContext().getTheme()) {
@@ -438,12 +446,13 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
    }
    }


    @Override
    @Override
    public Resources.Theme getDropDownViewTheme() {
    public @Nullable Resources.Theme getDropDownViewTheme() {
        return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme();
        return mDropDownInflater == null ? null : mDropDownInflater.getContext().getTheme();
    }
    }


    @Override
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
    public View getDropDownView(int position, @Nullable View convertView,
            @NonNull ViewGroup parent) {
        final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater;
        final LayoutInflater inflater = mDropDownInflater == null ? mInflater : mDropDownInflater;
        return createViewFromResource(inflater, position, convertView, parent, mDropDownResource);
        return createViewFromResource(inflater, position, convertView, parent, mDropDownResource);
    }
    }
@@ -458,16 +467,14 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
     *
     *
     * @return An ArrayAdapter<CharSequence>.
     * @return An ArrayAdapter<CharSequence>.
     */
     */
    public static ArrayAdapter<CharSequence> createFromResource(Context context,
    public static @NonNull ArrayAdapter<CharSequence> createFromResource(@NonNull Context context,
            @ArrayRes int textArrayResId, @LayoutRes int textViewResId) {
            @ArrayRes int textArrayResId, @LayoutRes int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        final CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
        return new ArrayAdapter<>(context, textViewResId, strings);
    }
    }


    /**
    @Override
     * {@inheritDoc}
    public @NonNull Filter getFilter() {
     */
    public Filter getFilter() {
        if (mFilter == null) {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
            mFilter = new ArrayFilter();
        }
        }
@@ -482,31 +489,31 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
    private class ArrayFilter extends Filter {
    private class ArrayFilter extends Filter {
        @Override
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            final FilterResults results = new FilterResults();


            if (mOriginalValues == null) {
            if (mOriginalValues == null) {
                synchronized (mLock) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<T>(mObjects);
                    mOriginalValues = new ArrayList<>(mObjects);
                }
                }
            }
            }


            if (prefix == null || prefix.length() == 0) {
            if (prefix == null || prefix.length() == 0) {
                ArrayList<T> list;
                final ArrayList<T> list;
                synchronized (mLock) {
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                    list = new ArrayList<>(mOriginalValues);
                }
                }
                results.values = list;
                results.values = list;
                results.count = list.size();
                results.count = list.size();
            } else {
            } else {
                String prefixString = prefix.toString().toLowerCase();
                final String prefixString = prefix.toString().toLowerCase();


                ArrayList<T> values;
                final ArrayList<T> values;
                synchronized (mLock) {
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                    values = new ArrayList<>(mOriginalValues);
                }
                }


                final int count = values.size();
                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();
                final ArrayList<T> newValues = new ArrayList<>();


                for (int i = 0; i < count; i++) {
                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final T value = values.get(i);
@@ -517,11 +524,8 @@ public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSp
                        newValues.add(value);
                        newValues.add(value);
                    } else {
                    } else {
                        final String[] words = valueText.split(" ");
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;
                        for (String word : words) {

                            if (word.startsWith(prefixString)) {
                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                newValues.add(value);
                                break;
                                break;
                            }
                            }