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.common.list;
     17 
     18 import android.content.ContentUris;
     19 import android.content.Context;
     20 import android.content.CursorLoader;
     21 import android.content.SharedPreferences;
     22 import android.database.Cursor;
     23 import android.net.Uri;
     24 import android.net.Uri.Builder;
     25 import android.preference.PreferenceManager;
     26 import android.provider.ContactsContract;
     27 import android.provider.ContactsContract.Contacts;
     28 import android.provider.ContactsContract.Directory;
     29 import android.provider.ContactsContract.SearchSnippets;
     30 import android.text.TextUtils;
     31 import android.view.View;
     32 
     33 import com.android.contacts.common.preference.ContactsPreferences;
     34 
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 /**
     39  * A cursor adapter for the {@link ContactsContract.Contacts#CONTENT_TYPE} content type.
     40  */
     41 public class DefaultContactListAdapter extends ContactListAdapter {
     42 
     43     public static final char SNIPPET_START_MATCH = '[';
     44     public static final char SNIPPET_END_MATCH = ']';
     45 
     46     public DefaultContactListAdapter(Context context) {
     47         super(context);
     48     }
     49 
     50     @Override
     51     public void configureLoader(CursorLoader loader, long directoryId) {
     52         if (loader instanceof ProfileAndContactsLoader) {
     53             ((ProfileAndContactsLoader) loader).setLoadProfile(shouldIncludeProfile());
     54         }
     55 
     56         ContactListFilter filter = getFilter();
     57         if (isSearchMode()) {
     58             String query = getQueryString();
     59             if (query == null) {
     60                 query = "";
     61             }
     62             query = query.trim();
     63             if (TextUtils.isEmpty(query)) {
     64                 // Regardless of the directory, we don't want anything returned,
     65                 // so let's just send a "nothing" query to the local directory.
     66                 loader.setUri(Contacts.CONTENT_URI);
     67                 loader.setProjection(getProjection(false));
     68                 loader.setSelection("0");
     69             } else {
     70                 Builder builder = Contacts.CONTENT_FILTER_URI.buildUpon();
     71                 builder.appendPath(query);      // Builder will encode the query
     72                 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
     73                         String.valueOf(directoryId));
     74                 if (directoryId != Directory.DEFAULT && directoryId != Directory.LOCAL_INVISIBLE) {
     75                     builder.appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
     76                             String.valueOf(getDirectoryResultLimit(getDirectoryById(directoryId))));
     77                 }
     78                 builder.appendQueryParameter(SearchSnippets.DEFERRED_SNIPPETING_KEY,"1");
     79                 loader.setUri(builder.build());
     80                 loader.setProjection(getProjection(true));
     81             }
     82         } else {
     83             configureUri(loader, directoryId, filter);
     84             loader.setProjection(getProjection(false));
     85             configureSelection(loader, directoryId, filter);
     86         }
     87 
     88         String sortOrder;
     89         if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
     90             sortOrder = Contacts.SORT_KEY_PRIMARY;
     91         } else {
     92             sortOrder = Contacts.SORT_KEY_ALTERNATIVE;
     93         }
     94 
     95         loader.setSortOrder(sortOrder);
     96     }
     97 
     98     protected void configureUri(CursorLoader loader, long directoryId, ContactListFilter filter) {
     99         Uri uri = Contacts.CONTENT_URI;
    100         if (filter != null && filter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) {
    101             String lookupKey = getSelectedContactLookupKey();
    102             if (lookupKey != null) {
    103                 uri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey);
    104             } else {
    105                 uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, getSelectedContactId());
    106             }
    107         }
    108 
    109         if (directoryId == Directory.DEFAULT && isSectionHeaderDisplayEnabled()) {
    110             uri = ContactListAdapter.buildSectionIndexerUri(uri);
    111         }
    112 
    113         // The "All accounts" filter is the same as the entire contents of Directory.DEFAULT
    114         if (filter != null
    115                 && filter.filterType != ContactListFilter.FILTER_TYPE_CUSTOM
    116                 && filter.filterType != ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) {
    117             final Uri.Builder builder = uri.buildUpon();
    118             builder.appendQueryParameter(
    119                     ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT));
    120             if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT) {
    121                 filter.addAccountQueryParameterToUrl(builder);
    122             }
    123             uri = builder.build();
    124         }
    125 
    126         loader.setUri(uri);
    127     }
    128 
    129     private void configureSelection(
    130             CursorLoader loader, long directoryId, ContactListFilter filter) {
    131         if (filter == null) {
    132             return;
    133         }
    134 
    135         if (directoryId != Directory.DEFAULT) {
    136             return;
    137         }
    138 
    139         StringBuilder selection = new StringBuilder();
    140         List<String> selectionArgs = new ArrayList<String>();
    141 
    142         switch (filter.filterType) {
    143             case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS: {
    144                 // We have already added directory=0 to the URI, which takes care of this
    145                 // filter
    146                 break;
    147             }
    148             case ContactListFilter.FILTER_TYPE_SINGLE_CONTACT: {
    149                 // We have already added the lookup key to the URI, which takes care of this
    150                 // filter
    151                 break;
    152             }
    153             case ContactListFilter.FILTER_TYPE_STARRED: {
    154                 selection.append(Contacts.STARRED + "!=0");
    155                 break;
    156             }
    157             case ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY: {
    158                 selection.append(Contacts.HAS_PHONE_NUMBER + "=1");
    159                 break;
    160             }
    161             case ContactListFilter.FILTER_TYPE_CUSTOM: {
    162                 selection.append(Contacts.IN_VISIBLE_GROUP + "=1");
    163                 if (isCustomFilterForPhoneNumbersOnly()) {
    164                     selection.append(" AND " + Contacts.HAS_PHONE_NUMBER + "=1");
    165                 }
    166                 break;
    167             }
    168             case ContactListFilter.FILTER_TYPE_ACCOUNT: {
    169                 // We use query parameters for account filter, so no selection to add here.
    170                 break;
    171             }
    172         }
    173         loader.setSelection(selection.toString());
    174         loader.setSelectionArgs(selectionArgs.toArray(new String[0]));
    175     }
    176 
    177     @Override
    178     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
    179         super.bindView(itemView, partition, cursor, position);
    180         final ContactListItemView view = (ContactListItemView)itemView;
    181 
    182         view.setHighlightedPrefix(isSearchMode() ? getUpperCaseQueryString() : null);
    183 
    184         if (isSelectionVisible()) {
    185             view.setActivated(isSelectedContact(partition, cursor));
    186         }
    187 
    188         bindSectionHeaderAndDivider(view, position, cursor);
    189 
    190         if (isQuickContactEnabled()) {
    191             bindQuickContact(view, partition, cursor, ContactQuery.CONTACT_PHOTO_ID,
    192                     ContactQuery.CONTACT_PHOTO_URI, ContactQuery.CONTACT_ID,
    193                     ContactQuery.CONTACT_LOOKUP_KEY, ContactQuery.CONTACT_DISPLAY_NAME);
    194         } else {
    195             if (getDisplayPhotos()) {
    196                 bindPhoto(view, partition, cursor);
    197             }
    198         }
    199 
    200         bindNameAndViewId(view, cursor);
    201         bindPresenceAndStatusMessage(view, cursor);
    202 
    203         if (isSearchMode()) {
    204             bindSearchSnippet(view, cursor);
    205         } else {
    206             view.setSnippet(null);
    207         }
    208     }
    209 
    210     private boolean isCustomFilterForPhoneNumbersOnly() {
    211         // TODO: this flag should not be stored in shared prefs.  It needs to be in the db.
    212         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    213         return prefs.getBoolean(ContactsPreferences.PREF_DISPLAY_ONLY_PHONES,
    214                 ContactsPreferences.PREF_DISPLAY_ONLY_PHONES_DEFAULT);
    215     }
    216 }
    217