-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch.java
168 lines (143 loc) · 4.34 KB
/
Stopwatch.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.apptuto.com.utility;
import android.os.Handler;
/**
* Created by essam on 3/22/16.
*/
public class Stopwatch {
public interface StopWatchListener {
public void onTick(String time);
}
private long startTime = 0;
private boolean running = false;
private long currentTime = 0;
public Stopwatch(){
}
public Stopwatch(StopWatchListener listener) {
this.listener = listener;
}
public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//lock to access running
synchronized (Stopwatch.this) {
if (running) {
if(listener != null)
listener.onTick(Stopwatch.this.toString());
handler.postDelayed(this, 1000);
}
}
}
});
}
/**
* Overload
* <p/>
* Starts the stopwatch from an offset
*
* @param offset The offset in milli seconds
*/
public void start(long offset) {
stop();
this.startTime = System.currentTimeMillis() - offset;
this.running = true;
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//lock to access running
synchronized (Stopwatch.this) {
if (running) {
if(listener != null)
listener.onTick(Stopwatch.this.toString());
handler.postDelayed(this, 1000);
}
}
}
});
}
public synchronized void stop() {
this.running = false;
}
public void restart() {
this.stop();
this.start();
}
public synchronized void pause() {
if(!isRunning()) return;
this.running = false;
currentTime = System.currentTimeMillis() - startTime;
}
public void resume() {
if (running) return;
synchronized (Stopwatch.this) {
this.running = true;
this.startTime = System.currentTimeMillis() - currentTime;
}
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
if (running) {
if(listener != null)
listener.onTick(Stopwatch.this.toString());
handler.postDelayed(this, 1000);
}
}
});
}
//elaspsed time in milliseconds
public long getElapsedTimeMili() {
long elapsed = 0;
if (running) {
elapsed = ((System.currentTimeMillis() - startTime) / 100) % 1000;
}
return elapsed;
}
//elaspsed time in seconds
public long getElapsedTimeSecs() {
long elapsed = 0;
if (running) {
elapsed = ((System.currentTimeMillis() - startTime) / 1000) % 60;
}
return elapsed;
}
//elaspsed time in minutes
public long getElapsedTimeMin() {
long elapsed = 0;
if (running) {
elapsed = (((System.currentTimeMillis() - startTime) / 1000) / 60) % 60;
}
return elapsed;
}
//elaspsed time in hours
public long getElapsedTimeHour() {
long elapsed = 0;
if (running) {
elapsed = ((((System.currentTimeMillis() - startTime) / 1000) / 60) / 60);
}
return elapsed;
}
public String toString() {
return padZero(getElapsedTimeHour()) + ":" + padZero(getElapsedTimeMin()) + ":"
+ padZero(getElapsedTimeSecs());
}
public long getTotalTimeElapsed() {
return (getElapsedTimeHour() * 60 * 60 + getElapsedTimeMin() * 60 + getElapsedTimeSecs()) * 1000;
}
public boolean isRunning() {
return running;
}
public void setListener(StopWatchListener listener){
this.listener = listener;
}
private String padZero(long time) {
if (time < 10)
return "0" + time;
return String.valueOf(time);
}
private StopWatchListener listener = null;
}