1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.messaging.ui.contact;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24import android.widget.CursorAdapter;
25import android.widget.SectionIndexer;
26
27import com.android.messaging.R;
28import com.android.messaging.util.Assert;
29
30public class ContactListAdapter extends CursorAdapter implements SectionIndexer {
31    private final ContactListItemView.HostInterface mClivHostInterface;
32    private final boolean mNeedAlphabetHeader;
33    private ContactSectionIndexer mSectionIndexer;
34
35    public ContactListAdapter(final Context context, final Cursor cursor,
36            final ContactListItemView.HostInterface clivHostInterface,
37            final boolean needAlphabetHeader) {
38        super(context, cursor, 0);
39        mClivHostInterface = clivHostInterface;
40        mNeedAlphabetHeader = needAlphabetHeader;
41        mSectionIndexer = new ContactSectionIndexer(cursor);
42    }
43
44    @Override
45    public void bindView(final View view, final Context context, final Cursor cursor) {
46        Assert.isTrue(view instanceof ContactListItemView);
47        final ContactListItemView contactListItemView = (ContactListItemView) view;
48        String alphabetHeader = null;
49        if (mNeedAlphabetHeader) {
50            final int position = cursor.getPosition();
51            final int section = mSectionIndexer.getSectionForPosition(position);
52            // Check if the position is the first in the section.
53            if (mSectionIndexer.getPositionForSection(section) == position) {
54                alphabetHeader = (String) mSectionIndexer.getSections()[section];
55            }
56        }
57        contactListItemView.bind(cursor, mClivHostInterface, mNeedAlphabetHeader, alphabetHeader);
58    }
59
60    @Override
61    public View newView(final Context context, final Cursor cursor, final ViewGroup parent) {
62        final LayoutInflater layoutInflater = LayoutInflater.from(context);
63        return layoutInflater.inflate(R.layout.contact_list_item_view, parent, false);
64    }
65
66    @Override
67    public Cursor swapCursor(final Cursor newCursor) {
68        mSectionIndexer = new ContactSectionIndexer(newCursor);
69        return super.swapCursor(newCursor);
70    }
71
72    @Override
73    public Object[] getSections() {
74        return mSectionIndexer.getSections();
75    }
76
77    @Override
78    public int getPositionForSection(final int sectionIndex) {
79        return mSectionIndexer.getPositionForSection(sectionIndex);
80    }
81
82    @Override
83    public int getSectionForPosition(final int position) {
84        return mSectionIndexer.getSectionForPosition(position);
85    }
86}
87