Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2010 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.provider.ContactsContract;
     24 import android.provider.ContactsContract.ContactCounts;
     25 import android.provider.ContactsContract.Data;
     26 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 
     30 /**
     31  * A cursor adapter for the {@link StructuredPostal#CONTENT_TYPE} content type.
     32  */
     33 public class PostalAddressListAdapter extends ContactEntryListAdapter {
     34 
     35     protected static class PostalQuery {
     36         private static final String[] PROJECTION_PRIMARY = new String[] {
     37             StructuredPostal._ID,                       // 0
     38             StructuredPostal.TYPE,                      // 1
     39             StructuredPostal.LABEL,                     // 2
     40             StructuredPostal.DATA,                      // 3
     41             StructuredPostal.PHOTO_ID,                  // 4
     42             StructuredPostal.DISPLAY_NAME_PRIMARY,      // 5
     43         };
     44 
     45         private static final String[] PROJECTION_ALTERNATIVE = new String[] {
     46             StructuredPostal._ID,                       // 0
     47             StructuredPostal.TYPE,                      // 1
     48             StructuredPostal.LABEL,                     // 2
     49             StructuredPostal.DATA,                      // 3
     50             StructuredPostal.PHOTO_ID,                  // 4
     51             StructuredPostal.DISPLAY_NAME_ALTERNATIVE,  // 5
     52         };
     53 
     54         public static final int POSTAL_ID           = 0;
     55         public static final int POSTAL_TYPE         = 1;
     56         public static final int POSTAL_LABEL        = 2;
     57         public static final int POSTAL_ADDRESS      = 3;
     58         public static final int POSTAL_PHOTO_ID     = 4;
     59         public static final int POSTAL_DISPLAY_NAME = 5;
     60     }
     61 
     62     private final CharSequence mUnknownNameText;
     63 
     64     public PostalAddressListAdapter(Context context) {
     65         super(context);
     66 
     67         mUnknownNameText = context.getText(android.R.string.unknownName);
     68     }
     69 
     70     @Override
     71     public void configureLoader(CursorLoader loader, long directoryId) {
     72         Uri uri = buildSectionIndexerUri(StructuredPostal.CONTENT_URI)
     73                 .buildUpon()
     74                 .appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true")
     75                 .build();
     76         loader.setUri(uri);
     77 
     78         if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
     79             loader.setProjection(PostalQuery.PROJECTION_PRIMARY);
     80         } else {
     81             loader.setProjection(PostalQuery.PROJECTION_ALTERNATIVE);
     82         }
     83 
     84         if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
     85             loader.setSortOrder(StructuredPostal.SORT_KEY_PRIMARY);
     86         } else {
     87             loader.setSortOrder(StructuredPostal.SORT_KEY_ALTERNATIVE);
     88         }
     89     }
     90 
     91     protected static Uri buildSectionIndexerUri(Uri uri) {
     92         return uri.buildUpon()
     93                 .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true").build();
     94     }
     95 
     96     @Override
     97     public String getContactDisplayName(int position) {
     98         return ((Cursor) getItem(position)).getString(PostalQuery.POSTAL_DISPLAY_NAME);
     99     }
    100 
    101     /**
    102      * Builds a {@link Data#CONTENT_URI} for the current cursor
    103      * position.
    104      */
    105     public Uri getDataUri(int position) {
    106         long id = ((Cursor)getItem(position)).getLong(PostalQuery.POSTAL_ID);
    107         return ContentUris.withAppendedId(Data.CONTENT_URI, id);
    108     }
    109 
    110     @Override
    111     protected View newView(Context context, int partition, Cursor cursor, int position,
    112             ViewGroup parent) {
    113         final ContactListItemView view = new ContactListItemView(context, null);
    114         view.setUnknownNameText(mUnknownNameText);
    115         view.setQuickContactEnabled(isQuickContactEnabled());
    116         return view;
    117     }
    118 
    119     @Override
    120     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
    121         ContactListItemView view = (ContactListItemView)itemView;
    122         bindSectionHeaderAndDivider(view, position);
    123         bindName(view, cursor);
    124         bindPhoto(view, cursor);
    125         bindPostalAddress(view, cursor);
    126     }
    127 
    128     protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
    129         CharSequence label = null;
    130         if (!cursor.isNull(PostalQuery.POSTAL_TYPE)) {
    131             final int type = cursor.getInt(PostalQuery.POSTAL_TYPE);
    132             final String customLabel = cursor.getString(PostalQuery.POSTAL_LABEL);
    133 
    134             // TODO cache
    135             label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel);
    136         }
    137         view.setLabel(label);
    138         view.showData(cursor, PostalQuery.POSTAL_ADDRESS);
    139     }
    140 
    141     protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
    142         final int section = getSectionForPosition(position);
    143         if (getPositionForSection(section) == position) {
    144             String title = (String)getSections()[section];
    145             view.setSectionHeader(title);
    146         } else {
    147             view.setDividerVisible(false);
    148             view.setSectionHeader(null);
    149         }
    150 
    151         // move the divider for the last item in a section
    152         if (getPositionForSection(section + 1) - 1 == position) {
    153             view.setDividerVisible(false);
    154         } else {
    155             view.setDividerVisible(true);
    156         }
    157     }
    158 
    159     protected void bindName(final ContactListItemView view, Cursor cursor) {
    160         view.showDisplayName(cursor, PostalQuery.POSTAL_DISPLAY_NAME, getContactNameDisplayOrder());
    161     }
    162 
    163     protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
    164         long photoId = 0;
    165         if (!cursor.isNull(PostalQuery.POSTAL_PHOTO_ID)) {
    166             photoId = cursor.getLong(PostalQuery.POSTAL_PHOTO_ID);
    167         }
    168 
    169         getPhotoLoader().loadPhoto(view.getPhotoView(), photoId, false, false);
    170     }
    171 //
    172 //    protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
    173 //        view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
    174 //                SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
    175 //    }
    176 
    177 }
    178