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 com.android.contacts.R;
     19 
     20 import android.content.Context;
     21 import android.content.CursorLoader;
     22 import android.database.Cursor;
     23 import android.database.MatrixCursor;
     24 import android.net.Uri;
     25 import android.net.Uri.Builder;
     26 import android.provider.ContactsContract;
     27 import android.provider.ContactsContract.Contacts;
     28 import android.provider.ContactsContract.Contacts.AggregationSuggestions;
     29 import android.provider.ContactsContract.Directory;
     30 import android.text.TextUtils;
     31 import android.view.LayoutInflater;
     32 import android.view.View;
     33 import android.view.ViewGroup;
     34 import android.widget.TextView;
     35 
     36 public class JoinContactListAdapter extends ContactListAdapter {
     37 
     38     /** Maximum number of suggestions shown for joining aggregates */
     39     private static final int MAX_SUGGESTIONS = 4;
     40 
     41     public static final int PARTITION_SUGGESTIONS = 0;
     42     public static final int PARTITION_ALL_CONTACTS = 1;
     43 
     44     private long mTargetContactId;
     45 
     46     public JoinContactListAdapter(Context context) {
     47         super(context);
     48         setPinnedPartitionHeadersEnabled(true);
     49         setSectionHeaderDisplayEnabled(true);
     50         setIndexedPartition(PARTITION_ALL_CONTACTS);
     51         setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE);
     52     }
     53 
     54     @Override
     55     protected void addPartitions() {
     56 
     57         // Partition 0: suggestions
     58         addPartition(false, true);
     59 
     60         // Partition 1: All contacts
     61         addPartition(createDefaultDirectoryPartition());
     62     }
     63 
     64     public void setTargetContactId(long targetContactId) {
     65         this.mTargetContactId = targetContactId;
     66     }
     67 
     68     @Override
     69     public void configureLoader(CursorLoader cursorLoader, long directoryId) {
     70         JoinContactLoader loader = (JoinContactLoader) cursorLoader;
     71 
     72         Builder builder = Contacts.CONTENT_URI.buildUpon();
     73         builder.appendEncodedPath(String.valueOf(mTargetContactId));
     74         builder.appendEncodedPath(AggregationSuggestions.CONTENT_DIRECTORY);
     75 
     76         String filter = getQueryString();
     77         if (!TextUtils.isEmpty(filter)) {
     78             builder.appendEncodedPath(Uri.encode(filter));
     79         }
     80 
     81         builder.appendQueryParameter("limit", String.valueOf(MAX_SUGGESTIONS));
     82 
     83         loader.setSuggestionUri(builder.build());
     84 
     85         // TODO simplify projection
     86         loader.setProjection(getProjection(false));
     87         Uri allContactsUri = buildSectionIndexerUri(Contacts.CONTENT_URI).buildUpon()
     88                 .appendQueryParameter(
     89                         ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT))
     90                 .build();
     91         loader.setUri(allContactsUri);
     92         loader.setSelection(Contacts._ID + "!=?");
     93         loader.setSelectionArgs(new String[]{String.valueOf(mTargetContactId)});
     94         if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
     95             loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
     96         } else {
     97             loader.setSortOrder(Contacts.SORT_KEY_ALTERNATIVE);
     98         }
     99     }
    100 
    101     @Override
    102     public boolean isEmpty() {
    103         return false;
    104     }
    105 
    106     public void setSuggestionsCursor(Cursor cursor) {
    107         changeCursor(PARTITION_SUGGESTIONS, cursor);
    108     }
    109 
    110     @Override
    111     public void changeCursor(Cursor cursor) {
    112         changeCursor(PARTITION_ALL_CONTACTS, cursor);
    113     }
    114 
    115     @Override
    116     public void configureDefaultPartition(boolean showIfEmpty, boolean hasHeader) {
    117          // Don't change default partition parameters from these defaults
    118         super.configureDefaultPartition(false, true);
    119     }
    120 
    121     @Override
    122     public int getViewTypeCount() {
    123         return super.getViewTypeCount() + 1;
    124     }
    125 
    126     @Override
    127     public int getItemViewType(int partition, int position) {
    128         return super.getItemViewType(partition, position);
    129     }
    130 
    131     @Override
    132     protected View newHeaderView(Context context, int partition, Cursor cursor,
    133             ViewGroup parent) {
    134         switch (partition) {
    135             case PARTITION_SUGGESTIONS: {
    136                 View view = inflate(R.layout.join_contact_picker_section_header, parent);
    137                 ((TextView) view.findViewById(R.id.text)).setText(
    138                         R.string.separatorJoinAggregateSuggestions);
    139                 return view;
    140             }
    141             case PARTITION_ALL_CONTACTS: {
    142                 View view = inflate(R.layout.join_contact_picker_section_header, parent);
    143                 ((TextView) view.findViewById(R.id.text)).setText(
    144                         R.string.separatorJoinAggregateAll);
    145                 return view;
    146             }
    147         }
    148 
    149         return null;
    150     }
    151 
    152     @Override
    153     protected void bindHeaderView(View view, int partitionIndex, Cursor cursor) {
    154         // Header views are static - nothing needs to be bound
    155     }
    156 
    157     @Override
    158     protected View newView(Context context, int partition, Cursor cursor, int position,
    159             ViewGroup parent) {
    160         switch (partition) {
    161             case PARTITION_SUGGESTIONS:
    162             case PARTITION_ALL_CONTACTS:
    163                 return super.newView(context, partition, cursor, position, parent);
    164         }
    165         return null;
    166     }
    167 
    168     private View inflate(int layoutId, ViewGroup parent) {
    169         return LayoutInflater.from(getContext()).inflate(layoutId, parent, false);
    170     }
    171 
    172     @Override
    173     protected void bindView(View itemView, int partition, Cursor cursor, int position) {
    174         switch (partition) {
    175             case PARTITION_SUGGESTIONS: {
    176                 final ContactListItemView view = (ContactListItemView)itemView;
    177                 view.setSectionHeader(null);
    178                 bindPhoto(view, partition, cursor);
    179                 bindName(view, cursor);
    180                 break;
    181             }
    182             case PARTITION_ALL_CONTACTS: {
    183                 final ContactListItemView view = (ContactListItemView)itemView;
    184                 bindSectionHeaderAndDivider(view, position, cursor);
    185                 bindPhoto(view, partition, cursor);
    186                 bindName(view, cursor);
    187                 break;
    188             }
    189         }
    190     }
    191 
    192     @Override
    193     public Uri getContactUri(int partitionIndex, Cursor cursor) {
    194         long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
    195         String lookupKey = cursor.getString(ContactQuery.CONTACT_LOOKUP_KEY);
    196         return Contacts.getLookupUri(contactId, lookupKey);
    197     }
    198 }
    199