Rescheduling pending messages after outside window failure

No more rescheduling for pending messages after DownloadMmsAction is
failed caused by outside window. Any pending download messages will be
remained as the DOWNLOADING state without actual downloading operations
and sending a DEFERRED M-NotifyResp.ind even.
It will reschedule pending messages whenever sending or downloading is
failed including outside window failure case. And it also removes dup
scheduling and fix some typo.

Test: Manual

Change-Id: Ie37088347c69f25d838875830fc65ce7727fefe6
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
diff --git a/src/com/android/messaging/datamodel/action/ProcessDownloadedMmsAction.java b/src/com/android/messaging/datamodel/action/ProcessDownloadedMmsAction.java
index 07e2cfb..60d3e35 100644
--- a/src/com/android/messaging/datamodel/action/ProcessDownloadedMmsAction.java
+++ b/src/com/android/messaging/datamodel/action/ProcessDownloadedMmsAction.java
@@ -83,7 +83,7 @@
     // Set when message downloaded by us (legacy)
     private static final String KEY_STATUS = "status";
     private static final String KEY_RAW_STATUS = "raw_status";
-    private static final String KEY_MMS_URI =  "mms_uri";
+    private static final String KEY_MMS_URI = "mms_uri";
 
     // Used to send a deferred response in response to auto-download failure
     private static final String KEY_SEND_DEFERRED_RESP_STATUS = "send_deferred_resp_status";
@@ -329,6 +329,8 @@
         if (response == null) {
             // No message download to process; doBackgroundWork sent a notify deferred response
             Assert.isTrue(actionParameters.getBoolean(KEY_SEND_DEFERRED_RESP_STATUS));
+            ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(
+                    true /* failed */, this);
             return null;
         }
 
@@ -343,7 +345,9 @@
 
         final int subId = actionParameters.getInt(KEY_SUB_ID, ParticipantData.DEFAULT_SELF_SUB_ID);
         // If we were trying to auto-download but have failed need to send the deferred response
-        if (autoDownload && message == null && status == MmsUtils.MMS_REQUEST_MANUAL_RETRY) {
+        final boolean needToSendDeferredResp =
+                autoDownload && (status == MmsUtils.MMS_REQUEST_MANUAL_RETRY);
+        if (needToSendDeferredResp) {
             final String transactionId = actionParameters.getString(KEY_TRANSACTION_ID);
             final String contentLocation = actionParameters.getString(KEY_CONTENT_LOCATION);
             sendDeferredRespStatus(messageId, transactionId, contentLocation, subId);
@@ -373,7 +377,11 @@
         }
 
         final boolean failed = (messageUri == null);
-        ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(failed, this);
+        // Scheduling pending messages. If auto downloading is failed and it needs to send the
+        // deferred response, Skip it here and it will be scheduled after sending the response.
+        if (!needToSendDeferredResp) {
+            ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(failed, this);
+        }
         if (failed) {
             BugleNotifications.update(false, BugleNotifications.UPDATE_ERRORS);
         }
diff --git a/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java b/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
index f164148..1a3eb63 100644
--- a/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
+++ b/src/com/android/messaging/datamodel/action/ProcessPendingMessagesAction.java
@@ -301,12 +301,16 @@
         unregister(subId);
 
         if (PhoneUtils.getDefault().isDefaultSmsApp()) {
-            queueActions(this);
+            if (!queueActions(this)) {
+                LogUtil.v(TAG, "ProcessPendingMessagesAction: rescheduling");
+                // TODO: Need to clear retry count here?
+                scheduleProcessPendingMessagesAction(true /* failed */, this);
+            }
         } else {
             if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                 LogUtil.v(TAG, "ProcessPendingMessagesAction: Not default SMS app; rescheduling");
             }
-            scheduleProcessPendingMessagesAction(true, this);
+            scheduleProcessPendingMessagesAction(true /* failed */, this);
         }
 
         return null;
