Merge "Guarded the getActivity() call" into jb-mr1-dev
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index cde1215..3dfaf8d 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -458,6 +458,7 @@
         Collapser.collapseList(mEmailEntries);
         Collapser.collapseList(mPostalEntries);
         Collapser.collapseList(mImEntries);
+        Collapser.collapseList(mEventEntries);
 
         mIsUniqueNumber = mPhoneEntries.size() == 1;
         mIsUniqueEmail = mEmailEntries.size() == 1;
@@ -573,15 +574,6 @@
                     PhoneDataItem phone = (PhoneDataItem) dataItem;
                     // Build phone entries
                     entry.data = phone.getFormattedPhoneNumber();
-                    if (entry.data == null) {
-                        // This case happens when the quick contact was opened from the contact
-                        // list, and then, the user touches the quick contact image and brings the
-                        // user to the detail card.  In this case, the Contact object that was
-                        // loaded from quick contacts does not contain the formatted phone number,
-                        // so it must be loaded here.
-                        phone.computeFormattedPhoneNumber(mDefaultCountryIso);
-                        entry.data = phone.getFormattedPhoneNumber();
-                    }
                     final Intent phoneIntent = mHasPhone ?
                             ContactsUtils.getCallIntent(entry.data) : null;
                     final Intent smsIntent = mHasSms ? new Intent(Intent.ACTION_SENDTO,
diff --git a/src/com/android/contacts/model/ContactLoader.java b/src/com/android/contacts/model/ContactLoader.java
index 0b7e40d..f9f630d 100644
--- a/src/com/android/contacts/model/ContactLoader.java
+++ b/src/com/android/contacts/model/ContactLoader.java
@@ -63,6 +63,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -332,6 +333,9 @@
                 if (mLoadStreamItems && result.getStreamItems() == null) {
                     loadStreamItems(result);
                 }
+                if (mComputeFormattedPhoneNumber) {
+                    computeFormattedPhoneNumbers(result);
+                }
                 if (!resultIsCached) loadPhotoBinaryData(result);
 
                 // Note ME profile should never have "Add connection"
@@ -384,7 +388,6 @@
                 if (!cursor.isNull(ContactQuery.DATA_ID)) {
                     ContentValues data = loadDataValues(cursor);
                     final DataItem item = rawContact.addDataItemValues(data);
-                    processDataItem(item);
 
                     if (!cursor.isNull(ContactQuery.PRESENCE)
                             || !cursor.isNull(ContactQuery.STATUS)) {
@@ -405,19 +408,6 @@
     }
 
     /**
-     * Performs any further computations on the data item
-     */
-    private void processDataItem(DataItem dataItem) {
-        if (dataItem instanceof PhoneDataItem) {
-            if (mComputeFormattedPhoneNumber) {
-                final PhoneDataItem phoneDataItem = (PhoneDataItem) dataItem;
-                final String defaultCountryIso = ContactsUtils.getCurrentCountryIso(getContext());
-                phoneDataItem.computeFormattedPhoneNumber(defaultCountryIso);
-            }
-        }
-    }
-
-    /**
      * Looks for the photo data item in entities. If found, creates a new Bitmap instance. If
      * not found, returns null
      */
@@ -808,6 +798,29 @@
                 .build());
     }
 
+    /**
+     * Iterates over all data items that represent phone numbers are tries to calculate a formatted
+     * number. This function can safely be called several times as no unformatted data is
+     * overwritten
+     */
+    private void computeFormattedPhoneNumbers(Contact contactData) {
+        final String countryIso = ContactsUtils.getCurrentCountryIso(getContext());
+        final ImmutableList<RawContact> rawContacts = contactData.getRawContacts();
+        final int rawContactCount = rawContacts.size();
+        for (int rawContactIndex = 0; rawContactIndex < rawContactCount; rawContactIndex++) {
+            final RawContact rawContact = rawContacts.get(rawContactIndex);
+            final List<DataItem> dataItems = rawContact.getDataItems();
+            final int dataCount = dataItems.size();
+            for (int dataIndex = 0; dataIndex < dataCount; dataIndex++) {
+                final DataItem dataItem = dataItems.get(dataIndex);
+                if (dataItem instanceof PhoneDataItem) {
+                    final PhoneDataItem phoneDataItem = (PhoneDataItem) dataItem;
+                    phoneDataItem.computeFormattedPhoneNumber(countryIso);
+                }
+            }
+        }
+    }
+
     @Override
     public void deliverResult(Contact result) {
         unregisterObserver();
@@ -895,12 +908,13 @@
     public void upgradeToFullContact() {
         // Everything requested already? Nothing to do, so let's bail out
         if (mLoadGroupMetaData && mLoadInvitableAccountTypes && mLoadStreamItems
-                && mPostViewNotification) return;
+                && mPostViewNotification && mComputeFormattedPhoneNumber) return;
 
         mLoadGroupMetaData = true;
         mLoadInvitableAccountTypes = true;
         mLoadStreamItems = true;
         mPostViewNotification = true;
+        mComputeFormattedPhoneNumber = true;
 
         // Cache the current result, so that we only load the "missing" parts of the contact.
         cacheResult();