From cf1deeceabbdc487e988e8d1f2559e97a128b739 Mon Sep 17 00:00:00 2001 From: Jesse Young Date: Fri, 30 Sep 2011 23:40:54 -0700 Subject: [PATCH] queue incoming messages like outgoing messages --- src/org/envaya/sms/Inbox.java | 63 ++++++++++++++----- src/org/envaya/sms/IncomingMessage.java | 3 + src/org/envaya/sms/Outbox.java | 2 +- .../sms/receiver/IncomingMessageRetry.java | 2 +- src/org/envaya/sms/ui/PendingMessages.java | 3 +- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/org/envaya/sms/Inbox.java b/src/org/envaya/sms/Inbox.java index 92694e8..1c7cc84 100755 --- a/src/org/envaya/sms/Inbox.java +++ b/src/org/envaya/sms/Inbox.java @@ -4,12 +4,17 @@ import android.content.Intent; import android.net.Uri; import java.util.Collection; import java.util.HashMap; +import java.util.LinkedList; import java.util.Map; +import java.util.Queue; public class Inbox { private Map incomingMessages = new HashMap(); private App app; + private int numForwardingMessages = 0; + private Queue incomingQueue = new LinkedList(); + public Inbox(App app) { this.app = app; @@ -29,45 +34,68 @@ public class Inbox { } incomingMessages.put(uri, message); - - app.log("Received "+message.getDisplayType()+" from " + message.getFrom()); + app.log("Received "+message.getDescription()); - message.setProcessingState(IncomingMessage.ProcessingState.Forwarding); - message.tryForwardToServer(); - - notifyChanged(); + enqueueMessage(message); } - public synchronized void retryForwardMessage(IncomingMessage message) + public synchronized void enqueueMessage(IncomingMessage message) { IncomingMessage.ProcessingState state = message.getProcessingState(); if (state == IncomingMessage.ProcessingState.Scheduled - || state == IncomingMessage.ProcessingState.None) - { + || state == IncomingMessage.ProcessingState.None) + { + incomingQueue.add(message); + message.setProcessingState(IncomingMessage.ProcessingState.Queued); + notifyChanged(); + maybeDequeueMessage(); + } + } + + public synchronized void maybeDequeueMessage() + { + if (numForwardingMessages < 2) + { + IncomingMessage message = incomingQueue.poll(); + + if (message == null) + { + return; + } + + numForwardingMessages++; message.setProcessingState(IncomingMessage.ProcessingState.Forwarding); message.tryForwardToServer(); - notifyChanged(); - } - } + } + } public synchronized void deleteMessage(IncomingMessage message) { incomingMessages.remove(message.getUri()); - app.log("SMS from " + message.getFrom() + " deleted"); + + if (message.getProcessingState() == IncomingMessage.ProcessingState.Queued) + { + incomingQueue.remove(message); + } + + app.log(message.getDescription() + " deleted"); notifyChanged(); } public synchronized void messageFailed(IncomingMessage message) { - message.setProcessingState(IncomingMessage.ProcessingState.None); + message.setProcessingState(IncomingMessage.ProcessingState.None); if (message.scheduleRetry()) { message.setProcessingState(IncomingMessage.ProcessingState.Scheduled); } notifyChanged(); + + numForwardingMessages--; + maybeDequeueMessage(); } public synchronized void messageForwarded(IncomingMessage message) { @@ -88,7 +116,10 @@ public class Inbox { app.getMmsUtils().deleteFromInbox(mms); } } - } + + numForwardingMessages--; + maybeDequeueMessage(); + } private void notifyChanged() { @@ -97,7 +128,7 @@ public class Inbox { public synchronized void retryAll() { for (IncomingMessage message : incomingMessages.values()) { - retryForwardMessage(message); + enqueueMessage(message); } } diff --git a/src/org/envaya/sms/IncomingMessage.java b/src/org/envaya/sms/IncomingMessage.java index 15b37d8..3623a52 100755 --- a/src/org/envaya/sms/IncomingMessage.java +++ b/src/org/envaya/sms/IncomingMessage.java @@ -13,6 +13,7 @@ public abstract class IncomingMessage extends QueuedMessage { public enum ProcessingState { None, // not doing anything with this sms now... just sitting around + Queued, // waiting to forward to server Forwarding, // currently sending to server Scheduled, // waiting for a while before retrying after failure forwarding Forwarded @@ -90,6 +91,8 @@ public abstract class IncomingMessage extends QueuedMessage { { case Scheduled: return "scheduled retry"; + case Queued: + return "queued to forward"; case Forwarding: return "forwarding to server"; default: diff --git a/src/org/envaya/sms/Outbox.java b/src/org/envaya/sms/Outbox.java index 31d8814..4ebdf43 100755 --- a/src/org/envaya/sms/Outbox.java +++ b/src/org/envaya/sms/Outbox.java @@ -178,7 +178,7 @@ public class Outbox { } notifyMessageStatus(message, App.STATUS_FAILED, "deleted by user"); - app.log("SMS to " + message.getTo() + " deleted"); + app.log(message.getDescription() + " deleted"); notifyChanged(); } diff --git a/src/org/envaya/sms/receiver/IncomingMessageRetry.java b/src/org/envaya/sms/receiver/IncomingMessageRetry.java index a49c786..29945cd 100755 --- a/src/org/envaya/sms/receiver/IncomingMessageRetry.java +++ b/src/org/envaya/sms/receiver/IncomingMessageRetry.java @@ -25,6 +25,6 @@ public class IncomingMessageRetry extends BroadcastReceiver return; } - app.inbox.retryForwardMessage(message); + app.inbox.enqueueMessage(message); } } diff --git a/src/org/envaya/sms/ui/PendingMessages.java b/src/org/envaya/sms/ui/PendingMessages.java index 7c70f1a..9105340 100755 --- a/src/org/envaya/sms/ui/PendingMessages.java +++ b/src/org/envaya/sms/ui/PendingMessages.java @@ -19,7 +19,6 @@ import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; -import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import java.text.DateFormat; @@ -221,7 +220,7 @@ public class PendingMessages extends ListActivity { { if (message instanceof IncomingMessage) { - app.inbox.retryForwardMessage((IncomingMessage)message); + app.inbox.enqueueMessage((IncomingMessage)message); } else {