From aaf730817fbe24493da121413f1745e9b0a1a576 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sun, 28 Oct 2018 14:52:58 +0000 Subject: [PATCH] Add support for message reset messages And tweak the handling of the message queue in notifications Add TODO for main display --- .../ctfwstimer/lib/CtFwSGameStateManager.java | 13 ++++++++ .../ctfwstimer/CtFwSCallbacksMQTT.java | 14 ++++++++ .../ctfwstimer/CtFwSDisplayLocal.java | 20 ++---------- .../ctfwstimer/MainService.java | 3 +- .../ctfwstimer/MainServiceNotification.java | 32 +++++++++++++++---- 5 files changed, 57 insertions(+), 25 deletions(-) diff --git a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java index a0b05ad..bb6e746 100644 --- a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java +++ b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java @@ -296,6 +296,19 @@ public class CtFwSGameStateManager { } } } + 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 { diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java index 4f976f1..cb0a458 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java @@ -67,4 +67,18 @@ class CtFwSCallbacksMQTT { 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); + } + }; } diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java index 2db10fe..90931d3 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java @@ -366,7 +366,6 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer { }); } - private CtFwSGameStateManager.Msg lastMsg; @Override public void onCtFwSMessage(CtFwSGameStateManager gs, List msgs) { final TextView msgstv = mAct.findViewById(R.id.msgs); @@ -382,21 +381,8 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer { 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 news = msgs.listIterator(ix); + for (ListIterator news = msgs.listIterator(0); news.hasNext(); ) { CtFwSGameStateManager.Msg m = news.next(); @@ -407,14 +393,12 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer { 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); } }); } diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java index 63d6046..1c4a422 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java @@ -107,6 +107,7 @@ public class MainService extends Service { 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() { @@ -203,7 +204,7 @@ public class MainService extends Service { 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"); diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java index e9cd115..4b2fd38 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java @@ -142,16 +142,36 @@ class MainServiceNotification { } } + private int lastMsgIx = 0; + @Override public void onCtFwSMessage(CtFwSGameStateManager game, List 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; } }); } -- 2.50.1