/
CountDownTimerWithPause.java
98 lines (79 loc) · 3.06 KB
/
CountDownTimerWithPause.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
package com.brucemax.boxintervals;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
public abstract class CountDownTimerWithPause {
private long mMillisInFuture;
private long mCountdownInterval;
private long mStopTimeInFuture;
private long mValuePause;
private boolean timerIsFinish = true;
public CountDownTimerWithPause(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
public void setMillisInFuture(long millisInFuture) {
this.mMillisInFuture = millisInFuture;
}
public void setCountdownInterval(long countdownInterval) {
this.mCountdownInterval = countdownInterval;
}
public synchronized final void pause() {
if(!mHandler.hasMessages(MSG)) return;
mValuePause = mStopTimeInFuture - SystemClock.elapsedRealtime();
mHandler.removeMessages(MSG);
}
public synchronized final void resume() {
if(mHandler.hasMessages(MSG)) return;
if(timerIsFinish) return;
mStopTimeInFuture = SystemClock.elapsedRealtime() + mValuePause;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
}
public synchronized final boolean isActive() {
if (mHandler.hasMessages(MSG))
return true;
return false;
}
public final void cancel() {
mHandler.removeMessages(MSG);
}
public synchronized final CountDownTimerWithPause start() {
if (mMillisInFuture <= 0) {
onFinish();
timerIsFinish = true;
return this;
}
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
timerIsFinish = false;
return this;
}
public abstract void onTick(long millisUntilFinished);
public abstract void onFinish();
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimerWithPause.this) {
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
if (millisLeft <= 0) {
timerIsFinish = true;
onFinish();
} else if (millisLeft < mCountdownInterval) {
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}