From b230cc4b2ba9d298889212d1f05151ccb280d1ba Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Fri, 10 Nov 2017 21:27:17 -0500 Subject: [PATCH] Add notifications on game end Add exit menu option, too, which should force the notification away even if nothing else works. --- .../ctfwstimer/lib/CtFwSGameState.java | 7 +++-- .../ctfwstimer/MainActivity.java | 6 +++++ .../ctfwstimer/MainService.java | 3 +++ .../ctfwstimer/MainServiceNotification.java | 27 ++++++++++++------- mobile/src/main/res/menu/mainmenu.xml | 5 ++++ mobile/src/main/res/values/strings.xml | 1 + 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameState.java b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameState.java index 0cb30a5..96bf735 100644 --- a/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameState.java +++ b/lib/src/main/java/com/acmetensortoys/ctfwstimer/lib/CtFwSGameState.java @@ -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); diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java index f6597f4..eef71ca 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainActivity.java @@ -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); diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java index e4cfcb1..9affb09 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java @@ -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(); diff --git a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java index f9bd49d..47b896f 100644 --- a/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java +++ b/mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainServiceNotification.java @@ -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; } diff --git a/mobile/src/main/res/menu/mainmenu.xml b/mobile/src/main/res/menu/mainmenu.xml index d5837ef..c952aac 100644 --- a/mobile/src/main/res/menu/mainmenu.xml +++ b/mobile/src/main/res/menu/mainmenu.xml @@ -25,4 +25,9 @@ android:icon="@android:drawable/ic_menu_help" android:title="@string/menutext_about" android:visible="true" /> + \ No newline at end of file diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index 5c22521..ca845ea 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -22,6 +22,7 @@ About Settings + Quit Connection Metadata: Server URI: -- 2.50.1