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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* 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.messaging.ui;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
* A general purpose adapter that composes one or more other adapters. It
* appends them in the order they are added.
*/
public class CompositeAdapter extends BaseAdapter {
private static final int INITIAL_CAPACITY = 2;
public static class Partition {
boolean mShowIfEmpty;
boolean mHasHeader;
BaseAdapter mAdapter;
public Partition(final boolean showIfEmpty, final boolean hasHeader,
final BaseAdapter adapter) {
this.mShowIfEmpty = showIfEmpty;
this.mHasHeader = hasHeader;
this.mAdapter = adapter;
}
/**
* True if the directory should be shown even if no contacts are found.
*/
public boolean showIfEmpty() {
return mShowIfEmpty;
}
public boolean hasHeader() {
return mHasHeader;
}
public int getCount() {
int count = mAdapter.getCount();
if (mHasHeader && (count != 0 || mShowIfEmpty)) {
count++;
}
return count;
}
public BaseAdapter getAdapter() {
return mAdapter;
}
public View getHeaderView(final View convertView, final ViewGroup parentView) {
return null;
}
public void close() {
// do nothing in base class.
}
}
private class Observer extends DataSetObserver {
@Override
public void onChanged() {
CompositeAdapter.this.notifyDataSetChanged();
}
@Override
public void onInvalidated() {
CompositeAdapter.this.notifyDataSetInvalidated();
}
};
protected final Context mContext;
private Partition[] mPartitions;
private int mSize = 0;
private int mCount = 0;
private boolean mCacheValid = true;
private final Observer mObserver;
public CompositeAdapter(final Context context) {
mContext = context;
mObserver = new Observer();
mPartitions = new Partition[INITIAL_CAPACITY];
}
public Context getContext() {
return mContext;
}
public void addPartition(final Partition partition) {
if (mSize >= mPartitions.length) {
final int newCapacity = mSize + 2;
final Partition[] newAdapters = new Partition[newCapacity];
System.arraycopy(mPartitions, 0, newAdapters, 0, mSize);
mPartitions = newAdapters;
}
mPartitions[mSize++] = partition;
partition.getAdapter().registerDataSetObserver(mObserver);
invalidate();
notifyDataSetChanged();
}
public void removePartition(final int index) {
final Partition partition = mPartitions[index];
partition.close();
System.arraycopy(mPartitions, index + 1, mPartitions, index,
mSize - index - 1);
mSize--;
partition.getAdapter().unregisterDataSetObserver(mObserver);
invalidate();
notifyDataSetChanged();
}
public void clearPartitions() {
for (int i = 0; i < mSize; i++) {
final Partition partition = mPartitions[i];
partition.close();
partition.getAdapter().unregisterDataSetObserver(mObserver);
}
invalidate();
notifyDataSetChanged();
}
public Partition getPartition(final int index) {
return mPartitions[index];
}
public int getPartitionAtPosition(final int position) {
ensureCacheValid();
int start = 0;
for (int i = 0; i < mSize; i++) {
final int end = start + mPartitions[i].getCount();
if (position >= start && position < end) {
int offset = position - start;
if (mPartitions[i].hasHeader() &&
(mPartitions[i].getCount() > 0 || mPartitions[i].showIfEmpty())) {
offset--;
}
if (offset == -1) {
return -1;
}
return i;
}
start = end;
}
return mSize - 1;
}
public int getPartitionCount() {
return mSize;
}
public void invalidate() {
mCacheValid = false;
}
private void ensureCacheValid() {
if (mCacheValid) {
return;
}
mCount = 0;
for (int i = 0; i < mSize; i++) {
mCount += mPartitions[i].getCount();
}
}
@Override
public int getCount() {
ensureCacheValid();
return mCount;
}
public int getCount(final int index) {
ensureCacheValid();
return mPartitions[index].getCount();
}
@Override
public Object getItem(final int position) {
ensureCacheValid();
int start = 0;
for (int i = 0; i < mSize; i++) {
final int end = start + mPartitions[i].getCount();
if (position >= start && position < end) {
final int offset = position - start;
final Partition partition = mPartitions[i];
if (partition.hasHeader() && offset == 0 &&
(partition.getCount() > 0 || partition.showIfEmpty())) {
// This is the header
return null;
}
return mPartitions[i].getAdapter().getItem(offset);
}
start = end;
}
return null;
}
@Override
public long getItemId(final int position) {
ensureCacheValid();
int start = 0;
for (int i = 0; i < mSize; i++) {
final int end = start + mPartitions[i].getCount();
if (position >= start && position < end) {
final int offset = position - start;
final Partition partition = mPartitions[i];
if (partition.hasHeader() && offset == 0 &&
(partition.getCount() > 0 || partition.showIfEmpty())) {
// Header
return 0;
}
return mPartitions[i].getAdapter().getItemId(offset);
}
start = end;
}
return 0;
}
@Override
public boolean isEnabled(int position) {
ensureCacheValid();
int start = 0;
for (int i = 0; i < mSize; i++) {
final int end = start + mPartitions[i].getCount();
if (position >= start && position < end) {
final int offset = position - start;
final Partition partition = mPartitions[i];
if (partition.hasHeader() && offset == 0 &&
(partition.getCount() > 0 || partition.showIfEmpty())) {
// This is the header
return false;
}
return true;
}
start = end;
}
return true;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parentView) {
ensureCacheValid();
int start = 0;
for (int i = 0; i < mSize; i++) {
final Partition partition = mPartitions[i];
final int end = start + partition.getCount();
if (position >= start && position < end) {
int offset = position - start;
View view;
if (partition.hasHeader() &&
(partition.getCount() > 0 || partition.showIfEmpty())) {
offset = offset - 1;
}
if (offset == -1) {
view = partition.getHeaderView(convertView, parentView);
} else {
view = partition.getAdapter().getView(offset, convertView, parentView);
}
if (view == null) {
throw new NullPointerException("View should not be null, partition: " + i
+ " position: " + offset);
}
return view;
}
start = end;
}
throw new ArrayIndexOutOfBoundsException(position);
}
}
|