]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Add notifications on game end
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 11 Nov 2017 02:27:17 +0000 (21:27 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Sat, 11 Nov 2017 02:27:17 +0000 (21:27 -0500)
Add exit menu option, too, which should force the notification away
even if nothing else works.

lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameState.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java
mobile/src/main/res/menu/mainmenu.xml
mobile/src/main/res/values/strings.xml

index 0cb30a592dae4857a3ef798123a35f8ee18adb4d..96bf7354bbd42823fa9008fafda4139c848b78b7 100644 (file)
@@ -68,6 +68,7 @@ public class CtFwSGameState {
     public class Now {
         public String rationale = null; // null if game is in play, otherwise other fields invalid
         public boolean stop = false;
+        public boolean past = false;
         public int round = 0;  // 0 for setup
         public long roundStart = 0, roundEnd = 0; // POSIX seconds
 
@@ -84,8 +85,9 @@ public class CtFwSGameState {
                 res.rationale = "Game not configured!";
                 res.stop = true;
             } else if (endT >= startT) {
-                res.rationale = "Game over!";
+                res.rationale = "Game declared over!";
                 res.stop = true;
+                res.past = true;
             } else if (now < startT) {
                 res.rationale = "Start time in the future!";
                 res.roundStart = res.roundEnd = startT;
@@ -103,8 +105,9 @@ public class CtFwSGameState {
             elapsed -= setupD;
             res.round = (int) (elapsed / roundD);
             if (res.round >= rounds) {
-                res.rationale = "Game over!";
+                res.rationale = "Game time up!";
                 res.stop = true;
+                res.past = true;
                 return res;
             }
             res.roundStart = startT + setupD + (res.round * roundD);
index f6597f4a9c9e5b4775e47aa2e97a342383cb1d97..eef71ca127afcfaaf3371e514c7162e07ff50c4b 100644 (file)
@@ -189,6 +189,12 @@ public class MainActivity extends AppCompatActivity {
             case R.id.menu_about :
                 startActivity(new Intent(this, AboutActivity.class));
                 return true;
+            case R.id.menu_quit:
+                if (mSrvBinder != null) {
+                    mSrvBinder.exit();
+                }
+                finish();
+                return true;
             // Cam: Changing this doesn't appear to do anything? Leaving just in case.
             default:
                 return super.onOptionsItemSelected(mi);
index e4cfcb172bca26d1d234e3177d36935279704053..9affb092edd0f732f2ae683f4a33214cb2245826 100644 (file)
@@ -328,6 +328,9 @@ public class MainService extends Service {
         void unregisterObserver(Observer o) {
             synchronized(MainService.this) { mObsvs.remove(o); }
         }
+        void exit() {
+            mMsn.ensureNoNotification(true);
+        }
     }
     private final LocalBinder mBinder = new LocalBinder();
 
index f9bd49df2ca3b27245c05cf700f25960ac78ed9e..47b896fd385868ea9afd0ff4bdc440a2e355ed5f 100644 (file)
@@ -46,14 +46,16 @@ class MainServiceNotification {
 
             @Override
             public void onCtFwSNow(CtFwSGameState game, CtFwSGameState.Now now) {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-                    userNoteBuilder.setWhen((now.roundEnd + 1) * 1000);
-                } else {
-                    userNoteBuilder.setWhen(now.roundStart * 1000);
-                }
-                userNoteBuilder.setUsesChronometer(true);
                 if (now.rationale == null || !now.stop) {
                     // game is afoot or in the future!
+
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                        userNoteBuilder.setWhen((now.roundEnd + 1) * 1000);
+                    } else {
+                        userNoteBuilder.setWhen(now.roundStart * 1000);
+                    }
+                    userNoteBuilder.setUsesChronometer(true);
+
                     Resources rs = mService.getResources();
 
                     if (now.rationale == null) {
@@ -75,7 +77,14 @@ class MainServiceNotification {
                     ensureNotification();
                 } else {
                     // game no longer afoot
-                    ensureNoNotification();
+                    if (now.past) {
+                        userNoteBuilder.setUsesChronometer(false);
+                        userNoteBuilder.setShowWhen(false);
+                        userNoteBuilder.setContentTitle(now.rationale);
+                        userNoteBuilder.setSubText(now.rationale);
+                        refreshNotification();
+                    }
+                    ensureNoNotification(!now.past);
                 }
             }
 
@@ -202,10 +211,10 @@ class MainServiceNotification {
             refreshNotification();
         }
     }
-    private void ensureNoNotification() {
+    void ensureNoNotification(boolean remove) {
         synchronized (this) {
             if (userNoteSC != null) {
-                mService.stopForeground(true);
+                mService.stopForeground(remove);
                 mService.unbindService(userNoteSC);
                 userNoteSC = null;
             }
index d5837ef5852ffee9143eda087782322c29b3fb85..c952aace07cbf63c12e6730f5486a6cb7be2affe 100644 (file)
@@ -25,4 +25,9 @@
         android:icon="@android:drawable/ic_menu_help"
         android:title="@string/menutext_about"
         android:visible="true" />
+    <item
+        android:id="@+id/menu_quit"
+        android:enabled="true"
+        android:title="@string/menutext_quit"
+        android:visible="true" />
 </menu>
\ No newline at end of file
index 5c22521344b51626fa39935293d04d2dffa5c1c9..ca845eaf6d79bd0cd85d19431781c3a06307cbc0 100644 (file)
@@ -22,6 +22,7 @@
 
     <string name="menutext_about">About</string>
     <string name="menutext_prf">Settings</string>
+    <string name="menutext_quit">Quit</string>
 
     <string name="mqtt_header">Connection Metadata:</string>
     <string name="mqtt_uri_label">Server URI:</string>