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 getByNumbers(Iterable<String> numbers, boolean canBlock)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 getByNumbers(String semiSepNumbers, boolean canBlock, boolean replaceNumber)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 */ getByIds(String spaceSepIds, boolean canBlock)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 getPresenceResId()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 addListeners(UpdateListener l)66 public void addListeners(UpdateListener l) { 67 for (Contact c : this) { 68 c.addListener(l); 69 } 70 } 71 removeListeners(UpdateListener l)72 public void removeListeners(UpdateListener l) { 73 for (Contact c : this) { 74 c.removeListener(l); 75 } 76 } 77 formatNames(String separator)78 public String formatNames(String separator) { 79 String[] names = new String[size()]; 80 int i = 0; 81 for (Contact c : this) { 82 names[i++] = c.getName(); 83 } 84 return TextUtils.join(separator, names); 85 } 86 formatNamesAndNumbers(String separator)87 public String formatNamesAndNumbers(String separator) { 88 String[] nans = new String[size()]; 89 int i = 0; 90 for (Contact c : this) { 91 nans[i++] = c.getNameAndNumber(); 92 } 93 return TextUtils.join(separator, nans); 94 } 95 serialize()96 public String serialize() { 97 return TextUtils.join(";", getNumbers()); 98 } 99 containsEmail()100 public boolean containsEmail() { 101 for (Contact c : this) { 102 if (c.isEmail()) { 103 return true; 104 } 105 } 106 return false; 107 } 108 getNumbers()109 public String[] getNumbers() { 110 return getNumbers(false /* don't scrub for MMS address */); 111 } 112 getNumbers(boolean scrubForMmsAddress)113 public String[] getNumbers(boolean scrubForMmsAddress) { 114 List<String> numbers = new ArrayList<String>(); 115 String number; 116 for (Contact c : this) { 117 number = c.getNumber(); 118 119 if (scrubForMmsAddress) { 120 // parse/scrub the address for valid MMS address. The returned number 121 // could be null if it's not a valid MMS address. We don't want to send 122 // a message to an invalid number, as the network may do its own stripping, 123 // and end up sending the message to a different number! 124 number = MessageUtils.parseMmsAddress(number); 125 } 126 127 // Don't add duplicate numbers. This can happen if a contact name has a comma. 128 // Since we use a comma as a delimiter between contacts, the code will consider 129 // the same recipient has been added twice. The recipients UI still works correctly. 130 // It's easiest to just make sure we only send to the same recipient once. 131 if (!TextUtils.isEmpty(number) && !numbers.contains(number)) { 132 numbers.add(number); 133 } 134 } 135 return numbers.toArray(new String[numbers.size()]); 136 } 137 138 @Override equals(Object obj)139 public boolean equals(Object obj) { 140 try { 141 ContactList other = (ContactList)obj; 142 // If they're different sizes, the contact 143 // set is obviously different. 144 if (size() != other.size()) { 145 return false; 146 } 147 148 // Make sure all the individual contacts are the same. 149 for (Contact c : this) { 150 if (!other.contains(c)) { 151 return false; 152 } 153 } 154 155 return true; 156 } catch (ClassCastException e) { 157 return false; 158 } 159 } 160 log(String msg)161 private void log(String msg) { 162 Log.d(LogTag.TAG, "[ContactList] " + msg); 163 } 164 } 165