blob: d92fecd8ec830b73e3de129d7697b8f0a76c5e14 [file] [log] [blame]
Mindy Pereira84dd9a82011-06-01 11:13:08 -07001/*
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 Pereira97d77682011-06-01 10:48:55 -070016
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;
Mindy Pereira18529312011-06-28 11:00:52 -070024import android.text.util.Rfc822Token;
25import android.text.util.Rfc822Tokenizer;
Mindy Pereira97d77682011-06-01 10:48:55 -070026import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.CursorAdapter;
Mindy Pereira67ad4892011-06-02 12:21:22 -070030import android.widget.ImageView;
Mindy Pereira97d77682011-06-01 10:48:55 -070031import android.widget.TextView;
32
Mindy Pereira18529312011-06-28 11:00:52 -070033import java.util.HashMap;
34
Mindy Pereira97d77682011-06-01 10:48:55 -070035public class RecipientAlternatesAdapter extends CursorAdapter {
Mindy Pereira18529312011-06-28 11:00:52 -070036 static final int MAX_LOOKUPS = 50;
Mindy Pereira97d77682011-06-01 10:48:55 -070037 private final LayoutInflater mLayoutInflater;
38
39 private final int mLayoutId;
40
Mindy Pereira97d77682011-06-01 10:48:55 -070041 private final long mCurrentId;
42
Mindy Pereira007a76b2011-06-14 11:39:36 -070043 private int mCheckedItemPosition = -1;
44
Mindy Pereira50863912011-06-16 18:54:10 -070045 private OnCheckedItemChangedListener mCheckedItemChangedListener;
46
Mindy Pereira18529312011-06-28 11:00:52 -070047 /**
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 Pereira50863912011-06-16 18:54:10 -070094 public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId,
95 OnCheckedItemChangedListener listener) {
Mindy Pereira97d77682011-06-01 10:48:55 -070096 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 Pereira97d77682011-06-01 10:48:55 -0700102 mCurrentId = currentId;
Mindy Pereira50863912011-06-16 18:54:10 -0700103 mCheckedItemChangedListener = listener;
Mindy Pereira97d77682011-06-01 10:48:55 -0700104 }
105
106 @Override
107 public long getItemId(int position) {
108 Cursor c = getCursor();
Mindy Pereira3bb52162011-07-01 14:52:22 -0700109 if (c.moveToPosition(position)) {
110 c.getLong(EmailQuery.DATA_ID);
111 }
112 return -1;
Mindy Pereira97d77682011-06-01 10:48:55 -0700113 }
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 Pereira67ad4892011-06-02 12:21:22 -0700125 Cursor cursor = getCursor();
126 cursor.moveToPosition(position);
Mindy Pereira97d77682011-06-01 10:48:55 -0700127 if (convertView == null) {
Mindy Pereira007a76b2011-06-14 11:39:36 -0700128 convertView = newView();
129 }
130 if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) {
131 mCheckedItemPosition = position;
Mindy Pereira50863912011-06-16 18:54:10 -0700132 if (mCheckedItemChangedListener != null) {
133 mCheckedItemChangedListener.onCheckedItemChanged(mCheckedItemPosition);
134 }
Mindy Pereira97d77682011-06-01 10:48:55 -0700135 }
Mindy Pereira67ad4892011-06-02 12:21:22 -0700136 bindView(convertView, convertView.getContext(), cursor);
Mindy Pereira97d77682011-06-01 10:48:55 -0700137 return convertView;
138 }
139
Mindy Pereira67ad4892011-06-02 12:21:22 -0700140 // TODO: this is VERY similar to the BaseRecipientAdapter. Can we combine
141 // somehow?
Mindy Pereira97d77682011-06-01 10:48:55 -0700142 @Override
143 public void bindView(View view, Context context, Cursor cursor) {
Mindy Pereira67ad4892011-06-02 12:21:22 -0700144 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 Pereira97d77682011-06-01 10:48:55 -0700161 destination.setText(cursor.getString(EmailQuery.ADDRESS));
162 }
163
164 @Override
165 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Mindy Pereira007a76b2011-06-14 11:39:36 -0700166 return newView();
Mindy Pereira97d77682011-06-01 10:48:55 -0700167 }
168
Mindy Pereira007a76b2011-06-14 11:39:36 -0700169 private View newView() {
170 return mLayoutInflater.inflate(mLayoutId, null);
171 }
172
Mindy Pereira50863912011-06-16 18:54:10 -0700173 /*package*/ static interface OnCheckedItemChangedListener {
174 public void onCheckedItemChanged(int position);
Mindy Pereira97d77682011-06-01 10:48:55 -0700175 }
176}