blob: 43c24280048268719f9487a76dd791ab73bcc6f6 [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
Brian Attwellbbd22012014-07-16 15:59:10 -070018import android.content.Context;
19import android.database.Cursor;
20import android.view.View;
21import android.view.ViewGroup;
22
Gary Mai0a49afa2016-12-05 15:53:58 -080023import com.android.contacts.R;
24
Brian Attwellbbd22012014-07-16 15:59:10 -070025/**
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 */
32public class HeaderEntryContactListAdapter extends DefaultContactListAdapter {
33
34 private boolean mShowCreateContact;
35
36 public HeaderEntryContactListAdapter(Context context) {
37 super(context);
38 }
39
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 */
47 public void setShowCreateContact(boolean showCreateContact) {
48 mShowCreateContact = showCreateContact;
49 invalidate();
50 }
51
52 @Override
53 public int getCount() {
54 return super.getCount() + getHeaderEntryCount();
55 }
56
57 @Override
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 {
Andrew Lee78cadf52015-03-20 17:57:15 -070065 itemView = (ContactListItemView) convertView;
Brian Attwellbbd22012014-07-16 15:59:10 -070066 }
John Shaobd9ef3c2016-12-15 12:42:03 -080067 itemView.setDrawableResource(R.drawable.quantum_ic_person_add_vd_theme_24);
Brian Attwellbbd22012014-07-16 15:59:10 -070068 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
76 public Object getItem(int position) {
77 return super.getItem(position - getHeaderEntryCount());
78 }
79
80 @Override
Brian Attwella2144632014-08-08 16:26:42 -070081 public boolean isEnabled(int position) {
82 return position < getHeaderEntryCount() || super
83 .isEnabled(position - getHeaderEntryCount());
84 }
85
86 @Override
87 public int getPartitionForPosition(int position) {
88 return super.getPartitionForPosition(position - getHeaderEntryCount());
89 }
90
91 @Override
Brian Attwellbbd22012014-07-16 15:59:10 -070092 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
93 super.bindView(itemView, partition, cursor, position + getHeaderEntryCount());
94 }
95
96 @Override
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
105 public int getViewTypeCount() {
106 // One additional view type, for the header entry.
107 return super.getViewTypeCount() + 1;
108 }
109
110 @Override
111 protected boolean getExtraStartingSection() {
112 return getHeaderEntryCount() > 0;
113 }
114}