30
30
import com .chess .clock .views .ClockMenu ;
31
31
import com .chess .clock .views .ViewUtils ;
32
32
33
+
33
34
public class ClockTimersActivity extends BaseActivity implements AdjustTimeDialogFragment .TimeAdjustmentsListener {
34
35
35
36
private static final String TAG = ClockTimersActivity .class .getName ();
@@ -57,8 +58,9 @@ public class ClockTimersActivity extends BaseActivity implements AdjustTimeDialo
57
58
/**
58
59
* Utils
59
60
*/
60
- private View mDecorView ;
61
- private TimersState mTimersState ;
61
+ private View windowDecorView ;
62
+ private TimersState timerState ;
63
+ private TimersState timersStatePreviousToPause ;
62
64
private final CountDownTimer .Callback playerOneCallback = new CountDownTimer .Callback () {
63
65
@ Override
64
66
public void onClockTimeUpdate (long millisUntilFinished ) {
@@ -68,7 +70,7 @@ public void onClockTimeUpdate(long millisUntilFinished) {
68
70
@ Override
69
71
public void onClockFinish () {
70
72
Log .i (TAG , "Player one loses" );
71
- mTimersState = TimersState .PLAYER_ONE_FINISHED ;
73
+ timerState = TimersState .PLAYER_ONE_FINISHED ;
72
74
soundManager .playSound (ClockSound .GAME_FINISHED , audioManager );
73
75
updateUIState ();
74
76
}
@@ -97,7 +99,7 @@ public void onClockTimeUpdate(long millisUntilFinished) {
97
99
@ Override
98
100
public void onClockFinish () {
99
101
Log .i (TAG , "Player two loses" );
100
- mTimersState = TimersState .PLAYER_TWO_FINISHED ;
102
+ timerState = TimersState .PLAYER_TWO_FINISHED ;
101
103
soundManager .playSound (ClockSound .GAME_FINISHED , audioManager );
102
104
updateUIState ();
103
105
}
@@ -117,15 +119,15 @@ public void onTotalStageNumber(int stagesNumber) {
117
119
playerTwoButton .setStages (stagesNumber );
118
120
}
119
121
};
120
- private TimersState mTimersStatePreviousToPause ;
122
+
121
123
122
124
private final ActivityResultLauncher <Intent > settingsResultLauncher = registerForActivityResult (
123
125
new ActivityResultContracts .StartActivityForResult (),
124
126
result -> {
125
127
if (result .getResultCode () == RESULT_OK ) {
126
128
// Both states at pause means it's the beginning of the game.
127
- mTimersState = TimersState .PAUSED ;
128
- mTimersStatePreviousToPause = TimersState .PAUSED ;
129
+ timerState = TimersState .PAUSED ;
130
+ timersStatePreviousToPause = TimersState .PAUSED ;
129
131
updateUIState ();
130
132
}
131
133
});
@@ -154,7 +156,7 @@ protected void onCreate(Bundle savedInstanceState) {
154
156
getProperLandscapeLayout () : R .layout .activity_clock_timers ;
155
157
setContentView (layout );
156
158
157
- mDecorView = getWindow ().getDecorView ();
159
+ windowDecorView = getWindow ().getDecorView ();
158
160
soundManager = new ClockSoundManagerImpl ();
159
161
soundManager .init (getApplicationContext ());
160
162
@@ -169,11 +171,11 @@ protected void onCreate(Bundle savedInstanceState) {
169
171
restoreState (savedInstanceState );
170
172
} else {
171
173
// Reset timers state to PAUSED
172
- mTimersState = TimersState .PAUSED ;
173
- mTimersStatePreviousToPause = TimersState .PAUSED ;
174
+ timerState = TimersState .PAUSED ;
175
+ timersStatePreviousToPause = TimersState .PAUSED ;
174
176
}
175
177
176
- initClock ();
178
+ initClock (savedInstanceState == null );
177
179
}
178
180
179
181
/**
@@ -186,14 +188,14 @@ public void onWindowFocusChanged(boolean hasFocus) {
186
188
boolean isFullScreen = appData .getClockFullScreen ();
187
189
if (hasFocus && currentApiVersion >= Build .VERSION_CODES .KITKAT ) {
188
190
if (isFullScreen ) {
189
- mDecorView .setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_STABLE
191
+ windowDecorView .setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_STABLE
190
192
| View .SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
191
193
| View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
192
194
| View .SYSTEM_UI_FLAG_HIDE_NAVIGATION
193
195
| View .SYSTEM_UI_FLAG_FULLSCREEN
194
196
| View .SYSTEM_UI_FLAG_IMMERSIVE_STICKY );
195
197
} else {
196
- mDecorView .setSystemUiVisibility (0 );
198
+ windowDecorView .setSystemUiVisibility (0 );
197
199
}
198
200
} else if (hasFocus ) {
199
201
if (isFullScreen ) {
@@ -233,15 +235,15 @@ private int getProperLandscapeLayout() {
233
235
private void restoreState (Bundle savedInstanceState ) {
234
236
if (savedInstanceState .containsKey (STATE_TIMERS_KEY )) {
235
237
int state = savedInstanceState .getInt (STATE_TIMERS_KEY );
236
- mTimersState = TimersState .fromInteger (state );
238
+ timerState = TimersState .fromInteger (state );
237
239
}
238
240
if (savedInstanceState .containsKey (STATE_TIMERS_PREVIOUS_PAUSE_KEY )) {
239
241
int state = savedInstanceState .getInt (STATE_TIMERS_PREVIOUS_PAUSE_KEY );
240
- mTimersStatePreviousToPause = TimersState .fromInteger (state );
242
+ timersStatePreviousToPause = TimersState .fromInteger (state );
241
243
}
242
244
243
245
// Set play/pause toggle btn
244
- if (mTimersState != TimersState .PAUSED ) {
246
+ if (timerState != TimersState .PAUSED ) {
245
247
clockMenu .showPause ();
246
248
}
247
249
}
@@ -267,19 +269,15 @@ protected void onDestroy() {
267
269
268
270
@ Override
269
271
public void onBackPressed () {
270
- if (mTimersState != TimersState .PAUSED ) {
272
+ if (timerState != TimersState .PAUSED ) {
271
273
pauseClock ();
272
274
}
273
275
super .onBackPressed ();
274
276
}
275
277
276
- void initClock () {
278
+ void initClock (boolean initializeClockSettings ) {
277
279
getClockManager ().setListeners (playerOneCallback , playerTwoCallback );
278
- /*
279
- Check if timer is already started. Started timer means that Time Controls were set in the clock.
280
- If no time control were set, restore a new timer with last used Time Controls.
281
- */
282
- if (!getClockManager ().isClockStarted ()) {
280
+ if (initializeClockSettings ) {
283
281
TimeControlWrapper selectedControl = TimeControlParser .getLastTimeControlOrDefault (this );
284
282
getClockManager ().setupClock (selectedControl );
285
283
Log .d (TAG , "Last controls set." );
@@ -294,19 +292,19 @@ public void resetClock() {
294
292
getClockManager ().resetClock ();
295
293
296
294
// Both states at pause means it's the beginning of the game.
297
- mTimersState = TimersState .PAUSED ;
298
- mTimersStatePreviousToPause = TimersState .PAUSED ;
295
+ timerState = TimersState .PAUSED ;
296
+ timersStatePreviousToPause = TimersState .PAUSED ;
299
297
updateUIState ();
300
298
soundManager .playSound (ClockSound .RESET_CLOCK , audioManager );
301
299
}
302
300
303
301
public void pauseClock () {
304
- if (mTimersState == TimersState .PLAYER_ONE_RUNNING || mTimersState == TimersState .PLAYER_TWO_RUNNING ) {
302
+ if (timerState == TimersState .PLAYER_ONE_RUNNING || timerState == TimersState .PLAYER_TWO_RUNNING ) {
305
303
Log .i (TAG , "Clock paused." );
306
- mTimersStatePreviousToPause = mTimersState ;
307
- mTimersState = TimersState .PAUSED ;
308
- Log .d (TAG , "Previous state: " + mTimersStatePreviousToPause +
309
- " , current state: " + mTimersState );
304
+ timersStatePreviousToPause = timerState ;
305
+ timerState = TimersState .PAUSED ;
306
+ Log .d (TAG , "Previous state: " + timersStatePreviousToPause +
307
+ " , current state: " + timerState );
310
308
getClockManager ().pauseClock ();
311
309
312
310
updateUIState ();
@@ -321,8 +319,8 @@ public void pauseClock() {
321
319
@ Override
322
320
public void onSaveInstanceState (Bundle saveInstanceState ) {
323
321
Log .v (TAG , "Saving UI State on instance Bundle " );
324
- saveInstanceState .putInt (STATE_TIMERS_KEY , mTimersState .getValue ());
325
- saveInstanceState .putInt (STATE_TIMERS_PREVIOUS_PAUSE_KEY , mTimersStatePreviousToPause .getValue ());
322
+ saveInstanceState .putInt (STATE_TIMERS_KEY , timerState .getValue ());
323
+ saveInstanceState .putInt (STATE_TIMERS_PREVIOUS_PAUSE_KEY , timersStatePreviousToPause .getValue ());
326
324
super .onSaveInstanceState (saveInstanceState );
327
325
}
328
326
@@ -374,8 +372,8 @@ public void timeSettingsClicked() {
374
372
375
373
@ Override
376
374
public void playPauseClicked () {
377
- if (mTimersState == TimersState .PAUSED ) {
378
- onPlayerClockClicked (mTimersStatePreviousToPause != TimersState .PLAYER_ONE_RUNNING );
375
+ if (timerState == TimersState .PAUSED ) {
376
+ onPlayerClockClicked (timersStatePreviousToPause != TimersState .PLAYER_ONE_RUNNING );
379
377
} else {
380
378
pauseClock ();
381
379
soundManager .playSound (ClockSound .MENU_ACTION , audioManager );
@@ -384,8 +382,8 @@ public void playPauseClicked() {
384
382
385
383
@ Override
386
384
public void resetClicked () {
387
- if (mTimersState == TimersState .PLAYER_ONE_RUNNING
388
- || mTimersState == TimersState .PLAYER_TWO_RUNNING ) {
385
+ if (timerState == TimersState .PLAYER_ONE_RUNNING
386
+ || timerState == TimersState .PLAYER_TWO_RUNNING ) {
389
387
pauseClock ();
390
388
}
391
389
showResetClockDialog ();
@@ -406,8 +404,8 @@ public void soundClicked() {
406
404
* Change buttons and timers UI according to TimerState.
407
405
*/
408
406
private void updateUIState () {
409
- Log .d (TAG , "Updating UI state to: " + mTimersState );
410
- switch (mTimersState ) {
407
+ Log .d (TAG , "Updating UI state to: " + timerState );
408
+ switch (timerState ) {
411
409
case PAUSED :
412
410
playerOneButton .updateUi (selectedTheme , ClockButton .State .IDLE );
413
411
playerTwoButton .updateUi (selectedTheme , ClockButton .State .IDLE );
@@ -436,24 +434,24 @@ private void updateUIState() {
436
434
}
437
435
clockMenu .updateSoundIcon (soundManager .areSoundsEnabled ());
438
436
if (clocksDivider != null ) {
439
- ViewUtils .showView (clocksDivider , mTimersState == TimersState .PAUSED );
437
+ ViewUtils .showView (clocksDivider , timerState == TimersState .PAUSED );
440
438
}
441
439
}
442
440
443
441
private void onPlayerClockClicked (boolean firstPlayer ) {
444
442
ClockPlayer player = ClockPlayer .ofBoolean (firstPlayer );
445
443
TimersState playerTimerRunning = firstPlayer ? TimersState .PLAYER_ONE_RUNNING : TimersState .PLAYER_TWO_RUNNING ;
446
444
447
- Log .i (TAG , "Player " + player .name () + " pressed the clock with state: " + mTimersState + " (previous: " + mTimersStatePreviousToPause + ")" );
448
- if (mTimersState == TimersState .PAUSED && mTimersStatePreviousToPause == TimersState .PAUSED ) {
445
+ Log .i (TAG , "Player " + player .name () + " pressed the clock with state: " + timerState + " (previous: " + timersStatePreviousToPause + ")" );
446
+ if (timerState == TimersState .PAUSED && timersStatePreviousToPause == TimersState .PAUSED ) {
449
447
clockMenu .showPause ();
450
448
}
451
- if (mTimersState == playerTimerRunning || mTimersState == TimersState .PAUSED ) {
449
+ if (timerState == playerTimerRunning || timerState == TimersState .PAUSED ) {
452
450
getClockManager ().pressClock (player );
453
- mTimersState = firstPlayer ? TimersState .PLAYER_TWO_RUNNING : TimersState .PLAYER_ONE_RUNNING ;
451
+ timerState = firstPlayer ? TimersState .PLAYER_TWO_RUNNING : TimersState .PLAYER_ONE_RUNNING ;
454
452
soundManager .playSound (ClockSound .PLAYER_ONE_MOVE , audioManager );
455
453
updateUIState ();
456
- } else if (mTimersState == TimersState .PLAYER_ONE_FINISHED || mTimersState == TimersState .PLAYER_TWO_FINISHED ) {
454
+ } else if (timerState == TimersState .PLAYER_ONE_FINISHED || timerState == TimersState .PLAYER_TWO_FINISHED ) {
457
455
showResetClockDialog ();
458
456
}
459
457
}
0 commit comments