5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-11-09 10:20:25 +00:00

queue incoming messages like outgoing messages

This commit is contained in:
Jesse Young 2011-09-30 23:40:54 -07:00
parent d7f803e60e
commit cf1deeceab
5 changed files with 53 additions and 20 deletions

View File

@ -4,12 +4,17 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.Queue;
public class Inbox { public class Inbox {
private Map<Uri, IncomingMessage> incomingMessages = new HashMap<Uri, IncomingMessage>(); private Map<Uri, IncomingMessage> incomingMessages = new HashMap<Uri, IncomingMessage>();
private App app; private App app;
private int numForwardingMessages = 0;
private Queue<IncomingMessage> incomingQueue = new LinkedList<IncomingMessage>();
public Inbox(App app) public Inbox(App app)
{ {
this.app = app; this.app = app;
@ -29,25 +34,39 @@ public class Inbox {
} }
incomingMessages.put(uri, message); incomingMessages.put(uri, message);
app.log("Received "+message.getDescription());
app.log("Received "+message.getDisplayType()+" from " + message.getFrom()); enqueueMessage(message);
message.setProcessingState(IncomingMessage.ProcessingState.Forwarding);
message.tryForwardToServer();
notifyChanged();
} }
public synchronized void retryForwardMessage(IncomingMessage message) public synchronized void enqueueMessage(IncomingMessage message)
{ {
IncomingMessage.ProcessingState state = message.getProcessingState(); IncomingMessage.ProcessingState state = message.getProcessingState();
if (state == IncomingMessage.ProcessingState.Scheduled 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.setProcessingState(IncomingMessage.ProcessingState.Forwarding);
message.tryForwardToServer(); message.tryForwardToServer();
notifyChanged(); notifyChanged();
} }
} }
@ -55,7 +74,13 @@ public class Inbox {
public synchronized void deleteMessage(IncomingMessage message) public synchronized void deleteMessage(IncomingMessage message)
{ {
incomingMessages.remove(message.getUri()); 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(); notifyChanged();
} }
@ -68,6 +93,9 @@ public class Inbox {
message.setProcessingState(IncomingMessage.ProcessingState.Scheduled); message.setProcessingState(IncomingMessage.ProcessingState.Scheduled);
} }
notifyChanged(); notifyChanged();
numForwardingMessages--;
maybeDequeueMessage();
} }
public synchronized void messageForwarded(IncomingMessage message) { public synchronized void messageForwarded(IncomingMessage message) {
@ -88,6 +116,9 @@ public class Inbox {
app.getMmsUtils().deleteFromInbox(mms); app.getMmsUtils().deleteFromInbox(mms);
} }
} }
numForwardingMessages--;
maybeDequeueMessage();
} }
private void notifyChanged() private void notifyChanged()
@ -97,7 +128,7 @@ public class Inbox {
public synchronized void retryAll() { public synchronized void retryAll() {
for (IncomingMessage message : incomingMessages.values()) { for (IncomingMessage message : incomingMessages.values()) {
retryForwardMessage(message); enqueueMessage(message);
} }
} }

View File

@ -13,6 +13,7 @@ public abstract class IncomingMessage extends QueuedMessage {
public enum ProcessingState public enum ProcessingState
{ {
None, // not doing anything with this sms now... just sitting around None, // not doing anything with this sms now... just sitting around
Queued, // waiting to forward to server
Forwarding, // currently sending to server Forwarding, // currently sending to server
Scheduled, // waiting for a while before retrying after failure forwarding Scheduled, // waiting for a while before retrying after failure forwarding
Forwarded Forwarded
@ -90,6 +91,8 @@ public abstract class IncomingMessage extends QueuedMessage {
{ {
case Scheduled: case Scheduled:
return "scheduled retry"; return "scheduled retry";
case Queued:
return "queued to forward";
case Forwarding: case Forwarding:
return "forwarding to server"; return "forwarding to server";
default: default:

View File

@ -178,7 +178,7 @@ public class Outbox {
} }
notifyMessageStatus(message, App.STATUS_FAILED, notifyMessageStatus(message, App.STATUS_FAILED,
"deleted by user"); "deleted by user");
app.log("SMS to " + message.getTo() + " deleted"); app.log(message.getDescription() + " deleted");
notifyChanged(); notifyChanged();
} }

View File

@ -25,6 +25,6 @@ public class IncomingMessageRetry extends BroadcastReceiver
return; return;
} }
app.inbox.retryForwardMessage(message); app.inbox.enqueueMessage(message);
} }
} }

View File

@ -19,7 +19,6 @@ import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import java.text.DateFormat; import java.text.DateFormat;
@ -221,7 +220,7 @@ public class PendingMessages extends ListActivity {
{ {
if (message instanceof IncomingMessage) if (message instanceof IncomingMessage)
{ {
app.inbox.retryForwardMessage((IncomingMessage)message); app.inbox.enqueueMessage((IncomingMessage)message);
} }
else else
{ {