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;
18d825ff3727407a154a35e20b3a9adc79e57879b9Dake Guimport java.util.Collections;
19d825ff3727407a154a35e20b3a9adc79e57879b9Dake Guimport java.util.List;
2020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
2120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn/**
22a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout * An {@link ObjectAdapter} implemented with an {@link ArrayList}.
2320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn */
2420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbournpublic class ArrayObjectAdapter extends ObjectAdapter {
2520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
2620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    private ArrayList<Object> mItems = new ArrayList<Object>();
2720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
28beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
29a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * Constructs an adapter with the given {@link PresenterSelector}.
30beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
3120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter(PresenterSelector presenterSelector) {
3220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super(presenterSelector);
3320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
3420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
35beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
36a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * Constructs an adapter that uses the given {@link Presenter} for all items.
37beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
3820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter(Presenter presenter) {
3920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super(presenter);
4020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
4120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
42beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn    /**
43a00bada00bff4a58436a39472ab14ccb7a8f619dCraig Stout     * Constructs an adapter.
44beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     */
4520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public ArrayObjectAdapter() {
4620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        super();
4720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
4820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
4920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    @Override
5020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public int size() {
5120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return mItems.size();
5220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
5320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
5420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    @Override
5520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public Object get(int index) {
5620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return mItems.get(index);
5720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
5820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
5920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
60beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Returns the index for the first occurrence of item in the adapter, or -1 if
61beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * not found.
62beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *
63beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item  The item to find in the list.
64beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @return Index of the first occurrence of the item in the adapter, or -1
65beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *         if not found.
66a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     */
67a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    public int indexOf(Object item) {
68a7ba44025ec38541f429a830763ff799b5e5267eDake Gu        return mItems.indexOf(item);
69a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    }
70a7ba44025ec38541f429a830763ff799b5e5267eDake Gu
71a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    /**
72beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Notify that the content of a range of items changed. Note that this is
73beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * not same as items being added or removed.
74beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     *
75a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     * @param positionStart The position of first item that has changed.
76beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param itemCount The count of how many items have changed.
77a7ba44025ec38541f429a830763ff799b5e5267eDake Gu     */
78a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    public void notifyArrayItemRangeChanged(int positionStart, int itemCount) {
79a7ba44025ec38541f429a830763ff799b5e5267eDake Gu        notifyItemRangeChanged(positionStart, itemCount);
80a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    }
81a7ba44025ec38541f429a830763ff799b5e5267eDake Gu
82a7ba44025ec38541f429a830763ff799b5e5267eDake Gu    /**
83beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Adds an item to the end of the adapter.
8420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
85beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to add to the end of the adapter.
8620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
8720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void add(Object item) {
8820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        add(mItems.size(), item);
8920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
9020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
9120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
92beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Inserts an item into this adapter at the specified index.
93eb66dab544c4c1eabe4d469b7cea348d4b01e664Craig Stout     * If the index is >= {@link #size} an exception will be thrown.
9420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
9520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param index The index at which the item should be inserted.
96beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to insert into the adapter.
9720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
9820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void add(int index, Object item) {
9920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.add(index, item);
10020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeInserted(index, 1);
10120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
10220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
10320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
104beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Adds the objects in the given collection to the adapter, starting at the
105eb66dab544c4c1eabe4d469b7cea348d4b01e664Craig Stout     * given index.  If the index is >= {@link #size} an exception will be thrown.
10620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
10720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param index The index at which the items should be inserted.
10820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param items A {@link Collection} of items to insert.
10920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
110a83005b70853ea52c5d98910762344de16b850a8Tim Kilbourn    public void addAll(int index, Collection items) {
11120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemsCount = items.size();
1129a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemsCount == 0) {
1139a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return;
1149a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
11520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.addAll(index, items);
11620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeInserted(index, itemsCount);
11720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
11820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
11920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
120beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes the first occurrence of the given item from the adapter.
12120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
122beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @param item The item to remove from the adapter.
123beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * @return True if the item was found and thus removed from the adapter.
12420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
12520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public boolean remove(Object item) {
12620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int index = mItems.indexOf(item);
12720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        if (index >= 0) {
12820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            mItems.remove(index);
12920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            notifyItemRangeRemoved(index, 1);
13020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        }
13120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return index >= 0;
13220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
13320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
13420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
1359a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * Replaces item at position with a new item and calls notifyItemRangeChanged()
1369a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * at the given position.  Note that this method does not compare new item to
1379a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * existing item.
1389a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * @param position  The index of item to replace.
1399a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     * @param item      The new item to be placed at given position.
1409a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu     */
1419a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    public void replace(int position, Object item) {
1429a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        mItems.set(position, item);
1439a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        notifyItemRangeChanged(position, 1);
1449a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    }
1459a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu
1469a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu    /**
147beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes a range of items from the adapter. The range is specified by giving
14820c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * the starting position and the number of elements to remove.
14920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     *
15020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param position The index of the first item to remove.
15120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @param count The number of items to remove.
15220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     * @return The number of items removed.
15320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
15420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public int removeItems(int position, int count) {
15520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemsToRemove = Math.min(count, mItems.size() - position);
1569a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemsToRemove <= 0) {
1579a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return 0;
1589a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
15920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
16020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        for (int i = 0; i < itemsToRemove; i++) {
16120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn            mItems.remove(position);
16220c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        }
16320c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeRemoved(position, itemsToRemove);
16420c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        return itemsToRemove;
16520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
16620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn
16720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    /**
168beeaa973d1b5bd79ee8ae798141231d9a315eba7Tim Kilbourn     * Removes all items from this adapter, leaving it empty.
16920c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn     */
17020c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    public void clear() {
17120c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        int itemCount = mItems.size();
1729a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        if (itemCount == 0) {
1739a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu            return;
1749a934fa3710ecb3fb2eb67e33a611563a144186dDake Gu        }
17520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        mItems.clear();
17620c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn        notifyItemRangeRemoved(0, itemCount);
17720c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn    }
178d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu
179d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu    /**
180d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu     * Gets a read-only view of the list of object of this ArrayObjectAdapter.
181d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu     */
182d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu    public <E> List<E> unmodifiableList() {
183d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu        return Collections.unmodifiableList((List<E>) mItems);
184d825ff3727407a154a35e20b3a9adc79e57879b9Dake Gu    }
18520c094c196271089a7119a965b6a99786ea9ed36Tim Kilbourn}
186