blob: 9822b79b13a0db7ca1208f5d7f85e21a074f00ad [file] [log] [blame]
Brian Attwellbbd22012014-07-16 15:59:10 -07001/*
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 */
16package com.android.contacts.list;
17
18import com.android.contacts.R;
19import com.android.contacts.common.list.ContactListItemView;
20import com.android.contacts.common.list.DefaultContactListAdapter;
21
22import android.content.Context;
23import android.database.Cursor;
24import android.view.View;
25import 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 */
34public class HeaderEntryContactListAdapter extends DefaultContactListAdapter {
35
36 private boolean mShowCreateContact;
37
38 public HeaderEntryContactListAdapter(Context context) {
39 super(context);
40 }
41
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 */
49 public void setShowCreateContact(boolean showCreateContact) {
50 mShowCreateContact = showCreateContact;
51 invalidate();
52 }
53
54 @Override
55 public int getCount() {
56 return super.getCount() + getHeaderEntryCount();
57 }
58
59 @Override
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 {
Andrew Lee78cadf52015-03-20 17:57:15 -070067 itemView = (ContactListItemView) convertView;
Brian Attwellbbd22012014-07-16 15:59:10 -070068 }
Andrew Lee78cadf52015-03-20 17:57:15 -070069 itemView.setDrawableResource(R.drawable.ic_search_add_contact);
Brian Attwellbbd22012014-07-16 15:59:10 -070070 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
78 public Object getItem(int position) {
79 return super.getItem(position - getHeaderEntryCount());
80 }
81
82 @Override
Brian Attwella2144632014-08-08 16:26:42 -070083 public boolean isEnabled(int position) {
84 return position < getHeaderEntryCount() || super
85 .isEnabled(position - getHeaderEntryCount());
86 }
87
88 @Override
89 public int getPartitionForPosition(int position) {
90 return super.getPartitionForPosition(position - getHeaderEntryCount());
91 }
92
93 @Override
Brian Attwellbbd22012014-07-16 15:59:10 -070094 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
95 super.bindView(itemView, partition, cursor, position + getHeaderEntryCount());
96 }
97
98 @Override
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
107 public int getViewTypeCount() {
108 // One additional view type, for the header entry.
109 return super.getViewTypeCount() + 1;
110 }
111
112 @Override
113 protected boolean getExtraStartingSection() {
114 return getHeaderEntryCount() > 0;
115 }
116}