RecipientAlternatesAdapter.java revision 80f4abfb682426384e88fb1dddc682be1c8a6c7f
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 android.content.Context; 20import android.database.Cursor; 21import android.text.util.Rfc822Token; 22import android.text.util.Rfc822Tokenizer; 23import android.util.Log; 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 31import com.android.ex.chips.Queries.Query; 32 33import java.util.HashMap; 34 35/** 36 * RecipientAlternatesAdapter backs the RecipientEditTextView for managing contacts 37 * queried by email or by phone number. 38 */ 39public class RecipientAlternatesAdapter extends CursorAdapter { 40 static final int MAX_LOOKUPS = 50; 41 private final LayoutInflater mLayoutInflater; 42 43 private final long mCurrentId; 44 45 private int mCheckedItemPosition = -1; 46 47 private OnCheckedItemChangedListener mCheckedItemChangedListener; 48 49 private static final String TAG = "RecipAlternates"; 50 51 public static final int QUERY_TYPE_EMAIL = 0; 52 public static final int QUERY_TYPE_PHONE = 1; 53 private Query mQuery; 54 55 public static HashMap<String, RecipientEntry> getMatchingRecipients(Context context, 56 String[] inAddresses) { 57 return getMatchingRecipients(context, inAddresses, QUERY_TYPE_EMAIL); 58 } 59 60 /** 61 * Get a HashMap of address to RecipientEntry that contains all contact 62 * information for a contact with the provided address, if one exists. This 63 * may block the UI, so run it in an async task. 64 * 65 * @param context Context. 66 * @param inAddresses Array of addresses on which to perform the lookup. 67 * @return HashMap<String,RecipientEntry> 68 */ 69 public static HashMap<String, RecipientEntry> getMatchingRecipients(Context context, 70 String[] inAddresses, int addressType) { 71 Queries.Query query; 72 if (addressType == QUERY_TYPE_EMAIL) { 73 query = Queries.EMAIL; 74 } else { 75 query = Queries.PHONE; 76 } 77 int addressesSize = Math.min(MAX_LOOKUPS, inAddresses.length); 78 String[] addresses = new String[addressesSize]; 79 StringBuilder bindString = new StringBuilder(); 80 // Create the "?" string and set up arguments. 81 for (int i = 0; i < addressesSize; i++) { 82 Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(inAddresses[i].toLowerCase()); 83 addresses[i] = (tokens.length > 0 ? tokens[0].getAddress() : inAddresses[i]); 84 bindString.append("?"); 85 if (i < addressesSize - 1) { 86 bindString.append(","); 87 } 88 } 89 90 if (Log.isLoggable(TAG, Log.DEBUG)) { 91 Log.d(TAG, "Doing reverse lookup for " + addresses.toString()); 92 } 93 94 HashMap<String, RecipientEntry> recipientEntries = new HashMap<String, RecipientEntry>(); 95 Cursor c = context.getContentResolver().query( 96 query.getContentUri(), query.getProjection(), 97 Queries.Query.DESTINATION + " IN (" + bindString.toString() + ")", 98 addresses, 99 null); 100 101 if (c != null) { 102 try { 103 if (c.moveToFirst()) { 104 do { 105 String address = c.getString(Queries.Query.DESTINATION); 106 recipientEntries.put(address, RecipientEntry.constructTopLevelEntry( 107 c.getString(Queries.Query.NAME), 108 c.getString(Queries.Query.DESTINATION), 109 c.getInt(Queries.Query.DESTINATION_TYPE), 110 c.getString(Queries.Query.DESTINATION_LABEL), 111 c.getLong(Queries.Query.CONTACT_ID), 112 c.getLong(Queries.Query.DATA_ID), 113 c.getString(Queries.Query.PHOTO_THUMBNAIL_URI))); 114 if (Log.isLoggable(TAG, Log.DEBUG)) { 115 Log.d(TAG, "Received reverse look up information for " + address 116 + " RESULTS: " 117 + " NAME : " + c.getString(Queries.Query.NAME) 118 + " CONTACT ID : " + c.getLong(Queries.Query.CONTACT_ID) 119 + " ADDRESS :" + c.getString(Queries.Query.DESTINATION)); 120 } 121 } while (c.moveToNext()); 122 } 123 } finally { 124 c.close(); 125 } 126 } 127 return recipientEntries; 128 } 129 130 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, 131 OnCheckedItemChangedListener listener) { 132 this(context, contactId, currentId, viewId, QUERY_TYPE_EMAIL, listener); 133 } 134 135 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId, 136 int queryMode, OnCheckedItemChangedListener listener) { 137 super(context, getCursorForConstruction(context, contactId, queryMode), 0); 138 mLayoutInflater = LayoutInflater.from(context); 139 mCurrentId = currentId; 140 mCheckedItemChangedListener = listener; 141 142 if (queryMode == QUERY_TYPE_EMAIL) { 143 mQuery = Queries.EMAIL; 144 } else if (queryMode == QUERY_TYPE_PHONE) { 145 mQuery = Queries.PHONE; 146 } else { 147 mQuery = Queries.EMAIL; 148 Log.e(TAG, "Unsupported query type: " + queryMode); 149 } 150 } 151 152 private static Cursor getCursorForConstruction(Context context, long contactId, int queryType) { 153 if (queryType == QUERY_TYPE_EMAIL) { 154 return context.getContentResolver().query( 155 Queries.EMAIL.getContentUri(), 156 Queries.EMAIL.getProjection(), 157 Queries.Query.CONTACT_ID + " =?", new String[] { 158 String.valueOf(contactId) 159 }, null); 160 } else { 161 return context.getContentResolver().query( 162 Queries.PHONE.getContentUri(), 163 Queries.PHONE.getProjection(), 164 Queries.Query.CONTACT_ID + " =?", new String[] { 165 String.valueOf(contactId) 166 }, null); 167 } 168 } 169 170 @Override 171 public long getItemId(int position) { 172 Cursor c = getCursor(); 173 if (c.moveToPosition(position)) { 174 c.getLong(Queries.Query.DATA_ID); 175 } 176 return -1; 177 } 178 179 public RecipientEntry getRecipientEntry(int position) { 180 Cursor c = getCursor(); 181 c.moveToPosition(position); 182 return RecipientEntry.constructTopLevelEntry( 183 c.getString(Queries.Query.NAME), 184 c.getString(Queries.Query.DESTINATION), 185 c.getInt(Queries.Query.DESTINATION_TYPE), 186 c.getString(Queries.Query.DESTINATION_LABEL), 187 c.getLong(Queries.Query.CONTACT_ID), 188 c.getLong(Queries.Query.DATA_ID), 189 c.getString(Queries.Query.PHOTO_THUMBNAIL_URI)); 190 } 191 192 @Override 193 public View getView(int position, View convertView, ViewGroup parent) { 194 Cursor cursor = getCursor(); 195 cursor.moveToPosition(position); 196 if (convertView == null) { 197 convertView = newView(); 198 } 199 if (cursor.getLong(Queries.Query.DATA_ID) == mCurrentId) { 200 mCheckedItemPosition = position; 201 if (mCheckedItemChangedListener != null) { 202 mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition); 203 } 204 } 205 bindView(convertView, convertView.getContext(), cursor); 206 return convertView; 207 } 208 209 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine 210 // somehow? 211 @Override 212 public void bindView(View view, Context context, Cursor cursor) { 213 int position = cursor.getPosition(); 214 215 TextView display = (TextView) view.findViewById(android.R.id.title); 216 ImageView imageView = (ImageView) view.findViewById(android.R.id.icon); 217 RecipientEntry entry = getRecipientEntry(position); 218 if (position == 0) { 219 display.setText(cursor.getString(Queries.Query.NAME)); 220 display.setVisibility(View.VISIBLE); 221 // TODO: see if this needs to be done outside the main thread 222 // as it may be too slow to get immediately. 223 imageView.setImageURI(entry.getPhotoThumbnailUri()); 224 imageView.setVisibility(View.VISIBLE); 225 } else { 226 display.setVisibility(View.GONE); 227 imageView.setVisibility(View.GONE); 228 } 229 TextView destination = (TextView) view.findViewById(android.R.id.text1); 230 destination.setText(cursor.getString(Queries.Query.DESTINATION)); 231 232 TextView destinationType = (TextView) view.findViewById(android.R.id.text2); 233 if (destinationType != null) { 234 destinationType.setText(mQuery.getTypeLabel(context.getResources(), 235 cursor.getInt(Queries.Query.DESTINATION_TYPE), 236 cursor.getString(Queries.Query.DESTINATION_LABEL)).toString().toUpperCase()); 237 } 238 } 239 240 @Override 241 public View newView(Context context, Cursor cursor, ViewGroup parent) { 242 return newView(); 243 } 244 245 private View newView() { 246 return mLayoutInflater.inflate(R.layout.chips_recipient_dropdown_item, null); 247 } 248 249 /*package*/ static interface OnCheckedItemChangedListener { 250 public void onCheckedItemChanged(int position); 251 } 252} 253