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