summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/eleven/sectionadapter/SectionAdapter.java
blob: 870382ac12ccc39769676e4ba706d5ede79bb7e7 (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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright (C) 2014 The CyanogenMod 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.cyanogenmod.eleven.sectionadapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cyanogenmod.eleven.R;
import com.cyanogenmod.eleven.ui.MusicHolder;
import com.cyanogenmod.eleven.utils.SectionCreatorUtils.Section;
import com.cyanogenmod.eleven.utils.SectionCreatorUtils.SectionType;
import com.cyanogenmod.eleven.widgets.IPopupMenuCallback;

import java.util.TreeMap;

/**
 * This class wraps an ArrayAdapter that implements BasicAdapter and allows Sections to be inserted
 * into the list.  This wraps the methods for getting the view/indices and returns the section
 * heads and if it is an underlying item it flows it through the underlying adapter
 * @param <TItem> The underlying item that is in the array adapter
 * @param <TArrayAdapter> the arrayadapter that contains TItem and implements BasicAdapter
 */
public class SectionAdapter<TItem,
        TArrayAdapter extends ArrayAdapter<TItem> & SectionAdapter.BasicAdapter & IPopupMenuCallback>
        extends BaseAdapter implements IPopupMenuCallback, IPopupMenuCallback.IListener {
    /**
     * Basic interface that the adapters implement
     */
    public interface BasicAdapter {
        public void unload();
        public void buildCache();
        public void flush();
        public int getItemPosition(long id);
    }

    /**
     * The underlying adapter to wrap
     */
    protected TArrayAdapter mUnderlyingAdapter;

    /**
     * A map of external position to the Section type and Identifier
     */
    protected TreeMap<Integer, Section> mSections;

    protected int mHeaderLayoutId;
    protected boolean mHeaderEnabled;

    protected int mFooterLayoutId;
    protected boolean mFooterEnabled;

    /**
     * Popup menu click listener
     */
    protected IListener mListener;

    /**
     * {@link Context}
     */
    protected final Context mContext;

    /**
     * Creates a SectionAdapter
     * @param context The {@link Context} to use.
     * @param underlyingAdapter the underlying adapter to wrap
     */
    public SectionAdapter(final Activity context, final TArrayAdapter underlyingAdapter) {
        mContext = context;
        mUnderlyingAdapter = underlyingAdapter;
        mUnderlyingAdapter.setPopupMenuClickedListener(this);
        mSections = new TreeMap<Integer, Section>();
        setupHeaderParameters(R.layout.list_header, false);
        // since we have no good default footer, just re-use the header layout
        setupFooterParameters(R.layout.list_header, false);
    }

    /**
     * Gets the underlying array adapter
     * @return the underlying array adapter
     */
    public TArrayAdapter getUnderlyingAdapter() {
        return mUnderlyingAdapter;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (isSection(position)) {
            if (convertView == null) {
                int layoutId = mHeaderLayoutId;
                if (isSectionFooter(position)) {
                    layoutId = mFooterLayoutId;
                }

                convertView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
            }

            TextView title = (TextView)convertView.findViewById(R.id.title);
            title.setText(mSections.get(position).mIdentifier);
        } else {
            convertView = mUnderlyingAdapter.getView(
                    getInternalPosition(position), convertView, parent);

            Object tag = convertView.getTag();
            if (tag instanceof MusicHolder) {
                MusicHolder holder = (MusicHolder)tag;
                View divider = holder.mDivider.get();
                if (divider != null) {
                    // if it is the last item in the list, or it is an item before a section divider
                    // then hide the divider, otherwise show it
                    if (position == getCount() - 1 || isSection(position + 1)) {
                        divider.setVisibility(View.INVISIBLE);
                    } else {
                        divider.setVisibility(View.VISIBLE);
                    }
                }
            }
        }

        return convertView;
    }

    /**
     * Setup the header parameters
     * @param layoutId the layout id used to inflate
     * @param enabled whether clicking is enabled on the header
     */
    public void setupHeaderParameters(int layoutId, boolean enabled) {
        mHeaderLayoutId = layoutId;
        mHeaderEnabled = enabled;
    }

    /**
     * Setup the footer parameters
     * @param layoutId the layout id used to inflate
     * @param enabled whether clicking is enabled on the footer
     */
    public void setupFooterParameters(int layoutId, boolean enabled) {
        mFooterLayoutId = layoutId;
        mFooterEnabled = enabled;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getCount() {
        return mSections.size() + mUnderlyingAdapter.getCount();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getItem(int position) {
        if (isSection(position)) {
            return mSections.get(position);
        }

        return mUnderlyingAdapter.getItem(getInternalPosition(position));
    }

    /**
     * Gets the underlying adapter's item
     * @param position position to query for
     * @return the underlying item or null if a section header is queried
     */
    public TItem getTItem(int position) {
        if (isSection(position)) {
            return null;
        }

        return mUnderlyingAdapter.getItem(getInternalPosition(position));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getItemViewType(int position) {
        if (isSectionHeader(position)) {
            // use the last view type id as the section header
            return getViewTypeCount() - 1;
        } else if (isSectionFooter(position)) {
            // use the last view type id as the section header
            return getViewTypeCount() - 2;
        }

        return mUnderlyingAdapter.getItemViewType(getInternalPosition(position));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getViewTypeCount() {
        // increment view type count by 2 for section headers and section footers
        return mUnderlyingAdapter.getViewTypeCount() + 2;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

        mUnderlyingAdapter.notifyDataSetChanged();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();

        mUnderlyingAdapter.notifyDataSetInvalidated();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isEnabled(int position) {
        if (isSectionHeader(position)) {
            return mHeaderEnabled;
        } else if (isSectionFooter(position)) {
            return mFooterEnabled;
        }

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    /**
     * Determines whether the item at the position is a section header
     * @param position position in the overall lis
     * @return true if a section header
     */
    public boolean isSectionHeader(int position) {
        return mSections.containsKey(position) && mSections.get(position).mType == SectionType.Header;
    }

    /**
     * Determines whether the item at the position is a section footer
     * @param position position in the overall lis
     * @return true if a section footer
     */
    public boolean isSectionFooter(int position) {
        return mSections.containsKey(position) && mSections.get(position).mType == SectionType.Footer;
    }

    /**
     * Determines whether the item at the position is a section of some type
     * @param position position in the overall lis
     * @return true if the item is a section
     */
    public boolean isSection(int position) {
        return mSections.containsKey(position);
    }

    /**
     * Converts the external position to the internal position.  This is needed to determine
     * the position to pass into the underlying adapter
     * @param position external position
     * @return the internal position
     */
    public int getInternalPosition(int position) {
        if (isSection(position)) {
            return -1;
        }

        int countSectionHeaders = 0;

        for (Integer sectionPosition : mSections.keySet()) {
            if (sectionPosition <= position) {
                countSectionHeaders++;
            } else {
                break;
            }
        }

        return position - countSectionHeaders;
    }

    /**
     * Converts the underlaying adapter position to wrapped adapter position
     * @param internalPosition the position of the underlying adapter
     * @return the position of the wrapped adapter
     */
    public int getExternalPosition(int internalPosition) {
        int externalPosition = internalPosition;
        for (Integer sectionPosition : mSections.keySet()) {
            // because the section headers are tracking the 'merged' lists, we need to keep bumping
            // our position for each found section header
            if (sectionPosition <= externalPosition) {
                externalPosition++;
            } else {
                break;
            }
        }

        return externalPosition;
    }

    /**
     * Sets the data on the adapter
     * @param data data to set
     */
    public void setData(SectionListContainer<TItem> data) {
        mUnderlyingAdapter.unload();

        if (data.mSections == null) {
            mSections.clear();
        } else {
            mSections = data.mSections;
        }

        mUnderlyingAdapter.addAll(data.mListResults);

        mUnderlyingAdapter.buildCache();

        notifyDataSetChanged();
    }

    /**
     * unloads the underlying adapter
     */
    public void unload() {
        mSections.clear();
        mUnderlyingAdapter.unload();
        notifyDataSetChanged();
    }

    /**
     * flushes the underlying adapter
     */
    public void flush() {
        mUnderlyingAdapter.flush();
        notifyDataSetChanged();
    }

    public void clear() {
        mSections.clear();
        mUnderlyingAdapter.clear();
        mSections.clear();
    }

    /**
     * Gets the item position for the given identifier
     * @param identifier used to identify the object
     * @return item position, or -1 if not found
     */
    public int getItemPosition(long identifier) {
        int internalPosition = mUnderlyingAdapter.getItemPosition(identifier);
        if (internalPosition >= 0) {
            return getExternalPosition(internalPosition);
        }

        return -1;
    }

    @Override
    public void setPopupMenuClickedListener(IListener listener) {
        mListener = listener;
    }

    @Override
    public void onPopupMenuClicked(View v, int position) {
        if (mListener != null) {
            mListener.onPopupMenuClicked(v, getExternalPosition(position));
        }
    }
}