bug #2236535: use an initial bad address (from a merged contact) would lead to permanent failure for the messaging thread even after a good number is used.

- when the user selects/types a new number in the recipients editor, and we find a cached contact, set the contact's number to be the newly selected number. This will allow the user to correct the address, at least until the next time MMS rebuilds the contact cache with the bad number stored in the database. This will get us to par with the Cupcake behavior. Unfortunately, after MMS app is relaunched, the thread would use the bad initial adddress again (loaded from the 'canonical_address' table in the db). That's a much harder, riskier fix, so deferring to later.

Change-Id: If32958482b14848bd770cff50e4736457c47ac8a
diff --git a/src/com/android/mms/data/Contact.java b/src/com/android/mms/data/Contact.java
index bb0f013..054e2ac 100644
--- a/src/com/android/mms/data/Contact.java
+++ b/src/com/android/mms/data/Contact.java
@@ -74,7 +74,7 @@
     private Contact(String number) {
         mNumber = number;
         mName = "";
-        mNameAndNumber = formatNameAndNumber(mName, mNumber);
+        updateNameAndNumber();
         mLabel = "";
         mPersonId = 0;
         mPresenceResId = 0;
@@ -179,7 +179,7 @@
     private static boolean handleLocalNumber(Contact c) {
         if (MessageUtils.isLocalNumber(c.mNumber)) {
             c.mName = Cache.getContext().getString(com.android.internal.R.string.me);
-            c.mNameAndNumber = formatNameAndNumber(c.mName, c.mNumber);
+            c.updateNameAndNumber();
             return true;
         }
         return false;
@@ -247,7 +247,7 @@
 
                 //c.mNumber = entry.phoneNumber;
                 c.mName = entry.name;
-                c.mNameAndNumber = formatNameAndNumber(c.mName, c.mNumber);
+                c.updateNameAndNumber();
                 c.mLabel = entry.phoneLabel;
                 c.mPersonId = entry.person_id;
                 c.mPresenceResId = entry.presenceResId;
@@ -282,6 +282,11 @@
         return mNumber;
     }
 
+    public synchronized void setNumber(String number) {
+        mNumber = number;
+        updateNameAndNumber();
+    }
+
     public synchronized String getName() {
         if (TextUtils.isEmpty(mName)) {
             return mNumber;
@@ -294,6 +299,10 @@
         return mNameAndNumber;
     }
 
+    private void updateNameAndNumber() {
+        mNameAndNumber = formatNameAndNumber(mName, mNumber);
+    }
+
     public synchronized String getLabel() {
         return mLabel;
     }
diff --git a/src/com/android/mms/data/Conversation.java b/src/com/android/mms/data/Conversation.java
index a9d7bfa..95be170 100644
--- a/src/com/android/mms/data/Conversation.java
+++ b/src/com/android/mms/data/Conversation.java
@@ -194,6 +194,7 @@
      * The recipient list of this conversation can be empty if the results
      * were not in cache.
      */
+    // TODO: check why can't load a cached Conversation object here.
     public static Conversation from(Context context, Cursor cursor) {
         return new Conversation(context, cursor, false);
     }
@@ -645,7 +646,7 @@
     private static void cacheAllThreads(Context context) {
         synchronized (Cache.getInstance()) {
             if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
-                LogTag.debug("cacheAllThreads called");
+                LogTag.debug("[Conversation] cacheAllThreads");
             }
             // Keep track of what threads are now on disk so we
             // can discard anything removed from the cache.
diff --git a/src/com/android/mms/transaction/SmsMessageSender.java b/src/com/android/mms/transaction/SmsMessageSender.java
index 747695f..36eefd1 100644
--- a/src/com/android/mms/transaction/SmsMessageSender.java
+++ b/src/com/android/mms/transaction/SmsMessageSender.java
@@ -138,8 +138,8 @@
             }
 
             if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
-                log("sendMessage: address " + i + ", threadId=" + mThreadId + ", uri=" + uri + 
-                        "msgs.count=" + messageCount);
+                log("sendMessage: address[" + i + "]=" + mDests[i] + ", threadId=" + mThreadId + ", uri=" + uri + 
+                        ", msgs.count=" + messageCount);
             }
 
             smsManager.sendMultipartTextMessage(
diff --git a/src/com/android/mms/ui/ComposeMessageActivity.java b/src/com/android/mms/ui/ComposeMessageActivity.java
index 63436c0..5217c72 100644
--- a/src/com/android/mms/ui/ComposeMessageActivity.java
+++ b/src/com/android/mms/ui/ComposeMessageActivity.java
@@ -1579,7 +1579,7 @@
             public void onFocusChange(View v, boolean hasFocus) {
                 if (!hasFocus) {
                     RecipientsEditor editor = (RecipientsEditor) v;
-                    bindToContactHeaderWidget(editor.getContacts());
+                    bindToContactHeaderWidget(editor.constructContactsFromInput());
                 }
             }
         });
diff --git a/src/com/android/mms/ui/RecipientsEditor.java b/src/com/android/mms/ui/RecipientsEditor.java
index 16f10a0..f897792 100644
--- a/src/com/android/mms/ui/RecipientsEditor.java
+++ b/src/com/android/mms/ui/RecipientsEditor.java
@@ -125,11 +125,13 @@
         return mTokenizer.getNumbers();
     }
 
-    public ContactList getContacts() {
+    public ContactList constructContactsFromInput() {
         List<String> numbers = mTokenizer.getNumbers();
         ContactList list = new ContactList();
         for (String number : numbers) {
-            list.add(Contact.get(number, false));
+            Contact contact = Contact.get(number, false);
+            contact.setNumber(number);
+            list.add(contact);
         }
         return list;
     }