Home | History | Annotate | Download | only in list
      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 package com.android.contacts.list;
     17 
     18 import android.content.ContentUris;
     19 import android.content.Context;
     20 import android.content.CursorLoader;
     21 import android.database.Cursor;
     22 import android.net.Uri;
     23 import android.net.Uri.Builder;
     24 import android.provider.ContactsContract;
     25 import android.provider.ContactsContract.CommonDataKinds.Email;
     26 import android.provider.ContactsContract.Data;
     27 import android.text.TextUtils;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 
     31 import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
     32 import com.android.contacts.common.list.ContactEntryListAdapter;
     33 import com.android.contacts.common.list.ContactListItemView;
     34 import com.android.contacts.common.preference.ContactsPreferences;
     35 
     36 /**
     37  * A cursor adapter for the {@link Email#CONTENT_TYPE} content type.
     38  */
     39 public class EmailAddressListAdapter extends ContactEntryListAdapter {
     40 
     41     protected static class EmailQuery {
     42         private static final String[] PROJECTION_PRIMARY = new String[] {
     43             Email._ID,                       // 0
     44             Email.TYPE,                      // 1
     45             Email.LABEL,                     // 2
     46             Email.DATA,                      // 3
     47             Email.PHOTO_ID,                  // 4
     48             Email.LOOKUP_KEY,                // 5
     49             Email.DISPLAY_NAME_PRIMARY,      // 6
     50         };
     51 
     52         private static final String[] PROJECTION_ALTERNATIVE = new String[] {
     53             Email._ID,                       // 0
     54             Email.TYPE,                      // 1
     55             Email.LABEL,                     // 2
     56             Email.DATA,                      // 3
     57             Email.PHOTO_ID,                  // 4
     58             Email.LOOKUP_KEY,                // 5
     59             Email.DISPLAY_NAME_ALTERNATIVE,  // 6
     60         };
     61 
     62         public static final int EMAIL_ID           = 0;
     63         public static final int EMAIL_TYPE         = 1;
     64         public static final int EMAIL_LABEL        = 2;
     65         public static final int EMAIL_ADDRESS      = 3;
     66         public static final int EMAIL_PHOTO_ID     = 4;
     67         public static final int EMAIL_LOOKUP_KEY   = 5;
     68         public static final int EMAIL_DISPLAY_NAME = 6;
     69     }
     70 
     71     private final CharSequence mUnknownNameText;
     72 
     73     public EmailAddressListAdapter(Context context) {
     74         super(context);
     75 
     76         mUnknownNameText = context.getText(android.R.string.unknownName);
     77     }
     78 
     79     @Override
     80     public void configureLoader(CursorLoader loader, long directoryId) {
     81         final Builder builder;
     82         if (isSearchMode()) {
     83             builder = Email.CONTENT_FILTER_URI.buildUpon();
     84             String query = getQueryString();
     85             builder.appendPath(TextUtils.isEmpty(query) ? "" : query);
     86         } else {
     87             builder = Email.CONTENT_URI.buildUpon();
     88             if (isSectionHeaderDisplayEnabled()) {
     89                 builder.appendQueryParameter(Email.EXTRA_ADDRESS_BOOK_INDEX, "true");
     90             }
     91         }
     92         builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
     93                 String.valueOf(directoryId));
     94         builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
     95         loader.setUri(builder.build());
     96 
     97         if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
     98             loader.setProjection(EmailQuery.PROJECTION_PRIMARY);
     99         } else {
    100             loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE);
    101         }
    102 
    103         if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
    104             loader.setSortOrder(Email.SORT_KEY_PRIMARY);
    105         } else {
    106             loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE);
    107         }
    108     }
    109 
    110     @Override
    111     public String getContactDisplayName(int position) {
    112         return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME);
    113     }
    114 
    115     /**
    116      * Builds a {@link Data#CONTENT_URI} for the current cursor
    117      * position.
    118      */
    119     public Uri getDataUri(int position) {
    120         long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID);
    121         return ContentUris.withAppendedId(Data.CONTENT_URI, id);
    122     }
    123 
    124     @Override
    125     protected ContactListItemView newView(
    126             Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
    127         ContactListItemView view = super.newView(context, partition, cursor, position, parent);
    128         view.setUnknownNameText(mUnknownNameText);
    129         view.setQuickContactEnabled(isQuickContactEnabled());
    130         return view;
    131     }
    132 
    133     @Override
    134     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
    135         super.bindView(itemView, partition, cursor, position);
    136         ContactListItemView view = (ContactListItemView)itemView;
    137         bindSectionHeaderAndDivider(view, position);
    138         bindName(view, cursor);
    139         bindViewId(view, cursor, EmailQuery.EMAIL_ID);
    140         bindPhoto(view, cursor);
    141         bindEmailAddress(view, cursor);
    142     }
    143 
    144     protected void bindEmailAddress(ContactListItemView view, Cursor cursor) {
    145         CharSequence label = null;
    146         if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) {
    147             final int type = cursor.getInt(EmailQuery.EMAIL_TYPE);
    148             final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL);
    149 
    150             // TODO cache
    151             label = Email.getTypeLabel(getContext().getResources(), type, customLabel);
    152         }
    153         view.setLabel(label);
    154         view.showData(cursor, EmailQuery.EMAIL_ADDRESS);
    155     }
    156 
    157     protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
    158         final int section = getSectionForPosition(position);
    159         if (getPositionForSection(section) == position) {
    160             String title = (String)getSections()[section];
    161             view.setSectionHeader(title);
    162         } else {
    163             view.setSectionHeader(null);
    164         }
    165     }
    166 
    167     protected void bindName(final ContactListItemView view, Cursor cursor) {
    168         view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder());
    169     }
    170 
    171     protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
    172         long photoId = 0;
    173         if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) {
    174             photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID);
    175         }
    176         DefaultImageRequest request = null;
    177         if (photoId == 0) {
    178              request = getDefaultImageRequestFromCursor(cursor, EmailQuery.EMAIL_DISPLAY_NAME,
    179                     EmailQuery.EMAIL_LOOKUP_KEY);
    180         }
    181         getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(),
    182                 request);
    183     }
    184 //
    185 //    protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
    186 //        view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
    187 //                SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
    188 //    }
    189 
    190 }
    191