120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn/*
220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * Copyright (C) 2014 The Android Open Source Project
320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn *
420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * in compliance with the License. You may obtain a copy of the License at
620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn *
720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * http://www.apache.org/licenses/LICENSE-2.0
820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn *
920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * Unless required by applicable law or agreed to in writing, software distributed under the License
1020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * or implied. See the License for the specific language governing permissions and limitations under
1220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn * the License.
1320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn */
1420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbournpackage android.support.v17.leanback.widget;
1520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
1620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbournimport java.util.ArrayList;
1720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbournimport java.util.Collection;
1820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
1920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn/**
20beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn * An ObjectAdapter implemented with an {@link ArrayList}.
2120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn */
2220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbournpublic class ArrayObjectAdapter extends ObjectAdapter {
2320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
2420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    private ArrayList<Object> mItems = new ArrayList<Object>();
2520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
26beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
27beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Construct an adapter with the given {@link PresenterSelector}.
28beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
2920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter(PresenterSelector presenterSelector) {
3020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super(presenterSelector);
3120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
3220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
33beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
34beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Construct an adapter that uses the given {@link Presenter} for all items.
35beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
3620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter(Presenter presenter) {
3720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super(presenter);
3820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
3920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
40beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
41beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Construct an adapter.
42beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
4320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter() {
4420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super();
4520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
4620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
4720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    @Override
4820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public int size() {
4920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return mItems.size();
5020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
5120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
5220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    @Override
5320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public Object get(int index) {
5420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return mItems.get(index);
5520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
5620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
5720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
58beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Returns the index for the first occurrence of item in the adapter, or -1 if
59beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * not found.
60beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *
61beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item  The item to find in the list.
62beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @return Index of the first occurrence of the item in the adapter, or -1
63beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *         if not found.
64a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     */
65a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    public int indexOf(Object item) {
66a7ba44025ec38541f429a830763ff799b5e5267eDake Gu        return mItems.indexOf(item);
67a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    }
68a7ba44025ec38541f429a830763ff799b5e5267eDake Gu
69a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    /**
70beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Notify that the content of a range of items changed. Note that this is
71beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * not same as items being added or removed.
72beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *
73a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     * @param positionStart The position of first item that has changed.
74beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param itemCount The count of how many items have changed.
75a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     */
76a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
77a7ba44025ec38541f429a830763ff799b5e5267eDake Gu        notifyItemRangeChanged(positionStart, itemCount);
78a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    }
79a7ba44025ec38541f429a830763ff799b5e5267eDake Gu
80a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    /**
81beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Adds an item to the end of the adapter.
8220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
83beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to add to the end of the adapter.
8420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
8520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void add(Object item) {
8620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        add(mItems.size(), item);
8720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
8820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
8920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
90beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Inserts an item into this adapter at the specified index.
91eb66dab544c4c1eabe4d469b7cea348d4b01e664Craig Stout     * If the index is >= {@link #size} an exception will be thrown.
9220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
9320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param index The index at which the item should be inserted.
94beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to insert into the adapter.
9520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
9620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void add(int index, Object item) {
9720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.add(index, item);
9820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeInserted(index, 1);
9920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
10020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
10120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
102beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Adds the objects in the given collection to the adapter, starting at the
103eb66dab544c4c1eabe4d469b7cea348d4b01e664Craig Stout     * given index.  If the index is >= {@link #size} an exception will be thrown.
10420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
10520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param index The index at which the items should be inserted.
10620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param items A {@link Collection} of items to insert.
10720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
108a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn    public void addAll(int index, Collection items) {
10920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemsCount = items.size();
1109a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemsCount == 0) {
1119a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return;
1129a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
11320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.addAll(index, items);
11420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeInserted(index, itemsCount);
11520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
11620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
11720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
118beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes the first occurrence of the given item from the adapter.
11920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
120beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to remove from the adapter.
121beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @return True if the item was found and thus removed from the adapter.
12220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
12320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public boolean remove(Object item) {
12420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int index = mItems.indexOf(item);
12520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        if (index >= 0) {
12620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            mItems.remove(index);
12720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            notifyItemRangeRemoved(index, 1);
12820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        }
12920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return index >= 0;
13020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
13120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
13220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
1339a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * Replaces item at position with a new item and calls notifyItemRangeChanged()
1349a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * at the given position.  Note that this method does not compare new item to
1359a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * existing item.
1369a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * @param position  The index of item to replace.
1379a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * @param item      The new item to be placed at given position.
1389a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     */
1399a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    public void replace(int position, Object item) {
1409a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        mItems.set(position, item);
1419a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        notifyItemRangeChanged(position, 1);
1429a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    }
1439a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu
1449a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    /**
145beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes a range of items from the adapter. The range is specified by giving
14620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * the starting position and the number of elements to remove.
14720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
14820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param position The index of the first item to remove.
14920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param count The number of items to remove.
15020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @return The number of items removed.
15120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
15220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public int removeItems(int position, int count) {
15320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemsToRemove = Math.min(count, mItems.size() - position);
1549a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemsToRemove <= 0) {
1559a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return 0;
1569a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
15720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
15820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        for (int i = 0; i < itemsToRemove; i++) {
15920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            mItems.remove(position);
16020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        }
16120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeRemoved(position, itemsToRemove);
16220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return itemsToRemove;
16320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
16420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
16520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
166beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes all items from this adapter, leaving it empty.
16720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
16820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void clear() {
16920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemCount = mItems.size();
1709a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemCount == 0) {
1719a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return;
1729a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
17320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.clear();
17420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeRemoved(0, itemCount);
17520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
17620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn}
177