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