RecipientAlternatesAdapter.java revision 156467329e276c9bc90945bea916ce3ac4849574
1/* 2 * Copyright (C) 2011 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 17package com.android.ex.chips; 18 19import com.android.ex.chips.BaseRecipientAdapter.EmailQuery; 20 21import android.content.Context; 22import android.database.Cursor; 23import android.provider.ContactsContract.CommonDataKinds.Email; 24import android.view.LayoutInflater; 25import android.view.View; 26import android.view.ViewGroup; 27import android.widget.CursorAdapter; 28import android.widget.ImageView; 29import android.widget.TextView; 30 31public class RecipientAlternatesAdapter extends CursorAdapter { 32 private final LayoutInflater mLayoutInflater; 33 34 private final int mLayoutId; 35 36 private final long mCurrentId; 37 38 private int mCheckedItemPosition = -1; 39 40 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId) { 41 super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION, 42 Email.CONTACT_ID + " =?", new String[] { 43 String.valueOf(contactId) 44 }, null), 0); 45 mLayoutInflater = LayoutInflater.from(context); 46 mLayoutId = viewId; 47 mCurrentId = currentId; 48 } 49 50 @Override 51 public long getItemId(int position) { 52 Cursor c = getCursor(); 53 c.moveToPosition(position); 54 return c.getLong(EmailQuery.DATA_ID); 55 } 56 57 public RecipientEntry getRecipientEntry(int position) { 58 Cursor c = getCursor(); 59 c.moveToPosition(position); 60 return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c 61 .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c 62 .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI)); 63 } 64 65 @Override 66 public View getView(int position, View convertView, ViewGroup parent) { 67 Cursor cursor = getCursor(); 68 cursor.moveToPosition(position); 69 if (convertView == null) { 70 convertView = newView(); 71 } 72 if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) { 73 mCheckedItemPosition = position; 74 } 75 bindView(convertView, convertView.getContext(), cursor); 76 return convertView; 77 } 78 79 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine 80 // somehow? 81 @Override 82 public void bindView(View view, Context context, Cursor cursor) { 83 int position = cursor.getPosition(); 84 85 TextView display = (TextView) view.findViewById(android.R.id.text1); 86 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); 87 RecipientEntry entry = getRecipientEntry(position); 88 if (position == 0) { 89 display.setText(cursor.getString(EmailQuery.NAME)); 90 display.setVisibility(View.VISIBLE); 91 // TODO: see if this needs to be done outside the main thread 92 // as it may be too slow to get immediately. 93 imageView.setImageURI(entry.getPhotoThumbnailUri()); 94 imageView.setVisibility(View.VISIBLE); 95 } else { 96 display.setVisibility(View.GONE); 97 imageView.setVisibility(View.GONE); 98 } 99 TextView destination = (TextView) view.findViewById(android.R.id.text2); 100 destination.setText(cursor.getString(EmailQuery.ADDRESS)); 101 } 102 103 @Override 104 public View newView(Context context, Cursor cursor, ViewGroup parent) { 105 return newView(); 106 } 107 108 private View newView() { 109 return mLayoutInflater.inflate(mLayoutId, null); 110 } 111 112 /** 113 * Get the position of the item that should be checked. 114 */ 115 public int getCheckedItemPosition() { 116 return mCheckedItemPosition; 117 } 118} 119