1bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell/* 2bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Copyright (C) 2014 The Android Open Source Project 3bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * 4bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Licensed under the Apache License, Version 2.0 (the "License"); 5bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * you may not use this file except in compliance with the License. 6bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * You may obtain a copy of the License at 7bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * 8bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * http://www.apache.org/licenses/LICENSE-2.0 9bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * 10bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Unless required by applicable law or agreed to in writing, software 11bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * distributed under the License is distributed on an "AS IS" BASIS, 12bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * See the License for the specific language governing permissions and 14bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * limitations under the License. 15bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell */ 16bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellpackage com.android.contacts.list; 17bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 18bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport com.android.contacts.R; 19bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport com.android.contacts.common.list.ContactListItemView; 20bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport com.android.contacts.common.list.DefaultContactListAdapter; 21bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 22bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.content.Context; 23bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.database.Cursor; 24bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.view.View; 25bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellimport android.view.ViewGroup; 26bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 27bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell/** 28bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Equivalent to DefaultContactListAdapter, except with an optional header entry that has the same 29bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * formatting as the other entries in the list. 30bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * 31bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * This header entry is hidden when in search mode. Should not be used with lists that contain a 32bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * "Me" contact. 33bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell */ 34bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwellpublic class HeaderEntryContactListAdapter extends DefaultContactListAdapter { 35bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 36bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell private boolean mShowCreateContact; 37bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 38bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public HeaderEntryContactListAdapter(Context context) { 39bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell super(context); 40bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 41bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 42bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell private int getHeaderEntryCount() { 43bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return isSearchMode() || !mShowCreateContact ? 0 : 1; 44bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 45bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 46bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell /** 47bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell * Whether the first entry should be "Create contact", when not in search mode. 48bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell */ 49bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public void setShowCreateContact(boolean showCreateContact) { 50bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell mShowCreateContact = showCreateContact; 51bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell invalidate(); 52bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 53bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 54bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 55bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public int getCount() { 56bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return super.getCount() + getHeaderEntryCount(); 57bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 58bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 59bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 60bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public View getView(int position, View convertView, ViewGroup parent) { 61bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell if (position == 0 && getHeaderEntryCount() > 0) { 62bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell final ContactListItemView itemView; 63bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell if (convertView == null) { 64bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell // Pass the cursor down. Don't worry, it isn't used. 65bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell itemView = newView(getContext(), 0, getCursor(0), 0, parent); 66bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } else { 67bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell itemView = (ContactListItemView ) convertView; 68bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 69bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell itemView.setDrawableResource(R.drawable.search_shortcut_background, 70bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell R.drawable.ic_search_add_contact); 71bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell itemView.setDisplayName(getContext().getResources().getString( 72bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell R.string.header_entry_contact_list_adapter_header_title)); 73bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return itemView; 74bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 75bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return super.getView(position - getHeaderEntryCount(), convertView, parent); 76bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 77bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 78bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 79bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public Object getItem(int position) { 80bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return super.getItem(position - getHeaderEntryCount()); 81bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 82bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 83bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 84a214463444f570fc693d66ba9870fff68896e606Brian Attwell public boolean isEnabled(int position) { 85a214463444f570fc693d66ba9870fff68896e606Brian Attwell return position < getHeaderEntryCount() || super 86a214463444f570fc693d66ba9870fff68896e606Brian Attwell .isEnabled(position - getHeaderEntryCount()); 87a214463444f570fc693d66ba9870fff68896e606Brian Attwell } 88a214463444f570fc693d66ba9870fff68896e606Brian Attwell 89a214463444f570fc693d66ba9870fff68896e606Brian Attwell @Override 90a214463444f570fc693d66ba9870fff68896e606Brian Attwell public int getPartitionForPosition(int position) { 91a214463444f570fc693d66ba9870fff68896e606Brian Attwell return super.getPartitionForPosition(position - getHeaderEntryCount()); 92a214463444f570fc693d66ba9870fff68896e606Brian Attwell } 93a214463444f570fc693d66ba9870fff68896e606Brian Attwell 94a214463444f570fc693d66ba9870fff68896e606Brian Attwell @Override 95bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell protected void bindView(View itemView, int partition, Cursor cursor, int position) { 96bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell super.bindView(itemView, partition, cursor, position + getHeaderEntryCount()); 97bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 98bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 99bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 100bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public int getItemViewType(int position) { 101bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell if (position == 0 && getHeaderEntryCount() > 0) { 102bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return getViewTypeCount() - 1; 103bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 104bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return super.getItemViewType(position - getHeaderEntryCount()); 105bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 106bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 107bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 108bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell public int getViewTypeCount() { 109bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell // One additional view type, for the header entry. 110bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return super.getViewTypeCount() + 1; 111bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 112bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell 113bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell @Override 114bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell protected boolean getExtraStartingSection() { 115bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell return getHeaderEntryCount() > 0; 116bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell } 117bbd220183e9bc6d0b056af3c68fa4a31f5b747f2Brian Attwell} 118