Home | History | Annotate | Download | only in contacts
      1 /*
      2  * Copyright (C) 2006 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 
     17 package com.android.contacts;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.graphics.drawable.Drawable;
     22 import android.net.Uri;
     23 import android.provider.ContactsContract.CommonDataKinds.Email;
     24 import android.provider.ContactsContract.CommonDataKinds.Nickname;
     25 import android.provider.ContactsContract.CommonDataKinds.Phone;
     26 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
     27 import android.provider.ContactsContract.Contacts.Data;
     28 import android.provider.ContactsContract.RawContacts;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.AdapterView;
     33 import android.widget.ArrayAdapter;
     34 import android.widget.ImageView;
     35 import android.widget.ListView;
     36 import android.widget.TextView;
     37 
     38 import com.android.contacts.model.AccountTypeManager;
     39 import com.android.contacts.model.account.AccountType;
     40 
     41 import java.util.ArrayList;
     42 import java.util.Collections;
     43 import java.util.HashMap;
     44 import java.util.List;
     45 
     46 /**
     47  * A list view for constituent contacts of an aggregate.  Shows the contact name, source icon
     48  * and additional data such as a nickname, email address or phone number, whichever
     49  * is available.
     50  */
     51 public class SplitAggregateView extends ListView {
     52 
     53     private static final String TAG = "SplitAggregateView";
     54 
     55     private interface SplitQuery {
     56         String[] COLUMNS = new String[] {
     57                 Data.MIMETYPE, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET, Data.RAW_CONTACT_ID,
     58                 Data.IS_PRIMARY, StructuredName.DISPLAY_NAME, Nickname.NAME, Email.DATA,
     59                 Phone.NUMBER
     60         };
     61 
     62         int MIMETYPE = 0;
     63         int ACCOUNT_TYPE = 1;
     64         int DATA_SET = 2;
     65         int RAW_CONTACT_ID = 3;
     66         int IS_PRIMARY = 4;
     67         int DISPLAY_NAME = 5;
     68         int NICKNAME = 6;
     69         int EMAIL = 7;
     70         int PHONE = 8;
     71     }
     72 
     73     private final Uri mAggregateUri;
     74     private OnContactSelectedListener mListener;
     75     private AccountTypeManager mAccountTypes;
     76 
     77     /**
     78      * Listener interface that gets the contact ID of the user-selected contact.
     79      */
     80     public interface OnContactSelectedListener {
     81         void onContactSelected(long rawContactId);
     82     }
     83 
     84     /**
     85      * Constructor.
     86      */
     87     public SplitAggregateView(Context context, Uri aggregateUri) {
     88         super(context);
     89 
     90         mAggregateUri = aggregateUri;
     91 
     92         mAccountTypes = AccountTypeManager.getInstance(context);
     93 
     94         final List<RawContactInfo> list = loadData();
     95 
     96         setAdapter(new SplitAggregateAdapter(context, list));
     97         setOnItemClickListener(new OnItemClickListener() {
     98 
     99             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    100                 mListener.onContactSelected(list.get(position).rawContactId);
    101             }
    102         });
    103     }
    104 
    105     /**
    106      * Sets a contact selection listener.
    107      */
    108     public void setOnContactSelectedListener(OnContactSelectedListener listener) {
    109         mListener = listener;
    110     }
    111 
    112     /**
    113      * Contact information loaded from the content provider.
    114      */
    115     private static class RawContactInfo implements Comparable<RawContactInfo> {
    116         final long rawContactId;
    117         String accountType;
    118         String dataSet;
    119         String name;
    120         String phone;
    121         String email;
    122         String nickname;
    123 
    124         public RawContactInfo(long rawContactId) {
    125             this.rawContactId = rawContactId;
    126         }
    127 
    128         public String getAdditionalData() {
    129             if (nickname != null) {
    130                 return nickname;
    131             }
    132 
    133             if (email != null) {
    134                 return email;
    135             }
    136 
    137             if (phone != null) {
    138                 return phone;
    139             }
    140 
    141             return "";
    142         }
    143 
    144         public int compareTo(RawContactInfo another) {
    145             String thisAccount = accountType != null ? accountType : "";
    146             String thatAccount = another.accountType != null ? another.accountType : "";
    147             return thisAccount.compareTo(thatAccount);
    148         }
    149     }
    150 
    151     /**
    152      * Loads data from the content provider, organizes it into {@link RawContactInfo} objects
    153      * and returns a sorted list of {@link RawContactInfo}'s.
    154      */
    155     private List<RawContactInfo> loadData() {
    156         HashMap<Long, RawContactInfo> rawContactInfos = new HashMap<Long, RawContactInfo>();
    157         Uri dataUri = Uri.withAppendedPath(mAggregateUri, Data.CONTENT_DIRECTORY);
    158         Cursor cursor = getContext().getContentResolver().query(dataUri,
    159                 SplitQuery.COLUMNS, null, null, null);
    160         try {
    161             while (cursor.moveToNext()) {
    162                 long rawContactId = cursor.getLong(SplitQuery.RAW_CONTACT_ID);
    163                 RawContactInfo info = rawContactInfos.get(rawContactId);
    164                 if (info == null) {
    165                     info = new RawContactInfo(rawContactId);
    166                     rawContactInfos.put(rawContactId, info);
    167                     info.accountType = cursor.getString(SplitQuery.ACCOUNT_TYPE);
    168                     info.dataSet = cursor.getString(SplitQuery.DATA_SET);
    169                 }
    170 
    171                 String mimetype = cursor.getString(SplitQuery.MIMETYPE);
    172                 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimetype)) {
    173                     loadStructuredName(cursor, info);
    174                 } else if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
    175                     loadPhoneNumber(cursor, info);
    176                 } else if (Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
    177                     loadEmail(cursor, info);
    178                 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
    179                     loadNickname(cursor, info);
    180                 }
    181             }
    182         } finally {
    183             cursor.close();
    184         }
    185 
    186         List<RawContactInfo> list = new ArrayList<RawContactInfo>(rawContactInfos.values());
    187         Collections.sort(list);
    188         return list;
    189     }
    190 
    191     private void loadStructuredName(Cursor cursor, RawContactInfo info) {
    192         info.name = cursor.getString(SplitQuery.DISPLAY_NAME);
    193     }
    194 
    195     private void loadNickname(Cursor cursor, RawContactInfo info) {
    196         if (info.nickname == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
    197             info.nickname = cursor.getString(SplitQuery.NICKNAME);
    198         }
    199     }
    200 
    201     private void loadEmail(Cursor cursor, RawContactInfo info) {
    202         if (info.email == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
    203             info.email = cursor.getString(SplitQuery.EMAIL);
    204         }
    205     }
    206 
    207     private void loadPhoneNumber(Cursor cursor, RawContactInfo info) {
    208         if (info.phone == null || cursor.getInt(SplitQuery.IS_PRIMARY) != 0) {
    209             info.phone = cursor.getString(SplitQuery.PHONE);
    210         }
    211     }
    212 
    213     private static class SplitAggregateItemCache  {
    214         TextView name;
    215         TextView additionalData;
    216         ImageView sourceIcon;
    217     }
    218 
    219     /**
    220      * List adapter for the list of {@link RawContactInfo} objects.
    221      */
    222     private class SplitAggregateAdapter extends ArrayAdapter<RawContactInfo> {
    223 
    224         private LayoutInflater mInflater;
    225 
    226         public SplitAggregateAdapter(Context context, List<RawContactInfo> sources) {
    227             super(context, 0, sources);
    228             mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    229         }
    230 
    231         @Override
    232         public View getView(int position, View convertView, ViewGroup parent) {
    233             if (convertView == null) {
    234                 convertView = mInflater.inflate(R.layout.split_aggregate_list_item, parent, false);
    235             }
    236 
    237             SplitAggregateItemCache cache = (SplitAggregateItemCache)convertView.getTag();
    238             if (cache == null) {
    239                 cache = new SplitAggregateItemCache();
    240                 cache.name = (TextView)convertView.findViewById(R.id.name);
    241                 cache.additionalData = (TextView)convertView.findViewById(R.id.additionalData);
    242                 cache.sourceIcon = (ImageView)convertView.findViewById(R.id.sourceIcon);
    243                 convertView.setTag(cache);
    244             }
    245 
    246             final RawContactInfo info = getItem(position);
    247             cache.name.setText(info.name);
    248             cache.additionalData.setText(info.getAdditionalData());
    249 
    250             Drawable icon = null;
    251             AccountType accountType = mAccountTypes.getAccountType(info.accountType, info.dataSet);
    252             if (accountType != null) {
    253                 icon = accountType.getDisplayIcon(getContext());
    254             }
    255             if (icon != null) {
    256                 cache.sourceIcon.setImageDrawable(icon);
    257             } else {
    258                 cache.sourceIcon.setImageResource(R.drawable.unknown_source);
    259             }
    260             return convertView;
    261         }
    262     }
    263 }
    264