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