2323import android .graphics .drawable .ColorDrawable ;
2424import android .media .ToneGenerator ;
2525import android .os .Bundle ;
26+ import android .os .CountDownTimer ;
2627import android .view .Menu ;
2728import android .view .MenuItem ;
2829import android .view .MotionEvent ;
3738import androidx .appcompat .app .AlertDialog ;
3839import androidx .appcompat .app .AppCompatActivity ;
3940
41+ import com .google .android .material .progressindicator .LinearProgressIndicator ;
42+
4043import java .util .ArrayList ;
4144import java .util .Collections ;
4245import java .util .Date ;
@@ -62,6 +65,7 @@ public class MainActivity extends AppCompatActivity implements View.OnTouchListe
6265 private int expectedNumber = 1 ;
6366 private GridLayout grid ;
6467 private View gridContainer ;
68+ private LinearProgressIndicator progressBar ;
6569
6670 private long startTime = 0 ;
6771
@@ -71,6 +75,8 @@ public class MainActivity extends AppCompatActivity implements View.OnTouchListe
7175
7276 private String additionalSpeech ;
7377
78+ private CountDownTimer timer ;
79+
7480 @ Override
7581 protected void onCreate (Bundle savedInstanceState ) {
7682 super .onCreate (savedInstanceState );
@@ -81,12 +87,32 @@ protected void onCreate(Bundle savedInstanceState) {
8187 data = new SavedData (this );
8288 grid = findViewById (R .id .grid );
8389 gridContainer = findViewById (R .id .grid_container );
90+ progressBar = findViewById (R .id .progress_bar );
8491
8592 speaker = new Speaker (this , data );
8693
8794 resetGrid ();
8895 }
8996
97+ private void startProgressTimer () {
98+ timer = new CountDownTimer (60000 , 1000 ) {
99+ @ Override
100+ public void onTick (long millisUntilFinished ) {
101+ long progress = millisUntilFinished / 1000 ;
102+ progressBar .setProgress ((int ) progress );
103+ if (progress <= 0 ) {
104+ showRestart (LOSE );
105+ cancel ();
106+ }
107+ }
108+
109+ @ Override
110+ public void onFinish () {
111+ showRestart (LOSE );
112+ }
113+ }.start ();
114+ }
115+
90116 private void resetGrid () {
91117 invalidateOptionsMenu ();
92118 gridContainer .setBackgroundResource (R .drawable .game_background );
@@ -98,12 +124,24 @@ private void resetGrid() {
98124 expectedNumber = 1 ;
99125 gameStarted = false ;
100126 additionalSpeech = "" ;
127+ progressBar .setProgress (60 );
128+ startProgressTimer ();
101129 }
102130
103131 private void generateSequence () {
104132 Collections .shuffle (sequence );
105133 }
106134
135+ @ Override
136+ protected void onPause () {
137+ super .onPause ();
138+ }
139+
140+ @ Override
141+ protected void onResume () {
142+ super .onResume ();
143+ }
144+
107145 @ SuppressLint ("ClickableViewAccessibility" )
108146 private void createButtons () {
109147 int k = 0 ;
@@ -153,8 +191,15 @@ private CharSequence getMappedString(int i) {
153191
154192
155193 private void showRestart (int status ) {
156- gridContainer .setBackgroundResource (status == WIN ?
157- R .drawable .win_background : R .drawable .lose_background );
194+ if (timer != null ) {
195+ timer .cancel ();
196+ timer = null ;
197+ }
198+ if (status == LOSE ) {
199+ data .resetStreak ();
200+ data .resetStars ();
201+ }
202+ gridContainer .setBackgroundResource (status == WIN ? R .drawable .win_background : R .drawable .lose_background );
158203 int timeTaken = (int ) TimeUnit .MILLISECONDS .toSeconds (new Date ().getTime () - startTime );
159204 boolean createdRecord = false ;
160205 int previousRecord = data .getFastestTime ();
@@ -182,13 +227,8 @@ private void showRestart(int status) {
182227
183228 data .updateStats (status == WIN );
184229
185- titleView .setText (status == WIN ?
186- String .format (Locale .ENGLISH , "🤩 You win!\n 🙌 Streak: %d" , data .getStreak ()) :
187- "😖 Game over!" );
188- messageView .setText (status == WIN ?
189- String .format (Locale .ENGLISH , getString (createdRecord ? R .string .success_message_record :
190- R .string .success_message ), timeTaken , previousRecord ) :
191- getString (R .string .game_over_message ));
230+ titleView .setText (status == WIN ? String .format (Locale .ENGLISH , "🤩 You win!\n 🙌 Streak: %d" , data .getStreak ()) : "😖 Game over!" );
231+ messageView .setText (status == WIN ? String .format (Locale .ENGLISH , getString (createdRecord ? R .string .success_message_record : R .string .success_message ), timeTaken , previousRecord ) : getString (R .string .game_over_message ));
192232
193233 AlertDialog dialog = builder .create ();
194234 Objects .requireNonNull (dialog .getWindow ()).setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
@@ -206,9 +246,7 @@ private void showRestart(int status) {
206246 if (!data .areSoundsOn ()) {
207247 neutralButton .setEnabled (false );
208248 }
209- neutralButton .setOnClickListener (v ->
210- speaker .say (String .format (Locale .ENGLISH , getString (R .string .tts_time_taken ), timeTaken ) +
211- ". " + additionalSpeech ));
249+ neutralButton .setOnClickListener (v -> speaker .say (String .format (Locale .ENGLISH , getString (R .string .tts_time_taken ), timeTaken ) + ". " + additionalSpeech ));
212250 }
213251
214252 dialog .show ();
@@ -326,9 +364,7 @@ private void showStatsDialog() {
326364 return ;
327365 }
328366 Float winRate = (float ) (100.0 * nWins / nGames );
329- String statsText = String .format (Locale .ENGLISH ,
330- "Total games played: %d\n Number of wins: %d\n Win rate: %.2f %%" ,
331- nGames , nWins , winRate );
367+ String statsText = String .format (Locale .ENGLISH , "Total games played: %d\n Number of wins: %d\n Win rate: %.2f %%" , nGames , nWins , winRate );
332368 messageView .setText (statsText );
333369
334370 AlertDialog dialog = builder .create ();
@@ -350,6 +386,9 @@ protected void onDestroy() {
350386 if (speaker != null ) {
351387 speaker .releaseResources ();
352388 }
389+ if (timer != null ) {
390+ timer .cancel ();
391+ }
353392 super .onDestroy ();
354393 }
355394
@@ -391,11 +430,10 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
391430 data .decrementStarsAvailable ();
392431 invalidateOptionsMenu ();
393432 } else {
394- data .resetStreak ();
395- data .resetStars ();
396433 MainActivity .this .showRestart (LOSE );
397434 }
398435 }
399436 return true ;
400437 }
438+
401439}
0 commit comments