summaryrefslogtreecommitdiff
path: root/library/main/src/com/android/setupwizardlib/items/ItemGroup.java
blob: 3e500bb0bf7edb2ca005aae19c309f0b1a6e80fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.setupwizardlib.items;

import android.content.Context;
import android.util.AttributeSet;
import android.util.SparseIntArray;

import java.util.ArrayList;
import java.util.List;

public class ItemGroup extends AbstractItemHierarchy implements ItemHierarchy.Observer {

    /* static section */

    /**
     * Binary search for the closest value that's smaller than or equal to {@code value}, and
     * return the corresponding key.
     */
    private static int binarySearch(SparseIntArray array, int value) {
        final int size = array.size();
        int lo = 0;
        int hi = size - 1;

        while (lo <= hi) {
            final int mid = (lo + hi) >>> 1;
            final int midVal = array.valueAt(mid);

            if (midVal < value) {
                lo = mid + 1;
            } else if (midVal > value) {
                hi = mid - 1;
            } else {
                return array.keyAt(mid);  // value found
            }
        }
        // Value not found. Return the last item before our search range, which is the closest
        // value smaller than the value we are looking for.
        return array.keyAt(lo - 1);
    }

    /* non-static section */

    private List<ItemHierarchy> mChildren = new ArrayList<>();

    /**
     * A mapping from the index of an item hierarchy in mChildren, to the first position in which
     * the corresponding child hierarchy represents. For example:
     *
     *   ItemHierarchy                 Item               Item Position
     *       Index
     * 
     *         0            [         Wi-Fi AP 1       ]        0
     *                      |         Wi-Fi AP 2       |        1
     *                      |         Wi-Fi AP 3       |        2
     *                      |         Wi-Fi AP 4       |        3
     *                      [         Wi-Fi AP 5       ]        4
     * 
     *         1            [  <Empty Item Hierarchy>  ]
     * 
     *         2            [     Use cellular data    ]        5
     * 
     *         3            [       Don't connect      ]        6
     * 
     * For this example of Wi-Fi screen, the following mapping will be produced:
     *     [ 0 -> 0 | 2 -> 5 | 3 -> 6 ]
     * 
     * Also note how ItemHierarchy index 1 is not present in the map, because it is empty.
     *
     * ItemGroup uses this map to look for which ItemHierarchy an item at a given position belongs
     * to.
     */
    private SparseIntArray mHierarchyStart = new SparseIntArray();

    private int mCount = 0;
    private boolean mDirty = false;

    public ItemGroup() {
        super();
    }

    public ItemGroup(Context context, AttributeSet attrs) {
        // Constructor for XML inflation
        super(context, attrs);
    }

    /**
     * Add a child hierarchy to this item group.
     */
    public void addChild(ItemHierarchy child) {
        mChildren.add(child);
        child.registerObserver(this);
        onHierarchyChanged();
    }

    /**
     * Remove a previously added child from this item group.
     *
     * @return True if there is a match for the child and it is removed. False if the child could
     *         not be found in our list of child hierarchies.
     */
    public boolean removeChild(ItemHierarchy child) {
        if (mChildren.remove(child)) {
            child.unregisterObserver(this);
            onHierarchyChanged();
            return true;
        }
        return false;
    }

    /**
     * Remove all children from this hierarchy.
     */
    public void clear() {
        if (mChildren.size() == 0) {
            return;
        }

        for (ItemHierarchy item : mChildren) {
            item.unregisterObserver(this);
        }
        mChildren.clear();
        onHierarchyChanged();
    }

    @Override
    public int getCount() {
        updateDataIfNeeded();
        return mCount;
    }

    @Override
    public IItem getItemAt(int position) {
        int itemIndex = getItemIndex(position);
        ItemHierarchy item = mChildren.get(itemIndex);
        int subpos = position - mHierarchyStart.get(itemIndex);
        return item.getItemAt(subpos);
    }

    @Override
    public void onChanged(ItemHierarchy hierarchy) {
        // Need to set dirty, because our children may have gotten more items.
        mDirty = true;
        notifyChanged();
    }

    private void onHierarchyChanged() {
        onChanged(null);
    }

    @Override
    public ItemHierarchy findItemById(int id) {
        if (id == getId()) {
            return this;
        }
        for (ItemHierarchy child : mChildren) {
            ItemHierarchy childFindItem = child.findItemById(id);
            if (childFindItem != null) {
                return childFindItem;
            }
        }
        return null;
    }

    /**
     * If dirty, this method will recalculate the number of items and mHierarchyStart.
     */
    private void updateDataIfNeeded() {
        if (mDirty) {
            mCount = 0;
            mHierarchyStart.clear();
            for (int itemIndex = 0; itemIndex < mChildren.size(); itemIndex++) {
                ItemHierarchy item = mChildren.get(itemIndex);
                if (item.getCount() > 0) {
                    mHierarchyStart.put(itemIndex, mCount);
                }
                mCount += item.getCount();
            }
            mDirty = false;
        }
    }

    /**
     * Use binary search to locate the item hierarchy a position is contained in.
     *
     * @return Index of the item hierarchy which is responsible for the item at {@code position}.
     */
    private int getItemIndex(int position) {
        updateDataIfNeeded();
        if (position < 0 || position >= mCount) {
            throw new IndexOutOfBoundsException("size=" + mCount + "; index=" + position);
        }
        int result = binarySearch(mHierarchyStart, position);
        if (result < 0) {
            throw new IllegalStateException("Cannot have item start index < 0");
        }
        return result;
    }
}