am d9938208: Merge "Process inactive msgs if Mms app is relaunched"

* commit 'd99382089c5fb4830915cf09aee54884cb20b8ea':
  Process inactive msgs if Mms app is relaunched
diff --git a/src/com/android/mms/MmsApp.java b/src/com/android/mms/MmsApp.java
index 74f4043..9092a29 100644
--- a/src/com/android/mms/MmsApp.java
+++ b/src/com/android/mms/MmsApp.java
@@ -19,6 +19,7 @@
 
 import android.app.Application;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Configuration;
 import android.drm.DrmManagerClient;
 import android.location.Country;
@@ -34,6 +35,9 @@
 import com.android.mms.data.Conversation;
 import com.android.mms.layout.LayoutManager;
 import com.android.mms.transaction.MessagingNotification;
+import com.android.mms.transaction.MmsSystemEventReceiver;
+import com.android.mms.transaction.SmsReceiver;
+import com.android.mms.transaction.SmsReceiverService;
 import com.android.mms.util.DownloadManager;
 import com.android.mms.util.DraftCache;
 import com.android.mms.util.PduLoaderManager;
@@ -94,6 +98,23 @@
         LayoutManager.init(this);
         SmileyParser.init(this);
         MessagingNotification.init(this);
+
+        activePendingMessages();
+    }
+
+    /**
+     * Try to process all pending messages(which were interrupted by user, OOM, Mms crashing,
+     * etc...) when Mms app is (re)launched.
+     */
+    private void activePendingMessages() {
+        // For Mms: try to process all pending transactions if possible
+        MmsSystemEventReceiver.wakeUpService(this);
+
+        // For Sms: retry to send smses in outbox and queued box
+        sendBroadcast(new Intent(SmsReceiverService.ACTION_SEND_INACTIVE_MESSAGE,
+                null,
+                this,
+                SmsReceiver.class));
     }
 
     synchronized public static MmsApp getApplication() {
diff --git a/src/com/android/mms/transaction/MmsSystemEventReceiver.java b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
index f5ed3ac..9b78ea0 100644
--- a/src/com/android/mms/transaction/MmsSystemEventReceiver.java
+++ b/src/com/android/mms/transaction/MmsSystemEventReceiver.java
@@ -44,7 +44,7 @@
     private static final String TAG = "MmsSystemEventReceiver";
     private static ConnectivityManager mConnMgr = null;
 
-    private static void wakeUpService(Context context) {
+    public static void wakeUpService(Context context) {
         if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
             Log.v(TAG, "wakeUpService: start transaction service ...");
         }
diff --git a/src/com/android/mms/transaction/SmsReceiverService.java b/src/com/android/mms/transaction/SmsReceiverService.java
index 642b778..4985edf 100755
--- a/src/com/android/mms/transaction/SmsReceiverService.java
+++ b/src/com/android/mms/transaction/SmsReceiverService.java
@@ -81,7 +81,9 @@
     public static final String EXTRA_MESSAGE_SENT_SEND_NEXT ="SendNextMsg";
 
     public static final String ACTION_SEND_MESSAGE =
-        "com.android.mms.transaction.SEND_MESSAGE";
+            "com.android.mms.transaction.SEND_MESSAGE";
+    public static final String ACTION_SEND_INACTIVE_MESSAGE =
+            "com.android.mms.transaction.SEND_INACTIVE_MESSAGE";
 
     // This must match the column IDs below.
     private static final String[] SEND_PROJECTION = new String[] {
@@ -209,6 +211,8 @@
                     handleServiceStateChanged(intent);
                 } else if (ACTION_SEND_MESSAGE.endsWith(action)) {
                     handleSendMessage();
+                } else if (ACTION_SEND_INACTIVE_MESSAGE.equals(action)) {
+                    handleSendInactiveMessage();
                 }
             }
             // NOTE: We MUST not call stopSelf() directly, since we need to
@@ -231,6 +235,12 @@
         }
     }
 
+    private void handleSendInactiveMessage() {
+        // Inactive messages includes all messages in outbox and queued box.
+        moveOutboxMessagesToQueuedBox();
+        sendFirstQueuedMessage();
+    }
+
     public synchronized void sendFirstQueuedMessage() {
         boolean success = true;
         // get all the queued messages from the database
@@ -394,6 +404,24 @@
     }
 
     /**
+     * Move all messages that are in the outbox to the queued state
+     * @return The number of messages that were actually moved
+     */
+    private int moveOutboxMessagesToQueuedBox() {
+        ContentValues values = new ContentValues(1);
+
+        values.put(Sms.TYPE, Sms.MESSAGE_TYPE_QUEUED);
+
+        int messageCount = SqliteWrapper.update(
+                getApplicationContext(), getContentResolver(), Outbox.CONTENT_URI,
+                values, "type = " + Sms.MESSAGE_TYPE_OUTBOX, null);
+        if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE) || LogTag.DEBUG_SEND) {
+            Log.v(TAG, "moveOutboxMessagesToQueuedBox messageCount: " + messageCount);
+        }
+        return messageCount;
+    }
+
+    /**
      * Move all messages that are in the outbox to the failed state and set them to unread.
      * @return The number of messages that were actually moved
      */