1/*
2 * Copyright (C) 2007 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.email;
18
19import com.android.email.mail.Address;
20import com.android.email.provider.EmailContent.Account;
21
22import android.content.ContentResolver;
23import android.content.Context;
24import android.database.Cursor;
25import android.net.Uri;
26import android.provider.ContactsContract.Contacts;
27import android.provider.ContactsContract.Data;
28import android.provider.ContactsContract.CommonDataKinds.Email;
29import android.view.View;
30import android.widget.ResourceCursorAdapter;
31import android.widget.TextView;
32
33public class EmailAddressAdapter extends ResourceCursorAdapter {
34    public static final int ID_INDEX = 0;
35    public static final int NAME_INDEX = 1;
36    public static final int DATA_INDEX = 2;
37
38    protected static final String SORT_ORDER =
39            Contacts.TIMES_CONTACTED + " DESC, " + Contacts.DISPLAY_NAME;
40
41    protected final ContentResolver mContentResolver;
42
43    protected static final String[] PROJECTION = {
44        Data._ID,               // 0
45        Contacts.DISPLAY_NAME,  // 1
46        Email.DATA              // 2
47    };
48
49    public EmailAddressAdapter(Context context) {
50        super(context, R.layout.recipient_dropdown_item, null);
51        mContentResolver = context.getContentResolver();
52    }
53
54    @Override
55    public final String convertToString(Cursor cursor) {
56        String name = cursor.getString(NAME_INDEX);
57        String address = cursor.getString(DATA_INDEX);
58
59        return new Address(address, name).toString();
60    }
61
62    @Override
63    public void bindView(View view, Context context, Cursor cursor) {
64        TextView text1 = (TextView)view.findViewById(R.id.text1);
65        TextView text2 = (TextView)view.findViewById(R.id.text2);
66        text1.setText(cursor.getString(NAME_INDEX));
67        text2.setText(cursor.getString(DATA_INDEX));
68    }
69
70    @Override
71    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
72        String filter = constraint == null ? "" : constraint.toString();
73        Uri uri = Uri.withAppendedPath(Email.CONTENT_FILTER_URI, Uri.encode(filter));
74        Cursor c = mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER);
75        // To prevent expensive execution in the UI thread
76        // Cursors get lazily executed, so if you don't call anything on the cursor before
77        // returning it from the background thread you'll have a complied program for the cursor,
78        // but it won't have been executed to generate the data yet. Often the execution is more
79        // expensive than the compilation...
80        if (c != null) {
81            c.getCount();
82        }
83        return c;
84    }
85
86    /**
87     * Set the account when known.  Not used for generic contacts lookup;  Use when
88     * linking lookup to specific account.
89     */
90    public void setAccount(Account account) { }
91}
92