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