]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Add support for message reset messages
authorNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sun, 28 Oct 2018 14:52:58 +0000 (14:52 +0000)
committerNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sun, 28 Oct 2018 15:16:12 +0000 (15:16 +0000)
And tweak the handling of the message queue in notifications
Add TODO for main display

lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameStateManager.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplayLocal.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java

index a0b05adb0476655d57b72a0ea754214aed618f8c..bb6e746e331261d1a2daf0dbaedd3d8c586a4aae 100644 (file)
@@ -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 {
index 4f976f1eb4c39ce8460d09c6dbe5205b591a96f6..cb0a458d7838a2d786dfce70cdd7bf29284e3968 100644 (file)
@@ -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);
+        }
+    };
 }
index 2db10fecc2d732334b80dc6b25b4f21e3c28180e..90931d356d6dc128f8bb0b230612c0dc92f66822 100644 (file)
@@ -366,7 +366,6 @@ class CtFwSDisplayLocal implements CtFwSGameStateManager.Observer {
         });
     }
 
-    private CtFwSGameStateManager.Msg lastMsg;
     @Override
     public void onCtFwSMessage(CtFwSGameStateManager gs, List<CtFwSGameStateManager.Msg> 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<CtFwSGameStateManager.Msg> news = msgs.listIterator(ix);
+        for (ListIterator<CtFwSGameStateManager.Msg> 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);
             }
         });
     }
index 63d6046d91d050e41da48f8165317f5c616fb54e..1c4a4225ab9a613f97fbcf067e22e992aa404b98 100644 (file)
@@ -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");
index e9cd11582e333b0f1a90f9b015e8c8fb6f276219..4b2fd3869061b83a881a3427321719ddf050289f 100644 (file)
@@ -142,16 +142,36 @@ class MainServiceNotification {
                 }
             }
 
+            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;
             }
         });
     }