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; |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame^] | 24 | import android.text.util.Rfc822Token; |
| 25 | import android.text.util.Rfc822Tokenizer; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.CursorAdapter; |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 30 | import android.widget.ImageView; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 31 | import android.widget.TextView; |
| 32 | |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame^] | 33 | import java.util.HashMap; |
| 34 | |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 35 | public class RecipientAlternatesAdapter extends CursorAdapter { |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame^] | 36 | static final int MAX_LOOKUPS = 50; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 37 | private final LayoutInflater mLayoutInflater; |
| 38 | |
| 39 | private final int mLayoutId; |
| 40 | |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 41 | private final long mCurrentId; |
| 42 | |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 43 | private int mCheckedItemPosition = -1; |
| 44 | |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame] | 45 | private OnCheckedItemChangedListener mCheckedItemChangedListener; |
| 46 | |
Mindy Pereira | 1852931 | 2011-06-28 11:00:52 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * Get a HashMap of address to RecipientEntry that contains all contact |
| 49 | * information for a contact with the provided address, if one exists. This |
| 50 | * may block the UI, so run it in an async task. |
| 51 | * |
| 52 | * @param context Context. |
| 53 | * @param addresses Array of addresses on which to perform the lookup. |
| 54 | * @return HashMap<String,RecipientEntry> |
| 55 | */ |
| 56 | public static HashMap<String, RecipientEntry> getMatchingRecipients(Context context, |
| 57 | String[] inAddresses) { |
| 58 | int addressesSize = Math.min(MAX_LOOKUPS, inAddresses.length); |
| 59 | String[] addresses = new String[addressesSize]; |
| 60 | StringBuilder bindString = new StringBuilder(); |
| 61 | // Create the "?" string and set up arguments. |
| 62 | for (int i = 0; i < addressesSize; i++) { |
| 63 | Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(inAddresses[i]); |
| 64 | addresses[i] = (tokens.length > 0 ? tokens[0].getAddress() : inAddresses[i]); |
| 65 | bindString.append("?"); |
| 66 | if (i < addressesSize - 1) { |
| 67 | bindString.append(","); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Cursor c = context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION, |
| 72 | Email.ADDRESS + " IN (" + bindString.toString() + ")", addresses, null); |
| 73 | HashMap<String, RecipientEntry> recipientEntries = new HashMap<String, RecipientEntry>(); |
| 74 | if (c != null) { |
| 75 | try { |
| 76 | if (c.moveToFirst()) { |
| 77 | do { |
| 78 | String address = c.getString(EmailQuery.ADDRESS); |
| 79 | recipientEntries.put(address, RecipientEntry.constructTopLevelEntry( |
| 80 | c.getString(EmailQuery.NAME), |
| 81 | c.getString(EmailQuery.ADDRESS), |
| 82 | c.getLong(EmailQuery.CONTACT_ID), |
| 83 | c.getLong(EmailQuery.DATA_ID), |
| 84 | c.getString(EmailQuery.PHOTO_THUMBNAIL_URI))); |
| 85 | } while (c.moveToNext()); |
| 86 | } |
| 87 | } finally { |
| 88 | c.close(); |
| 89 | } |
| 90 | } |
| 91 | return recipientEntries; |
| 92 | } |
| 93 | |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame] | 94 | public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, |
| 95 | OnCheckedItemChangedListener listener) { |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 96 | super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION, |
| 97 | Email.CONTACT_ID + " =?", new String[] { |
| 98 | String.valueOf(contactId) |
| 99 | }, null), 0); |
| 100 | mLayoutInflater = LayoutInflater.from(context); |
| 101 | mLayoutId = viewId; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 102 | mCurrentId = currentId; |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame] | 103 | mCheckedItemChangedListener = listener; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public long getItemId(int position) { |
| 108 | Cursor c = getCursor(); |
Mindy Pereira | 3bb5216 | 2011-07-01 14:52:22 -0700 | [diff] [blame] | 109 | if (c.moveToPosition(position)) { |
| 110 | c.getLong(EmailQuery.DATA_ID); |
| 111 | } |
| 112 | return -1; |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public RecipientEntry getRecipientEntry(int position) { |
| 116 | Cursor c = getCursor(); |
| 117 | c.moveToPosition(position); |
| 118 | return RecipientEntry.constructTopLevelEntry(c.getString(EmailQuery.NAME), c |
| 119 | .getString(EmailQuery.ADDRESS), c.getLong(EmailQuery.CONTACT_ID), c |
| 120 | .getLong(EmailQuery.DATA_ID), c.getString(EmailQuery.PHOTO_THUMBNAIL_URI)); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public View getView(int position, View convertView, ViewGroup parent) { |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 125 | Cursor cursor = getCursor(); |
| 126 | cursor.moveToPosition(position); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 127 | if (convertView == null) { |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 128 | convertView = newView(); |
| 129 | } |
| 130 | if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) { |
| 131 | mCheckedItemPosition = position; |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame] | 132 | if (mCheckedItemChangedListener != null) { |
| 133 | mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition); |
| 134 | } |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 135 | } |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 136 | bindView(convertView, convertView.getContext(), cursor); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 137 | return convertView; |
| 138 | } |
| 139 | |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 140 | // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine |
| 141 | // somehow? |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 142 | @Override |
| 143 | public void bindView(View view, Context context, Cursor cursor) { |
Mindy Pereira | 67ad489 | 2011-06-02 12:21:22 -0700 | [diff] [blame] | 144 | int position = cursor.getPosition(); |
| 145 | |
| 146 | TextView display = (TextView) view.findViewById(android.R.id.text1); |
| 147 | ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); |
| 148 | RecipientEntry entry = getRecipientEntry(position); |
| 149 | if (position == 0) { |
| 150 | display.setText(cursor.getString(EmailQuery.NAME)); |
| 151 | display.setVisibility(View.VISIBLE); |
| 152 | // TODO: see if this needs to be done outside the main thread |
| 153 | // as it may be too slow to get immediately. |
| 154 | imageView.setImageURI(entry.getPhotoThumbnailUri()); |
| 155 | imageView.setVisibility(View.VISIBLE); |
| 156 | } else { |
| 157 | display.setVisibility(View.GONE); |
| 158 | imageView.setVisibility(View.GONE); |
| 159 | } |
| 160 | TextView destination = (TextView) view.findViewById(android.R.id.text2); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 161 | destination.setText(cursor.getString(EmailQuery.ADDRESS)); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 166 | return newView(); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Mindy Pereira | 007a76b | 2011-06-14 11:39:36 -0700 | [diff] [blame] | 169 | private View newView() { |
| 170 | return mLayoutInflater.inflate(mLayoutId, null); |
| 171 | } |
| 172 | |
Mindy Pereira | 5086391 | 2011-06-16 18:54:10 -0700 | [diff] [blame] | 173 | /*package*/ static interface OnCheckedItemChangedListener { |
| 174 | public void onCheckedItemChanged(int position); |
Mindy Pereira | 97d7768 | 2011-06-01 10:48:55 -0700 | [diff] [blame] | 175 | } |
| 176 | } |