Permalink
Browse files

Use JSR-305 thread annotations, and fix a missing lock.

Change-Id: I74ef136c117b8cdcbca0cab0194f65e5fe1b401c
  • Loading branch information...
1 parent 04a2774 commit ccc3a9219b3e1d1cff5dde3c13ecea1bb06226c3 @bradfitz bradfitz committed May 14, 2010
Showing with 25 additions and 11 deletions.
  1. +1 −1 Android.mk
  2. +24 −10 src/com/android/mms/data/RecipientIdCache.java
View
@@ -12,7 +12,7 @@ LOCAL_PACKAGE_NAME := Mms
# Builds against the public SDK
#LOCAL_SDK_VERSION := current
-LOCAL_STATIC_JAVA_LIBRARIES += android-common
+LOCAL_STATIC_JAVA_LIBRARIES += android-common jsr305
LOCAL_REQUIRED_MODULES := SoundRecorder
@@ -5,6 +5,9 @@
import java.util.List;
import java.util.Map;
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+
import android.content.Context;
import android.content.ContentValues;
import android.content.ContentUris;
@@ -17,6 +20,7 @@
import com.android.mms.LogTag;
import android.database.sqlite.SqliteWrapper;
+@ThreadSafe
public class RecipientIdCache {
private static final boolean LOCAL_DEBUG = false;
private static final String TAG = "Mms/cache";
@@ -29,7 +33,10 @@
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 {
@@ -112,7 +119,7 @@ public static void fill() {
if (Log.isLoggable(LogTag.THREAD_CACHE, Log.VERBOSE)) {
dump();
}
-
+
fill();
number = sInstance.mCache.get(longId);
}
@@ -144,17 +151,24 @@ public static void updateNumbers(long threadId, ContactList contacts) {
}
String number1 = contact.getNumber();
- String number2 = sInstance.mCache.get(recipientId);
+ boolean needsDbUpdate = false;
+ synchronized (sInstance) {
+ String number2 = sInstance.mCache.get(recipientId);
- if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
- Log.d(TAG, "[RecipientIdCache] updateNumbers: comparing " + number1 +
- " with " + number2);
- }
+ if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
+ Log.d(TAG, "[RecipientIdCache] updateNumbers: comparing " + number1 +
+ " with " + 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);
+ // 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);
}
}

0 comments on commit ccc3a92

Please sign in to comment.