diff --git a/src/com/android/messaging/datamodel/action/ProcessSentMessageAction.java b/src/com/android/messaging/datamodel/action/ProcessSentMessageAction.java
index f408e47..489a0f1 100644
--- a/src/com/android/messaging/datamodel/action/ProcessSentMessageAction.java
+++ b/src/com/android/messaging/datamodel/action/ProcessSentMessageAction.java
@@ -207,6 +207,8 @@
         if (message == null) {
             LogUtil.w(TAG, "ProcessSentMessageAction: Sent message " + messageId
                     + " missing from local database");
+            ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(
+                    true /* failed */, processingAction);
             return;
         }
         final String conversationId = message.getConversationId();
diff --git a/src/com/android/messaging/datamodel/action/SendMessageAction.java b/src/com/android/messaging/datamodel/action/SendMessageAction.java
index d7ebe8f..35e6280 100644
--- a/src/com/android/messaging/datamodel/action/SendMessageAction.java
+++ b/src/com/android/messaging/datamodel/action/SendMessageAction.java
@@ -44,7 +44,6 @@
  * Action used to send an outgoing message. It writes MMS messages to the telephony db
  * ({@link InsertNewMessageAction}) writes SMS messages to the telephony db). It also
  * initiates the actual sending. It will all be used for re-sending a failed message.
- * NOTE: This action must queue a ProcessPendingMessagesAction when it is done (success or failure).
  * <p>
  * This class is public (not package-private) because the SMS/MMS (e.g. MmsUtils) classes need to
  * access the EXTRA_* fields for setting up the 'sent' pending intent.
@@ -294,9 +293,6 @@
                 MmsUtils.MMS_REQUEST_MANUAL_RETRY, MessageData.RAW_TELEPHONY_STATUS_UNDEFINED,
                 isSms, this, subId, resultCode, httpStatusCode);
 
-        // Whether we succeeded or failed we will check and maybe schedule some more work
-        ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(true, this);
-
         return null;
     }
 
diff --git a/src/com/android/messaging/datamodel/data/MessageData.java b/src/com/android/messaging/datamodel/data/MessageData.java
index a8c8bc1..b2cab48 100644
--- a/src/com/android/messaging/datamodel/data/MessageData.java
+++ b/src/com/android/messaging/datamodel/data/MessageData.java
@@ -582,7 +582,7 @@
             // primary user.
             return false;
         }
-        // Should show option for manual download iff status is manual download or failed
+        // Should show option for manual download if status is manual download or failed
         return (status == BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED ||
                 status == BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD ||
                 // If debug is enabled, allow to download an expired or unavailable message.
@@ -596,7 +596,7 @@
             // primary user.
             return false;
         }
-        // Can download iff status is retrying auto/manual downloading
+        // Can download if status is retrying auto/manual downloading
         return (mStatus == BUGLE_STATUS_INCOMING_RETRYING_MANUAL_DOWNLOAD ||
                 mStatus == BUGLE_STATUS_INCOMING_RETRYING_AUTO_DOWNLOAD);
     }
@@ -607,7 +607,7 @@
             // primary user.
             return false;
         }
-        // Can redownload iff status is manual download not started or download failed
+        // Can redownload if status is manual download not started or download failed
         return (mStatus == BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED ||
                 mStatus == BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD ||
                 // If debug is enabled, allow to download an expired or unavailable message.
@@ -616,12 +616,12 @@
     }
 
     static boolean getShowResendMessage(final int status) {
-        // Should show option to resend iff status is failed
+        // Should show option to resend if status is failed
         return (status == BUGLE_STATUS_OUTGOING_FAILED);
     }
 
     static boolean getOneClickResendMessage(final int status, final int rawStatus) {
-        // Should show option to resend iff status is failed
+        // Should show option to resend if status is failed
         return (status == BUGLE_STATUS_OUTGOING_FAILED
                 && rawStatus == RAW_TELEPHONY_STATUS_UNDEFINED);
     }