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.search_shortcut_background,
70                     R.drawable.ic_search_add_contact);
71             itemView.setDisplayName(getContext().getResources().getString(
72                     R.string.header_entry_contact_list_adapter_header_title));
73             return itemView;
74         }
75         return super.getView(position - getHeaderEntryCount(), convertView, parent);
76     }
77 
78     @Override
getItem(int position)79     public Object getItem(int position) {
80         return super.getItem(position - getHeaderEntryCount());
81     }
82 
83     @Override
isEnabled(int position)84     public boolean isEnabled(int position) {
85         return position < getHeaderEntryCount() || super
86                 .isEnabled(position - getHeaderEntryCount());
87     }
88 
89     @Override
getPartitionForPosition(int position)90     public int getPartitionForPosition(int position) {
91         return super.getPartitionForPosition(position - getHeaderEntryCount());
92     }
93 
94     @Override
bindView(View itemView, int partition, Cursor cursor, int position)95     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
96         super.bindView(itemView, partition, cursor, position + getHeaderEntryCount());
97     }
98 
99     @Override
getItemViewType(int position)100     public int getItemViewType(int position) {
101         if (position == 0 && getHeaderEntryCount() > 0) {
102             return getViewTypeCount() - 1;
103         }
104         return super.getItemViewType(position - getHeaderEntryCount());
105     }
106 
107     @Override
getViewTypeCount()108     public int getViewTypeCount() {
109         // One additional view type, for the header entry.
110         return super.getViewTypeCount() + 1;
111     }
112 
113     @Override
getExtraStartingSection()114     protected boolean getExtraStartingSection() {
115         return getHeaderEntryCount() > 0;
116     }
117 }
118