RecipientAlternatesAdapter.java revision 3bb52162b7e842243d4a14e73c15b20dbd1804d7
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 private OnCheckedItemChangedListener mCheckedItemChangedListener; 41 42 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, 43 OnCheckedItemChangedListener listener) { 44 super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION, 45 Email.CONTACT_ID + " =?", new String[] { 46 String.valueOf(contactId) 47 }, null), 0); 48 mLayoutInflater = LayoutInflater.from(context); 49 mLayoutId = viewId; 50 mCurrentId = currentId; 51 mCheckedItemChangedListener = listener; 52 } 53 54 @Override 55 public long getItemId(int position) { 56 Cursor c = getCursor(); 57 if (c.moveToPosition(position)) { 58 c.getLong(EmailQuery.DATA_ID); 59 } 60 return -1; 61 } 62 63 public RecipientEntry getRecipientEntry(int position) { 64 Cursor c = getCursor(); 65 c.moveToPosition(position); 66 return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c 67 .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c 68 .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI)); 69 } 70 71 @Override 72 public View getView(int position, View convertView, ViewGroup parent) { 73 Cursor cursor = getCursor(); 74 cursor.moveToPosition(position); 75 if (convertView == null) { 76 convertView = newView(); 77 } 78 if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) { 79 mCheckedItemPosition = position; 80 if (mCheckedItemChangedListener != null) { 81 mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition); 82 } 83 } 84 bindView(convertView, convertView.getContext(), cursor); 85 return convertView; 86 } 87 88 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine 89 // somehow? 90 @Override 91 public void bindView(View view, Context context, Cursor cursor) { 92 int position = cursor.getPosition(); 93 94 TextView display = (TextView) view.findViewById(android.R.id.text1); 95 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); 96 RecipientEntry entry = getRecipientEntry(position); 97 if (position == 0) { 98 display.setText(cursor.getString(EmailQuery.NAME)); 99 display.setVisibility(View.VISIBLE); 100 // TODO: see if this needs to be done outside the main thread 101 // as it may be too slow to get immediately. 102 imageView.setImageURI(entry.getPhotoThumbnailUri()); 103 imageView.setVisibility(View.VISIBLE); 104 } else { 105 display.setVisibility(View.GONE); 106 imageView.setVisibility(View.GONE); 107 } 108 TextView destination = (TextView) view.findViewById(android.R.id.text2); 109 destination.setText(cursor.getString(EmailQuery.ADDRESS)); 110 } 111 112 @Override 113 public View newView(Context context, Cursor cursor, ViewGroup parent) { 114 return newView(); 115 } 116 117 private View newView() { 118 return mLayoutInflater.inflate(mLayoutId, null); 119 } 120 121 /*package*/ static interface OnCheckedItemChangedListener { 122 public void onCheckedItemChanged(int position); 123 } 124} 125