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 17 package com.android.calendar; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.net.Uri; 23 import android.provider.ContactsContract.Contacts; 24 import android.provider.ContactsContract.Data; 25 import android.provider.ContactsContract.CommonDataKinds.Email; 26 import android.text.util.Rfc822Token; 27 import android.view.View; 28 import android.widget.ResourceCursorAdapter; 29 import android.widget.TextView; 30 31 // Customized from com.android.email.EmailAddressAdapter 32 33 public class EmailAddressAdapter extends ResourceCursorAdapter { 34 public static final int NAME_INDEX = 1; 35 public static final int DATA_INDEX = 2; 36 37 private static final String SORT_ORDER = 38 Contacts.TIMES_CONTACTED + " DESC, " + Contacts.DISPLAY_NAME; 39 40 private ContentResolver mContentResolver; 41 42 private static final String[] PROJECTION = { 43 Data._ID, // 0 44 Contacts.DISPLAY_NAME, // 1 45 Email.DATA // 2 46 }; 47 48 public EmailAddressAdapter(Context context) { 49 super(context, android.R.layout.simple_dropdown_item_1line, null); 50 mContentResolver = context.getContentResolver(); 51 } 52 53 @Override 54 public final String convertToString(Cursor cursor) { 55 return makeDisplayString(cursor); 56 } 57 58 private final String makeDisplayString(Cursor cursor) { 59 String name = cursor.getString(NAME_INDEX); 60 String address = cursor.getString(DATA_INDEX); 61 62 return new Rfc822Token(name, address, null).toString(); 63 } 64 65 @Override 66 public final void bindView(View view, Context context, Cursor cursor) { 67 ((TextView) view).setText(makeDisplayString(cursor)); 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 return mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER); 75 } 76 } 77