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