Speedup contact name resolution

The first time Messaging is launched, a background thread loads
the conversations threads, which loads the contact info cache. While
this is happening, the ConversationList activity starts and the
first thing it does is clear the contact cache that is being built
in the background. Don't clear the cache if it's currently being
built. Fixes bug 2167799.

Change-Id: Id8a9345caaf7905d1f4bcc18ee45d11cb860d293
diff --git a/src/com/android/mms/data/Conversation.java b/src/com/android/mms/data/Conversation.java
index 7e13564..e5d13e9 100644
--- a/src/com/android/mms/data/Conversation.java
+++ b/src/com/android/mms/data/Conversation.java
@@ -64,6 +64,7 @@
     private boolean mHasError;          // True if any message is in an error state.
 
     private static ContentValues mReadContentValues;
+    private static boolean mLoadingThreads;
 
 
     private Conversation(Context context) {
@@ -650,11 +651,20 @@
         }).start();
     }
 
+    /**
+     * Are we in the process of loading and caching all the threads?.
+     */
+   public static boolean loadingThreads() {
+        return mLoadingThreads;
+    }
+
     private static void cacheAllThreads(Context context) {
         synchronized (Cache.getInstance()) {
             if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                 LogTag.debug("[Conversation] cacheAllThreads");
             }
+            mLoadingThreads = true;
+
             // Keep track of what threads are now on disk so we
             // can discard anything removed from the cache.
             HashSet<Long> threadsOnDisk = new HashSet<Long>();
@@ -687,6 +697,7 @@
                 }
             } finally {
                 c.close();
+                mLoadingThreads = false;
             }
 
             // Purge the cache of threads that no longer exist on disk.
diff --git a/src/com/android/mms/ui/ConversationList.java b/src/com/android/mms/ui/ConversationList.java
index 38e25c8..1ac441f 100644
--- a/src/com/android/mms/ui/ConversationList.java
+++ b/src/com/android/mms/ui/ConversationList.java
@@ -231,9 +231,13 @@
         // we invalidate the contact cache here because we want to get updated presence
         // and any contact changes. We don't invalidate the cache by observing presence and contact
         // changes (since that's too untargeted), so as a tradeoff we do it here.
+        // If we're in the middle of the app initialization where we're loading the conversation
+        // threads, don't invalidate the cache because we're in the process of building it.
         // TODO: think of a better way to invalidate cache more surgically or based on actual
         // TODO: changes we care about
-        Contact.invalidateCache();
+        if (!Conversation.loadingThreads()) {
+            Contact.invalidateCache();
+        }
     }
 
     protected void privateOnStart() {