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