1a775c2f6886281de846d631cabc1a763a6dabe24mylaccpackage com.xtremelabs.robolectric.shadows;
2a775c2f6886281de846d631cabc1a763a6dabe24mylacc
3a775c2f6886281de846d631cabc1a763a6dabe24mylaccimport android.content.Context;
4853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport android.view.View;
5853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport android.view.ViewGroup;
6a775c2f6886281de846d631cabc1a763a6dabe24mylaccimport android.widget.ArrayAdapter;
73783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirshimport android.widget.Filter;
8e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richardimport android.widget.TextView;
9f540e3687a4ca75ea6b40f577b7807cca30119bePhil Goodwinimport com.xtremelabs.robolectric.Robolectric;
10c2e21b4055efb3648d243cfdd871f0dbc1db4046Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implementation;
11c2e21b4055efb3648d243cfdd871f0dbc1db4046Christian Williams & Phil Goodwinimport com.xtremelabs.robolectric.internal.Implements;
12853a3bf79187b0134658313f2522e2350f9a81aaTom Parkerimport com.xtremelabs.robolectric.res.ResourceLoader;
13a775c2f6886281de846d631cabc1a763a6dabe24mylacc
142645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultzimport java.util.ArrayList;
152645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultzimport java.util.Arrays;
162645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultzimport java.util.List;
172645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultz
182645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
192645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultz
204baf90fcda4a6d4c5cad3e8f857be1f4a070074eRonald Dehuysser@SuppressWarnings( { "UnusedDeclaration" })
21a775c2f6886281de846d631cabc1a763a6dabe24mylacc@Implements(ArrayAdapter.class)
22a775c2f6886281de846d631cabc1a763a6dabe24mylaccpublic class ShadowArrayAdapter<T> extends ShadowBaseAdapter {
23a775c2f6886281de846d631cabc1a763a6dabe24mylacc
243783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    private static final Filter STUB_FILTER = new Filter() {
253783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        @Override
263783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        protected FilterResults performFiltering(CharSequence constraint) {
273783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh            return null;
283783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        }
293783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh
303783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        @Override
313783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        protected void publishResults(CharSequence constraint, FilterResults results) {
323783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        }
333783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    };
343783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh
3572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private Context context;
3672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private List<T> list;
3772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private int resource;
3872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private int textViewResourceId;
393783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    private Filter filter;
402c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    private boolean notifyOnChange = true;
4172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
4210f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken    public int getTextViewResourceId() {
4310f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        return textViewResourceId;
4410f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken    }
452c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen
4610f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken    public int getResourceId() {
4710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        return resource;
4810f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken    }
492c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen
5072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int textViewResourceId) {
5110f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        init(context, textViewResourceId, 0, new ArrayList<T>());
5272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
5372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
5472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int resource, int textViewResourceId) {
5572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        init(context, resource, textViewResourceId, new ArrayList<T>());
5672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
5772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
5872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int textViewResourceId, T[] objects) {
5910f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        init(context, textViewResourceId, 0, Arrays.asList(objects));
6072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
6172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
6272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int resource, int textViewResourceId, T[] objects) {
6372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        init(context, resource, textViewResourceId, Arrays.asList(objects));
6472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
6572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
6672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int textViewResourceId, List<T> objects) {
6710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        init(context, textViewResourceId, 0, objects);
6872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
6972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
7072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void __constructor__(Context context, int resource, int textViewResourceId, List<T> objects) {
7172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        init(context, resource, textViewResourceId, objects);
7272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
7372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
7472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
7572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        this.context = context;
7672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        this.list = objects;
7772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        this.resource = resource;
7872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        this.textViewResourceId = textViewResourceId;
7972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
8072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
8172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
8272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public void add(T object) {
8372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        list.add(object);
842c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        if (notifyOnChange)
852c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen            notifyDataSetChanged();
8672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
8772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
8872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
89200d386eeb9c78cfcc64a27d209e8eee2d646501Tim Labeeuw & Tyler Schultz    public void clear() {
90200d386eeb9c78cfcc64a27d209e8eee2d646501Tim Labeeuw & Tyler Schultz        list.clear();
912c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        if (notifyOnChange)
922c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen            notifyDataSetChanged();
93200d386eeb9c78cfcc64a27d209e8eee2d646501Tim Labeeuw & Tyler Schultz    }
94200d386eeb9c78cfcc64a27d209e8eee2d646501Tim Labeeuw & Tyler Schultz
95200d386eeb9c78cfcc64a27d209e8eee2d646501Tim Labeeuw & Tyler Schultz    @Implementation
96a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard    public void remove(T object) {
97a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard        list.remove(object);
982c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        if (notifyOnChange)
992c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen            notifyDataSetChanged();
100a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard    }
101a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard
102a50847d33e5e00ac0c4390010ec0acf3b2df9b32David Farber & Ryan Richard    @Implementation
10306cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore    public void insert(T object, int index) {
10406cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore        list.add(index, object);
1052c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        if (notifyOnChange)
1062c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen            notifyDataSetChanged();
10706cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore    }
10806cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore
10906cade0625ec80c90a11b5acf130a38f36fa1148David Farber & Joe Moore    @Implementation
11072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public Context getContext() {
11172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return context;
11272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
11372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
11472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
11572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public int getCount() {
11672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return list.size();
11772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
11872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
11972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
12072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public T getItem(int position) {
12172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return list.get(position);
12272ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
12372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
12472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
12572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public int getPosition(T item) {
12672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return list.indexOf(item);
12772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
12872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
12972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
13072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public View getView(int position, View convertView, ViewGroup parent) {
13172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        T item = list.get(position);
1322645db31a493949ed5c39a06b6d305bd8f86587bTyler Schultz        View view;
133e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard
13410f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        if (convertView == null) {
135a875daf8fe5986605569c4955b1b37e60b577be5Stephan Fellhofer            view = getResourceLoader().inflateView(context,resource, null);
13610f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        } else {
13710f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken            view = convertView;
13810f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken        }
13910f0c6198317ebaf7dec221d24234fc67cca425aChris Van Vranken
140e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        TextView text;
141e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        if (textViewResourceId == 0) {
142e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard            text = (TextView) view;
143e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        } else {
144e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard            text = (TextView) view.findViewById(textViewResourceId);
145e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        }
146e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard
147e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        if (item instanceof CharSequence) {
148f2bc42e35b96307f81f8069ada91be57be95e43bMichael Portuesi            Robolectric.shadowOf(text).setText((CharSequence)item);
149e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        } else {
150f2bc42e35b96307f81f8069ada91be57be95e43bMichael Portuesi        	Robolectric.shadowOf(text).setText(item.toString());
151e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        }
152e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard
153e04e87dd1d1b5399264d7be0ed3ae8670dc175a8Ryan Richard        return view;
15472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
15572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
1563783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    @Implementation
1573783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    public Filter getFilter() {
1583783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh        return STUB_FILTER;
1593783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh    }
1603783a296406676c639a954d14ef464a4207de689Joe Moore & Lowell Kirsh
1612c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    @Override
1622c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    @Implementation
1632c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    public void notifyDataSetChanged() {
1642c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        super.notifyDataSetChanged();
1652c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        notifyOnChange = true;
1662c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    }
1672c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen
1682c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    @Implementation
1692c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    public void setNotifyOnChange(boolean notifyOnChange) {
1702c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen        this.notifyOnChange = notifyOnChange;
1712c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen    }
1722c32ef1725ac0f15d67ac0604963ecc70c248a21Jon Boekenoogen
17372ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    private ResourceLoader getResourceLoader() {
17472ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return shadowOf(Robolectric.application).getResourceLoader();
17572ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
17672ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin
17772ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    @Implementation
17872ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    public static ArrayAdapter<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
17972ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
18072ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin        return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
18172ee6c0e205c424f0706051d89e1c7f184bf8269Phil Goodwin    }
1824baf90fcda4a6d4c5cad3e8f857be1f4a070074eRonald Dehuysser}