}
}
}
+ public void onMessageReset(long before) {
+ synchronized(this) {
+ while(!msgs.isEmpty()) {
+ Msg m = msgs.get(0);
+ if (m.when <= before) {
+ msgs.remove(0);
+ } else {
+ break;
+ }
+ }
+ notifyMessages();
+ }
+ }
// Observer interface
public interface Observer {
mCgs.onNewMessage(str);
}
};
+
+ final IMqttMessageListener onMessageReset = new IMqttMessageListener() {
+ @Override
+ public void messageArrived(String topic, MqttMessage message) throws Exception {
+ String str = message.toString();
+ long before;
+ try {
+ before = Long.parseLong(message.toString());
+ } catch (NumberFormatException e) {
+ return;
+ }
+ mCgs.onMessageReset(before);
+ }
+ };
}
});
}
- private CtFwSGameStateManager.Msg lastMsg;
@Override
public void onCtFwSMessage(CtFwSGameStateManager gs, List<CtFwSGameStateManager.Msg> msgs) {
final TextView msgstv = mAct.findViewById(R.id.msgs);
return;
}
- int ix;
- if (lastMsg == null) {
- ix = 0;
- } else {
- ix = msgs.indexOf(lastMsg);
- if (ix == -1) {
- ix = 0;
- } else if (ix == s) {
- return;
- } else {
- ix = ix + 1;
- }
- }
final StringBuffer sb = new StringBuffer();
- for (ListIterator<CtFwSGameStateManager.Msg> news = msgs.listIterator(ix);
+ for (ListIterator<CtFwSGameStateManager.Msg> news = msgs.listIterator(0);
news.hasNext(); ) {
CtFwSGameStateManager.Msg m = news.next();
sb.append(": ");
sb.append(m.msg);
sb.append("\n");
-
- lastMsg = m;
}
msgstv.post(new Runnable() {
@Override
public void run() {
- msgstv.append(sb);
+ msgstv.setText(sb);
}
});
}
mMqc.subscribe(p + "flags", 2, null, subal, mCtfwscbs.onFlags);
mMqc.subscribe(p + "message", 2, null, subal, mCtfwscbs.onMessage);
mMqc.subscribe(p + "message/player", 2, null, subal, mCtfwscbs.onPlayerMessage);
+ mMqc.subscribe(p + "messagereset", 2, null, subal, mCtfwscbs.onMessageReset);
/* This one isn't really about the game state so much, so handle it ourselves. */
mMqc.subscribe(p + "timesync", 2, null, subal, new IMqttMessageListener() {
String p = "ctfws/game/";
mMqc.unsubscribe(new String[]{
p + "config", p + "endtime", p + "flags", p + "timesync",
- p + "message", p + "message/player"
+ p + "message", p + "message/player", p + "message/reset"
});
} catch (MqttException me) {
Log.d("Service", "domqtt discon unsub exn");
}
}
+ private int lastMsgIx = 0;
+
@Override
public void onCtFwSMessage(CtFwSGameStateManager game, List<CtFwSGameStateManager.Msg> msgs) {
- // Only do anything if we aren't clearing the message list
+ // Only do anything if we have added something to the list since last we looked
+ // and if it's in (or after) the current game.
+ // Always update the length in case this is a reset to zero.
int s = msgs.size();
- if (s != 0) {
- notifyUserSomehow(NotificationSource.MESG);
- lastContextTextSource = LastContentTextSource.MESG;
- userNoteBuilder.setContentText(msgs.get(s - 1).msg);
- refreshNotification();
+ if (s > lastMsgIx) {
+ CtFwSGameStateManager.Msg m = msgs.get(s-1);
+ if (game.isConfigured() && m.when >= game.getStartT()) {
+ notifyUserSomehow(NotificationSource.MESG);
+ lastContextTextSource = LastContentTextSource.MESG;
+ userNoteBuilder.setContentText(m.msg);
+ refreshNotification();
+ }
+ } else {
+ // This is a message reset event that pruned something. It might
+ // have emptied the list, and if so, we should clear out the notification's
+ // content text. If the list isn't empty, whatever we put up last is
+ // just fine to stay there.
+ if (lastContextTextSource == LastContentTextSource.MESG && s == 0) {
+ lastContextTextSource = LastContentTextSource.NONE;
+ userNoteBuilder.setContentText(null);
+ // Suppress vibe, we're just updating the text
+ notifyUserSomehow(NotificationSource.NONE);
+ refreshNotification();
+ }
}
+ lastMsgIx = s;
}
});
}