1 /*
2  * Copyright (C) 2014 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 package com.android.contacts.list;
17 
18 import android.content.Context;
19 import android.database.Cursor;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import com.android.contacts.R;
24 
25 /**
26  * Equivalent to DefaultContactListAdapter, except with an optional header entry that has the same
27  * formatting as the other entries in the list.
28  *
29  * This header entry is hidden when in search mode. Should not be used with lists that contain a
30  * "Me" contact.
31  */
32 public class HeaderEntryContactListAdapter extends DefaultContactListAdapter {
33 
34     private boolean mShowCreateContact;
35 
HeaderEntryContactListAdapter(Context context)36     public HeaderEntryContactListAdapter(Context context) {
37         super(context);
38     }
39 
getHeaderEntryCount()40     private int getHeaderEntryCount() {
41         return isSearchMode() || !mShowCreateContact ? 0 : 1;
42     }
43 
44     /**
45      * Whether the first entry should be "Create contact", when not in search mode.
46      */
setShowCreateContact(boolean showCreateContact)47     public void setShowCreateContact(boolean showCreateContact) {
48         mShowCreateContact = showCreateContact;
49         invalidate();
50     }
51 
52     @Override
getCount()53     public int getCount() {
54         return super.getCount() + getHeaderEntryCount();
55     }
56 
57     @Override
getView(int position, View convertView, ViewGroup parent)58     public View getView(int position, View convertView, ViewGroup parent) {
59         if (position == 0 && getHeaderEntryCount() > 0) {
60             final ContactListItemView itemView;
61             if (convertView == null) {
62                 // Pass the cursor down. Don't worry, it isn't used.
63                 itemView = newView(getContext(), 0, getCursor(0), 0, parent);
64             } else {
65                 itemView = (ContactListItemView) convertView;
66             }
67             itemView.setDrawableResource(R.drawable.quantum_ic_person_add_vd_theme_24);
68             itemView.setDisplayName(getContext().getResources().getString(
69                     R.string.header_entry_contact_list_adapter_header_title));
70             return itemView;
71         }
72         return super.getView(position - getHeaderEntryCount(), convertView, parent);
73     }
74 
75     @Override
getItem(int position)76     public Object getItem(int position) {
77         return super.getItem(position - getHeaderEntryCount());
78     }
79 
80     @Override
isEnabled(int position)81     public boolean isEnabled(int position) {
82         return position < getHeaderEntryCount() || super
83                 .isEnabled(position - getHeaderEntryCount());
84     }
85 
86     @Override
getPartitionForPosition(int position)87     public int getPartitionForPosition(int position) {
88         return super.getPartitionForPosition(position - getHeaderEntryCount());
89     }
90 
91     @Override
bindView(View itemView, int partition, Cursor cursor, int position)92     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
93         super.bindView(itemView, partition, cursor, position + getHeaderEntryCount());
94     }
95 
96     @Override
getItemViewType(int position)97     public int getItemViewType(int position) {
98         if (position == 0 && getHeaderEntryCount() > 0) {
99             return getViewTypeCount() - 1;
100         }
101         return super.getItemViewType(position - getHeaderEntryCount());
102     }
103 
104     @Override
getViewTypeCount()105     public int getViewTypeCount() {
106         // One additional view type, for the header entry.
107         return super.getViewTypeCount() + 1;
108     }
109 
110     @Override
getExtraStartingSection()111     protected boolean getExtraStartingSection() {
112         return getHeaderEntryCount() > 0;
113     }
114 }
115