Home | History | Annotate | Download | only in data
      1 package com.android.mms.data;
      2 
      3 import java.util.ArrayList;
      4 import java.util.List;
      5 
      6 import android.text.TextUtils;
      7 import android.util.Log;
      8 
      9 import com.android.mms.data.Contact.UpdateListener;
     10 import com.android.mms.LogTag;
     11 import com.android.mms.ui.MessageUtils;
     12 
     13 public class ContactList extends ArrayList<Contact>  {
     14     private static final long serialVersionUID = 1L;
     15 
     16     public static ContactList getByNumbers(Iterable<String> numbers, boolean canBlock) {
     17         ContactList list = new ContactList();
     18         for (String number : numbers) {
     19             if (!TextUtils.isEmpty(number)) {
     20                 list.add(Contact.get(number, canBlock));
     21             }
     22         }
     23         return list;
     24     }
     25 
     26     public static ContactList getByNumbers(String semiSepNumbers,
     27                                            boolean canBlock,
     28                                            boolean replaceNumber) {
     29         ContactList list = new ContactList();
     30         for (String number : semiSepNumbers.split(";")) {
     31             if (!TextUtils.isEmpty(number)) {
     32                 Contact contact = Contact.get(number, canBlock);
     33                 if (replaceNumber) {
     34                     contact.setNumber(number);
     35                 }
     36                 list.add(contact);
     37             }
     38         }
     39         return list;
     40     }
     41 
     42     /**
     43      * Returns a ContactList for the corresponding recipient ids passed in. This method will
     44      * create the contact if it doesn't exist, and would inject the recipient id into the contact.
     45      */
     46     public static ContactList getByIds(String spaceSepIds, boolean canBlock) {
     47         ContactList list = new ContactList();
     48         for (RecipientIdCache.Entry entry : RecipientIdCache.getAddresses(spaceSepIds)) {
     49             if (entry != null && !TextUtils.isEmpty(entry.number)) {
     50                 Contact contact = Contact.get(entry.number, canBlock);
     51                 contact.setRecipientId(entry.id);
     52                 list.add(contact);
     53             }
     54         }
     55         return list;
     56     }
     57 
     58     public int getPresenceResId() {
     59         // We only show presence for single contacts.
     60         if (size() != 1)
     61             return 0;
     62 
     63         return get(0).getPresenceResId();
     64     }
     65 
     66     public String formatNames(String separator) {
     67         String[] names = new String[size()];
     68         int i = 0;
     69         for (Contact c : this) {
     70             names[i++] = c.getName();
     71         }
     72         return TextUtils.join(separator, names);
     73     }
     74 
     75     public String formatNamesAndNumbers(String separator) {
     76         String[] nans = new String[size()];
     77         int i = 0;
     78         for (Contact c : this) {
     79             nans[i++] = c.getNameAndNumber();
     80         }
     81         return TextUtils.join(separator, nans);
     82     }
     83 
     84     public String serialize() {
     85         return TextUtils.join(";", getNumbers());
     86     }
     87 
     88     public boolean containsEmail() {
     89         for (Contact c : this) {
     90             if (c.isEmail()) {
     91                 return true;
     92             }
     93         }
     94         return false;
     95     }
     96 
     97     public String[] getNumbers() {
     98         return getNumbers(false /* don't scrub for MMS address */);
     99     }
    100 
    101     public String[] getNumbers(boolean scrubForMmsAddress) {
    102         List<String> numbers = new ArrayList<String>();
    103         String number;
    104         for (Contact c : this) {
    105             number = c.getNumber();
    106 
    107             if (scrubForMmsAddress) {
    108                 // parse/scrub the address for valid MMS address. The returned number
    109                 // could be null if it's not a valid MMS address. We don't want to send
    110                 // a message to an invalid number, as the network may do its own stripping,
    111                 // and end up sending the message to a different number!
    112                 number = MessageUtils.parseMmsAddress(number);
    113             }
    114 
    115             // Don't add duplicate numbers. This can happen if a contact name has a comma.
    116             // Since we use a comma as a delimiter between contacts, the code will consider
    117             // the same recipient has been added twice. The recipients UI still works correctly.
    118             // It's easiest to just make sure we only send to the same recipient once.
    119             if (!TextUtils.isEmpty(number) && !numbers.contains(number)) {
    120                 numbers.add(number);
    121             }
    122         }
    123         return numbers.toArray(new String[numbers.size()]);
    124     }
    125 
    126     @Override
    127     public boolean equals(Object obj) {
    128         try {
    129             ContactList other = (ContactList)obj;
    130             // If they're different sizes, the contact
    131             // set is obviously different.
    132             if (size() != other.size()) {
    133                 return false;
    134             }
    135 
    136             // Make sure all the individual contacts are the same.
    137             for (Contact c : this) {
    138                 if (!other.contains(c)) {
    139                     return false;
    140                 }
    141             }
    142 
    143             return true;
    144         } catch (ClassCastException e) {
    145             return false;
    146         }
    147     }
    148 
    149     private void log(String msg) {
    150         Log.d(LogTag.TAG, "[ContactList] " + msg);
    151     }
    152 }
    153