12
12
import android .view .MotionEvent ;
13
13
import android .view .View ;
14
14
15
+ import java .util .Random ;
15
16
import java .util .Timer ;
16
17
import java .util .TimerTask ;
17
18
@@ -90,7 +91,9 @@ static class TextPaints {
90
91
private int mVisualSearchTasksLeft = 10 ;
91
92
92
93
// Other instance fields
93
- private Timer mTimer = new Timer ();
94
+ private Random mRandom = new Random (42 );
95
+ private Timer mVisualSearchTaskViewTimer = new Timer ();
96
+ private Timer mNotificationTimer = new Timer ();
94
97
VisualSearchTaskGrid mVisualSearchTaskGrid ;
95
98
private Mode mMode = Mode .START ;
96
99
Canvas mCanvas ;
@@ -161,10 +164,10 @@ public boolean onTouchEvent(MotionEvent event) {
161
164
if (mMode == Mode .START ) {
162
165
if (--mStartConfirmTapsLeft == 0 ) {
163
166
mMode = Mode .COUNTDOWN ;
164
- mTimer .cancel ();
165
- mTimer .purge ();
166
- mTimer = new Timer ();
167
- mTimer .scheduleAtFixedRate (new CountdownTimerTask (), 0 , COUNTDOWN_DISPLAY_DURATION );
167
+ mVisualSearchTaskViewTimer .cancel ();
168
+ mVisualSearchTaskViewTimer .purge ();
169
+ mVisualSearchTaskViewTimer = new Timer ();
170
+ mVisualSearchTaskViewTimer .scheduleAtFixedRate (new CountdownTimerTask (), 0 , COUNTDOWN_DISPLAY_DURATION );
168
171
} else {
169
172
this .triggerRedraw ();
170
173
}
@@ -221,17 +224,24 @@ private void drawNextGrid() {
221
224
222
225
//endregion Drawing
223
226
224
- //region Task Methods
227
+ //region Timer Methods
225
228
226
229
public void startNewVisualSearchTaskAfterYesOrNoPressed () {
227
230
Assert .that (mMode == Mode .VISUAL_SEARCH );
228
- mTimer .cancel ();
229
- mTimer .purge ();
230
- mTimer = new Timer ();
231
- mTimer .scheduleAtFixedRate (new VisualSearchTimerTask (), 0 , VISUAL_SEARCH_DISPLAY_DURATION );
231
+ mVisualSearchTaskViewTimer .cancel ();
232
+ mVisualSearchTaskViewTimer .purge ();
233
+ mVisualSearchTaskViewTimer = new Timer ();
234
+ mVisualSearchTaskViewTimer .scheduleAtFixedRate (new VisualSearchTimerTask (), 0 , VISUAL_SEARCH_DISPLAY_DURATION );
232
235
}
233
236
234
- //endregion Task Methods
237
+ private void queueNextNotification () {
238
+ mNotificationTimer .cancel ();
239
+ mNotificationTimer .purge ();
240
+ mNotificationTimer = new Timer ();
241
+ mNotificationTimer .schedule (new SendNotificationTask (), getNextNotificationDelayMS ());
242
+ }
243
+
244
+ //endregion Timer Methods
235
245
236
246
//region Helpers
237
247
@@ -252,6 +262,15 @@ private void drawTextInViewCenter(String text, Paint paint) {
252
262
mCanvas .drawText (text , x , y , paint );
253
263
}
254
264
265
+ private int getNextNotificationDelayMS () {
266
+ // Values from [4, 5, 6, 7]
267
+ return (4 + mRandom .nextInt (4 )) * 1000 ;
268
+ }
269
+
270
+ private int getNextNotificationNumber () {
271
+ return 1 + mRandom .nextInt (3 );
272
+ }
273
+
255
274
//endregion Helpers
256
275
257
276
private class CountdownTimerTask extends TimerTask {
@@ -262,13 +281,16 @@ public void run() {
262
281
if (mCurrentCountdownValue == 1 ) {
263
282
// Cancel TimerTask and Timer
264
283
this .cancel ();
265
- mTimer .cancel ();
266
- mTimer .purge ();
284
+ mVisualSearchTaskViewTimer .cancel ();
285
+ mVisualSearchTaskViewTimer .purge ();
267
286
// Start the VisualSearchTimerTask
268
287
mMode = Mode .VISUAL_SEARCH ;
269
- mTimer = new Timer ();
270
- mTimer .scheduleAtFixedRate (new VisualSearchTimerTask (), 0 , VISUAL_SEARCH_DISPLAY_DURATION );
288
+ mVisualSearchTaskViewTimer = new Timer ();
289
+ mVisualSearchTaskViewTimer .scheduleAtFixedRate (new VisualSearchTimerTask (), 0 , VISUAL_SEARCH_DISPLAY_DURATION );
271
290
mVisualSearchViewEventsListener .onVisualSearchTaskFirstStart ();
291
+
292
+ queueNextNotification ();
293
+
272
294
return ;
273
295
}
274
296
@@ -287,7 +309,7 @@ public void run() {
287
309
288
310
if (mVisualSearchTasksLeft == 0 ) {
289
311
this .cancel ();
290
- mTimer .cancel ();
312
+ mVisualSearchTaskViewTimer .cancel ();
291
313
// Show the completed screen
292
314
mMode = Mode .COMPLETED ;
293
315
mVisualSearchViewEventsListener .onVisualSearchTaskCompleted ();
@@ -302,12 +324,24 @@ public void run() {
302
324
}
303
325
}
304
326
327
+ private class SendNotificationTask extends TimerTask {
328
+ @ Override
329
+ public void run () {
330
+ // Send the notification now
331
+ int nextNotificationNumber = getNextNotificationNumber ();
332
+ mVisualSearchViewEventsListener .onActivityShouldSendNotification (nextNotificationNumber );
333
+ // Queue the next one
334
+ queueNextNotification ();
335
+ }
336
+ }
337
+
305
338
public void setVisualSearchTaskEventsListener (VisualSearchViewEventsListener listener ) {
306
339
mVisualSearchViewEventsListener = listener ;
307
340
}
308
341
309
342
public interface VisualSearchViewEventsListener {
310
343
void onVisualSearchTaskFirstStart ();
344
+ void onActivityShouldSendNotification (int number );
311
345
void onVisualSearchTaskCompleted ();
312
346
}
313
347
}
0 commit comments