Skip ab/6749736 in stage.

Merged-In: I0a6ebef1ef342258391bddeb34f5cc719ff28449
Change-Id: I08363195474384d1bc8a5f79b1fb81a724560b67
diff --git a/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java b/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
index 1a3eb63..a897ce0 100644
--- a/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
+++ b/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
@@ -335,7 +335,7 @@
                         selfId}
                     );
 
-            // Look for messages we cound send
+            // Look for messages we could send
             cursor = db.query(DatabaseHelper.MESSAGES_TABLE,
                     MessageData.getProjection(),
                     DatabaseHelper.MessageColumns.STATUS + " IN (?, ?) AND "
@@ -354,43 +354,34 @@
             values.put(DatabaseHelper.MessageColumns.STATUS,
                     MessageData.BUGLE_STATUS_OUTGOING_FAILED);
 
+            // Prior to L_MR1, isActiveSubscription is true always
+            boolean isActiveSubscription = true;
+            if (OsUtil.isAtLeastL_MR1()) {
+                final ParticipantData messageSelf =
+                        BugleDatabaseOperations.getExistingParticipant(db, selfId);
+                if (messageSelf == null || !messageSelf.isActiveSubscription()) {
+                    isActiveSubscription = false;
+                }
+            }
             while (cursor.moveToNext()) {
                 final MessageData message = new MessageData();
                 message.bind(cursor);
-                if (message.getInResendWindow(now)) {
-                    // If no messages currently sending
-                    if (sendingCnt == 0) {
-                        // Resend this message
-                        toSendMessageId = message.getMessageId();
-                        // Before queuing the message for resending, check if the message's self is
-                        // active. If not, switch back to the system's default subscription.
-                        if (OsUtil.isAtLeastL_MR1()) {
-                            final ParticipantData messageSelf = BugleDatabaseOperations
-                                    .getExistingParticipant(db, selfId);
-                            if (messageSelf == null || !messageSelf.isActiveSubscription()) {
-                                final ParticipantData defaultSelf = BugleDatabaseOperations
-                                        .getOrCreateSelf(db, PhoneUtils.getDefault()
-                                                .getDefaultSmsSubscriptionId());
-                                if (defaultSelf != null) {
-                                    message.bindSelfId(defaultSelf.getId());
-                                    final ContentValues selfValues = new ContentValues();
-                                    selfValues.put(MessageColumns.SELF_PARTICIPANT_ID,
-                                            defaultSelf.getId());
-                                    BugleDatabaseOperations.updateMessageRow(db,
-                                            message.getMessageId(), selfValues);
-                                    MessagingContentProvider.notifyMessagesChanged(
-                                            message.getConversationId());
-                                }
-                            }
-                        }
-                    }
-                    break;
-                } else {
+
+                // Mark this message as failed if the message's self is inactive or the message is
+                // outside of resend window
+                if (!isActiveSubscription || !message.getInResendWindow(now)) {
                     failedCnt++;
 
                     // Mark message as failed
                     BugleDatabaseOperations.updateMessageRow(db, message.getMessageId(), values);
                     MessagingContentProvider.notifyMessagesChanged(message.getConversationId());
+                } else {
+                    // If no messages currently sending
+                    if (sendingCnt == 0) {
+                        // Send this message
+                        toSendMessageId = message.getMessageId();
+                    }
+                    break;
                 }
             }
             db.setTransactionSuccessful();
diff --git a/src/com/android/messaging/mmslib/pdu/PduPersister.java b/src/com/android/messaging/mmslib/pdu/PduPersister.java
index d1175ab..6615d30 100644
--- a/src/com/android/messaging/mmslib/pdu/PduPersister.java
+++ b/src/com/android/messaging/mmslib/pdu/PduPersister.java
@@ -1415,11 +1415,11 @@
 
                     // For received messages (whether group MMS is enabled or not) we want to
                     // associate this message with the thread composed of all the recipients
-                    // EXCLUDING our own number. This includes the person who sent the
-                    // message (the FROM field above) in addition to the other people the message
-                    // was addressed TO (or CC fields to address group messaging compatibility
-                    // issues with devices that place numbers in this field). Typically our own
-                    // number is in the TO/CC field so we have to remove it in loadRecipients.
+                    // EXCLUDING our own number. This includes the person who sent the message
+                    // (the FROM field above) in addition to the other people the message was
+                    // addressed TO (or CC fields to address group messaging compatibility issues
+                    // with devices that place numbers in this field). Typically our own number is
+                    // in the TO/CC field so we have to remove it in checkAndLoadToCcRecipients.
                     checkAndLoadToCcRecipients(recipients, addressMap, subPhoneNumber);
                     break;
                 case PduHeaders.MESSAGE_TYPE_SEND_REQ:
@@ -1582,9 +1582,17 @@
                 }
             }
         }
+
+        // If selfNumber is unavailable and there is only a single address in all TO and CC, we can
+        // skip adding it into recipients as assuming it is my own phone number.
+        final boolean isSelfNumberUnavailable = TextUtils.isEmpty(selfNumber);
+        if (isSelfNumberUnavailable && numbers.size() == 1) {
+            return;
+        }
+
         for (final String number : numbers) {
             // Only add numbers which aren't my own number.
-            if (TextUtils.isEmpty(selfNumber) || !PhoneNumberUtils.compare(number, selfNumber)) {
+            if (isSelfNumberUnavailable || !PhoneNumberUtils.compare(number, selfNumber)) {
                 if (!recipients.contains(number)) {
                     // Only add numbers which aren't already included.
                     recipients.add(number);