This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
RecipientIdCache.java
281 lines (238 loc) · 9.57 KB
/
RecipientIdCache.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package com.android.mms.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SqliteWrapper;
import android.net.Uri;
import android.provider.Telephony;
import android.text.TextUtils;
import android.util.Log;
import com.android.mms.LogTag;
@ThreadSafe
public class RecipientIdCache {
private static final boolean LOCAL_DEBUG = false;
private static final String TAG = LogTag.TAG;
private static Uri sAllCanonical =
Uri.parse("content://mms-sms/canonical-addresses");
private static Uri sSingleCanonicalAddressUri =
Uri.parse("content://mms-sms/canonical-address");
private static RecipientIdCache sInstance;
static RecipientIdCache getInstance() { return sInstance; }
@GuardedBy("this")
private final Map<Long, String> mCache;
private final Context mContext;
public static class Entry {
public long id;
public String number;
public Entry(long id, String number) {
this.id = id;
this.number = number;
}
};
static void init(Context context) {
sInstance = new RecipientIdCache(context);
new Thread(new Runnable() {
public void run() {
fill();
}
}, "RecipientIdCache.init").start();
}
RecipientIdCache(Context context) {
mCache = new HashMap<Long, String>();
mContext = context;
}
public static void fill() {
if (LogTag.VERBOSE || Log.isLoggable(LogTag.THREAD_CACHE, Log.VERBOSE)) {
LogTag.debug("[RecipientIdCache] fill: begin");
}
Context context = sInstance.mContext;
Cursor c = SqliteWrapper.query(context, context.getContentResolver(),
sAllCanonical, null, null, null, null);
if (c == null) {
Log.w(TAG, "null Cursor in fill()");
return;
}
try {
synchronized (sInstance) {
// Technically we don't have to clear this because the stupid
// canonical_addresses table is never GC'ed.
sInstance.mCache.clear();
while (c.moveToNext()) {
// TODO: don't hardcode the column indices
long id = c.getLong(0);
String number = c.getString(1);
sInstance.mCache.put(id, number);
}
}
} finally {
c.close();
}
if (LogTag.VERBOSE || Log.isLoggable(LogTag.THREAD_CACHE, Log.VERBOSE)) {
LogTag.debug("[RecipientIdCache] fill: finished");
dump();
}
}
public static List<Entry> getAddresses(String spaceSepIds) {
synchronized (sInstance) {
List<Entry> numbers = new ArrayList<Entry>();
String[] ids = spaceSepIds.split(" ");
for (String id : ids) {
long longId;
try {
longId = Long.parseLong(id);
} catch (NumberFormatException ex) {
// skip this id
continue;
}
String number = sInstance.mCache.get(longId);
if (number == null) {
Log.w(TAG, "RecipientId " + longId + " not in cache!");
if (Log.isLoggable(LogTag.THREAD_CACHE, Log.VERBOSE)) {
dump();
}
fill();
number = sInstance.mCache.get(longId);
}
if (TextUtils.isEmpty(number)) {
Log.w(TAG, "RecipientId " + longId + " has empty number!");
} else {
numbers.add(new Entry(longId, number));
}
}
return numbers;
}
}
public static void updateNumbers(long threadId, ContactList contacts) {
long recipientId = 0;
for (Contact contact : contacts) {
if (contact.isNumberModified()) {
contact.setIsNumberModified(false);
} else {
// if the contact's number wasn't modified, don't bother.
continue;
}
recipientId = contact.getRecipientId();
if (recipientId == 0) {
continue;
}
String number1 = contact.getNumber();
boolean needsDbUpdate = false;
synchronized (sInstance) {
String number2 = sInstance.mCache.get(recipientId);
if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.d(TAG, "[RecipientIdCache] updateNumbers: contact=" + contact +
", wasModified=true, recipientId=" + recipientId);
Log.d(TAG, " contact.getNumber=" + number1 +
", sInstance.mCache.get(recipientId)=" + number2);
}
// if the numbers don't match, let's update the RecipientIdCache's number
// with the new number in the contact.
if (!number1.equalsIgnoreCase(number2)) {
sInstance.mCache.put(recipientId, number1);
needsDbUpdate = true;
}
}
if (needsDbUpdate) {
// Do this without the lock held.
sInstance.updateCanonicalAddressInDb(recipientId, number1);
}
}
}
private void updateCanonicalAddressInDb(long id, String number) {
if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.d(TAG, "[RecipientIdCache] updateCanonicalAddressInDb: id=" + id +
", number=" + number);
}
final ContentValues values = new ContentValues();
values.put(Telephony.CanonicalAddressesColumns.ADDRESS, number);
final StringBuilder buf = new StringBuilder(Telephony.CanonicalAddressesColumns._ID);
buf.append('=').append(id);
final Uri uri = ContentUris.withAppendedId(sSingleCanonicalAddressUri, id);
final ContentResolver cr = mContext.getContentResolver();
// We're running on the UI thread so just fire & forget, hope for the best.
// (We were ignoring the return value anyway...)
new Thread("updateCanonicalAddressInDb") {
public void run() {
cr.update(uri, values, buf.toString(), null);
}
}.start();
}
public static void dump() {
// Only dump user private data if we're in special debug mode
synchronized (sInstance) {
Log.d(TAG, "*** Recipient ID cache dump ***");
for (Long id : sInstance.mCache.keySet()) {
Log.d(TAG, id + ": " + sInstance.mCache.get(id));
}
}
}
public static void canonicalTableDump() {
Log.d(TAG, "**** Dump of canoncial_addresses table ****");
Context context = sInstance.mContext;
Cursor c = SqliteWrapper.query(context, context.getContentResolver(),
sAllCanonical, null, null, null, null);
if (c == null) {
Log.w(TAG, "null Cursor in content://mms-sms/canonical-addresses");
}
try {
while (c.moveToNext()) {
// TODO: don't hardcode the column indices
long id = c.getLong(0);
String number = c.getString(1);
Log.d(TAG, "id: " + id + " number: " + number);
}
} finally {
c.close();
}
}
/**
* getSingleNumberFromCanonicalAddresses looks up the recipientId in the canonical_addresses
* table and returns the associated number or email address.
* @param context needed for the ContentResolver
* @param recipientId of the contact to look up
* @return phone number or email address of the recipientId
*/
public static String getSingleAddressFromCanonicalAddressInDb(final Context context,
final String recipientId) {
Cursor c = SqliteWrapper.query(context, context.getContentResolver(),
ContentUris.withAppendedId(sSingleCanonicalAddressUri, Long.parseLong(recipientId)),
null, null, null, null);
if (c == null) {
LogTag.warn(TAG, "null Cursor looking up recipient: " + recipientId);
return null;
}
try {
if (c.moveToFirst()) {
String number = c.getString(0);
return number;
}
} finally {
c.close();
}
return null;
}
// used for unit tests
public static void insertCanonicalAddressInDb(final Context context, String number) {
if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
Log.d(TAG, "[RecipientIdCache] insertCanonicalAddressInDb: number=" + number);
}
final ContentValues values = new ContentValues();
values.put(Telephony.CanonicalAddressesColumns.ADDRESS, number);
final ContentResolver cr = context.getContentResolver();
// We're running on the UI thread so just fire & forget, hope for the best.
// (We were ignoring the return value anyway...)
new Thread("insertCanonicalAddressInDb") {
public void run() {
cr.insert(sAllCanonical, values);
}
}.start();
}
}