5
0
mirror of https://github.com/cwinfo/envayasms.git synced 2024-12-04 12:35:32 +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 java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
public class Inbox {
private Map<Uri, IncomingMessage> incomingMessages = new HashMap<Uri, IncomingMessage>();
private App app;
private int numForwardingMessages = 0;
private Queue<IncomingMessage> incomingQueue = new LinkedList<IncomingMessage>();
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);
}
}

View File

@ -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:

View File

@ -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();
}

View File

@ -25,6 +25,6 @@ public class IncomingMessageRetry extends BroadcastReceiver
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.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
{