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