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 | |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 36 | private final long mCurrentId; |
| 37 | |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 38 | private int mCheckedItemPosition = -1; |
| 39 | |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame^] | 40 | private OnCheckedItemChangedListener mCheckedItemChangedListener; |
| 41 | |
| 42 | public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, |
| 43 | OnCheckedItemChangedListener listener) { |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 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; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 50 | mCurrentId = currentId; |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame^] | 51 | mCheckedItemChangedListener = listener; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public long getItemId(int position) { |
| 56 | Cursor c = getCursor(); |
| 57 | c.moveToPosition(position); |
| 58 | return c.getLong(EmailQuery.DATA_ID); |
| 59 | } |
| 60 | |
| 61 | public RecipientEntry getRecipientEntry(int position) { |
| 62 | Cursor c = getCursor(); |
| 63 | c.moveToPosition(position); |
| 64 | return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c |
| 65 | .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c |
| 66 | .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI)); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public View getView(int position, View convertView, ViewGroup parent) { |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 71 | Cursor cursor = getCursor(); |
| 72 | cursor.moveToPosition(position); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 73 | if (convertView == null) { |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 74 | convertView = newView(); |
| 75 | } |
| 76 | if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) { |
| 77 | mCheckedItemPosition = position; |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame^] | 78 | if (mCheckedItemChangedListener != null) { |
| 79 | mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition); |
| 80 | } |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 81 | } |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 82 | bindView(convertView, convertView.getContext(), cursor); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 83 | return convertView; |
| 84 | } |
| 85 | |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 86 | // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine |
| 87 | // somehow? |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void bindView(View view, Context context, Cursor cursor) { |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 90 | int position = cursor.getPosition(); |
| 91 | |
| 92 | TextView display = (TextView) view.findViewById(android.R.id.text1); |
| 93 | ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); |
| 94 | RecipientEntry entry = getRecipientEntry(position); |
| 95 | if (position == 0) { |
| 96 | display.setText(cursor.getString(EmailQuery.NAME)); |
| 97 | display.setVisibility(View.VISIBLE); |
| 98 | // TODO: see if this needs to be done outside the main thread |
| 99 | // as it may be too slow to get immediately. |
| 100 | imageView.setImageURI(entry.getPhotoThumbnailUri()); |
| 101 | imageView.setVisibility(View.VISIBLE); |
| 102 | } else { |
| 103 | display.setVisibility(View.GONE); |
| 104 | imageView.setVisibility(View.GONE); |
| 105 | } |
| 106 | TextView destination = (TextView) view.findViewById(android.R.id.text2); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 107 | destination.setText(cursor.getString(EmailQuery.ADDRESS)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 112 | return newView(); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 115 | private View newView() { |
| 116 | return mLayoutInflater.inflate(mLayoutId, null); |
| 117 | } |
| 118 | |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame^] | 119 | /*package*/ static interface OnCheckedItemChangedListener { |
| 120 | public void onCheckedItemChanged(int position); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 121 | } |
| 122 | } |