HeaderEntryContactListAdapter.java revision bbd220183e9bc6d0b056af3c68fa4a31f5b747f2
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 */ 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 { 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 79 public Object getItem(int position) { 80 return super.getItem(position - getHeaderEntryCount()); 81 } 82 83 @Override 84 protected void bindView(View itemView, int partition, Cursor cursor, int position) { 85 super.bindView(itemView, partition, cursor, position + getHeaderEntryCount()); 86 } 87 88 @Override 89 public int getItemViewType(int position) { 90 if (position == 0 && getHeaderEntryCount() > 0) { 91 return getViewTypeCount() - 1; 92 } 93 return super.getItemViewType(position - getHeaderEntryCount()); 94 } 95 96 @Override 97 public int getViewTypeCount() { 98 // One additional view type, for the header entry. 99 return super.getViewTypeCount() + 1; 100 } 101 102 @Override 103 protected boolean getExtraStartingSection() { 104 return getHeaderEntryCount() > 0; 105 } 106} 107