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