-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomCountDownTimer.java
109 lines (87 loc) · 1.96 KB
/
CustomCountDownTimer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import android.os.CountDownTimer;
/**
* Created by Vignesh on 3/21/16.
*/
public abstract class CustomCountDownTimer {
long millisInFuture =0;
long countDownInterval =0;
long millisRemaining = 0;
boolean intervalReduced = false;
long countDownIntervalOrig;
CountDownTimer timer=null;
boolean isPaused = true;
public CustomCountDownTimer(long millisInFuture, long countDownInterval)
{
this.countDownIntervalOrig = countDownInterval;
this.countDownInterval = countDownInterval;
this.millisInFuture = millisInFuture;
this.millisRemaining = millisInFuture;
}
public void CreateCDT(long addition)
{
countDownInterval = countDownIntervalOrig;
millisRemaining +=addition;
if(millisRemaining <countDownInterval)
countdownInterval = millisRemaining;
timer = new CountDownTimer(millisRemaining,countDownInterval) {
@Override
public void onTick(long millisUntilFinished) {
millisRemaining = millisUntilFinished;
if(intervalReduced)
CustomCountDownTimer.this.OnTick(millisRemaining*2);
else
CustomCountDownTimer.this.OnTick(millisRemaining);
}
@Override
public void onFinish() {
CustomCountDownTimer.this.OnFinish();
}
};
}
public abstract void OnTick(long millisRemaining);
public abstract void OnFinish();
public void Cancel()
{
if(timer!=null)
{
timer.cancel();
}
this.millisRemaining = 0;
}
public synchronized void Start()
{
if(isPaused == true)
{
CreateCDT(0);
timer.start();
isPaused = false;
}
}
public void Pause()throws IllegalStateException
{
if(isPaused == false)
{
timer.cancel();
}
else
{
throw new IllegalStateException("Timer is already paused");
}
isPaused = true;
}
public void Update(long addition)
{
timer.cancel();
CreateCDT(addition);
timer.start();
}
public void UpdateInterval()
{
timer.cancel();
intervalReduced = true;
millisRemaining /= 2;
countDownIntervalOrig = 500;
CreateCDT(0);
timer.start();
}
}