1bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell/*
2bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Copyright (C) 2014 The Android Open Source Project
3bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell *
4bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Licensed under the Apache License, Version 2.0 (the "License");
5bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * you may not use this file except in compliance with the License.
6bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * You may obtain a copy of the License at
7bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell *
8bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell *      http://www.apache.org/licenses/LICENSE-2.0
9bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell *
10bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Unless required by applicable law or agreed to in writing, software
11bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * distributed under the License is distributed on an "AS IS" BASIS,
12bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * See the License for the specific language governing permissions and
14bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * limitations under the License.
15bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell */
16bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellpackage com.android.contacts.list;
17bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
18bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.content.Context;
19bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.database.Cursor;
20bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.view.View;
21bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.view.ViewGroup;
22bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
230a49afa2ad697307cc04ef4cb86570574fa720f2Gary Maiimport com.android.contacts.R;
240a49afa2ad697307cc04ef4cb86570574fa720f2Gary Mai
25bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell/**
26bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Equivalent to DefaultContactListAdapter, except with an optional header entry that has the same
27bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * formatting as the other entries in the list.
28bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell *
29bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * This header entry is hidden when in search mode. Should not be used with lists that contain a
30bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * "Me" contact.
31bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell */
32bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellpublic class HeaderEntryContactListAdapter extends DefaultContactListAdapter {
33bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
34bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    private boolean mShowCreateContact;
35bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
36bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public HeaderEntryContactListAdapter(Context context) {
37bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        super(context);
38bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
39bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
40bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    private int getHeaderEntryCount() {
41bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return isSearchMode() || !mShowCreateContact ? 0 : 1;
42bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
43bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
44bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    /**
45bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell     * Whether the first entry should be "Create contact", when not in search mode.
46bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell     */
47bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public void setShowCreateContact(boolean showCreateContact) {
48bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        mShowCreateContact = showCreateContact;
49bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        invalidate();
50bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
51bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
52bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
53bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public int getCount() {
54bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return super.getCount() + getHeaderEntryCount();
55bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
56bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
57bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
58bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public View getView(int position, View convertView, ViewGroup parent) {
59bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        if (position == 0 && getHeaderEntryCount() > 0) {
60bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            final ContactListItemView itemView;
61bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            if (convertView == null) {
62bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell                // Pass the cursor down. Don't worry, it isn't used.
63bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell                itemView = newView(getContext(), 0, getCursor(0), 0, parent);
64bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            } else {
6578cadf518675f998b7c1f4a8da22c00a725e66faAndrew Lee                itemView = (ContactListItemView) convertView;
66bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            }
67bd9ef3c60669d71543b3506d4959c1fe4be409dcJohn Shao            itemView.setDrawableResource(R.drawable.quantum_ic_person_add_vd_theme_24);
68bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            itemView.setDisplayName(getContext().getResources().getString(
69bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell                    R.string.header_entry_contact_list_adapter_header_title));
70bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            return itemView;
71bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        }
72bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return super.getView(position - getHeaderEntryCount(), convertView, parent);
73bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
74bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
75bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
76bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public Object getItem(int position) {
77bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return super.getItem(position - getHeaderEntryCount());
78bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
79bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
80bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
81a214463444f570fc693d66ba9870fff68896e606Brian Attwell    public boolean isEnabled(int position) {
82a214463444f570fc693d66ba9870fff68896e606Brian Attwell        return position < getHeaderEntryCount() || super
83a214463444f570fc693d66ba9870fff68896e606Brian Attwell                .isEnabled(position - getHeaderEntryCount());
84a214463444f570fc693d66ba9870fff68896e606Brian Attwell    }
85a214463444f570fc693d66ba9870fff68896e606Brian Attwell
86a214463444f570fc693d66ba9870fff68896e606Brian Attwell    @Override
87a214463444f570fc693d66ba9870fff68896e606Brian Attwell    public int getPartitionForPosition(int position) {
88a214463444f570fc693d66ba9870fff68896e606Brian Attwell        return super.getPartitionForPosition(position - getHeaderEntryCount());
89a214463444f570fc693d66ba9870fff68896e606Brian Attwell    }
90a214463444f570fc693d66ba9870fff68896e606Brian Attwell
91a214463444f570fc693d66ba9870fff68896e606Brian Attwell    @Override
92bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    protected void bindView(View itemView, int partition, Cursor cursor, int position) {
93bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        super.bindView(itemView, partition, cursor, position + getHeaderEntryCount());
94bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
95bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
96bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
97bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public int getItemViewType(int position) {
98bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        if (position == 0 && getHeaderEntryCount() > 0) {
99bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell            return getViewTypeCount() - 1;
100bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        }
101bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return super.getItemViewType(position - getHeaderEntryCount());
102bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
103bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
104bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
105bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    public int getViewTypeCount() {
106bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        // One additional view type, for the header entry.
107bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return super.getViewTypeCount() + 1;
108bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
109bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell
110bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    @Override
111bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    protected boolean getExtraStartingSection() {
112bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell        return getHeaderEntryCount() > 0;
113bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell    }
114bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell}
115