blob: 444ebc520cb79de749f0407db0234fee74335da6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
Roozbeh Pournader241872a2016-12-15 15:17:49 -080021import android.icu.text.MeasureFormat;
22import android.icu.text.MeasureFormat.FormatWidth;
23import android.icu.util.Measure;
24import android.icu.util.MeasureUnit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.os.SystemClock;
26import android.text.format.DateUtils;
27import android.util.AttributeSet;
28import android.util.Log;
Simon Dubray814e1f52015-11-05 11:38:40 +010029import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.widget.RemoteViews.RemoteView;
31
Selim Cineked1a33c2016-02-18 17:12:57 -080032import com.android.internal.R;
33
Roozbeh Pournader241872a2016-12-15 15:17:49 -080034import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import java.util.Formatter;
36import java.util.IllegalFormatException;
37import java.util.Locale;
38
39/**
40 * Class that implements a simple timer.
41 * <p>
42 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
43 * and it counts up from that, or if you don't give it a base time, it will use the
Selim Cineked1a33c2016-02-18 17:12:57 -080044 * time at which you call {@link #start}.
45 *
46 * <p>The timer can also count downward towards the base time by
47 * setting {@link #setCountDown(boolean)} to true.
48 *
49 * <p>By default it will display the current
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
51 * to format the timer value into an arbitrary string.
52 *
53 * @attr ref android.R.styleable#Chronometer_format
Selim Cineked1a33c2016-02-18 17:12:57 -080054 * @attr ref android.R.styleable#Chronometer_countDown
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
56@RemoteView
57public class Chronometer extends TextView {
58 private static final String TAG = "Chronometer";
59
60 /**
61 * A callback that notifies when the chronometer has incremented on its own.
62 */
63 public interface OnChronometerTickListener {
64
65 /**
66 * Notification that the chronometer has changed.
67 */
68 void onChronometerTick(Chronometer chronometer);
69
70 }
71
72 private long mBase;
Dan Sandlera79a7472015-06-05 16:52:22 -040073 private long mNow; // the currently displayed time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 private boolean mVisible;
75 private boolean mStarted;
76 private boolean mRunning;
77 private boolean mLogged;
78 private String mFormat;
79 private Formatter mFormatter;
80 private Locale mFormatterLocale;
81 private Object[] mFormatterArgs = new Object[1];
82 private StringBuilder mFormatBuilder;
83 private OnChronometerTickListener mOnChronometerTickListener;
84 private StringBuilder mRecycle = new StringBuilder(8);
Selim Cineked1a33c2016-02-18 17:12:57 -080085 private boolean mCountDown;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
88 * Initialize this Chronometer object.
89 * Sets the base to the current time.
90 */
91 public Chronometer(Context context) {
92 this(context, null, 0);
93 }
94
95 /**
96 * Initialize with standard view layout information.
97 * Sets the base to the current time.
98 */
99 public Chronometer(Context context, AttributeSet attrs) {
100 this(context, attrs, 0);
101 }
102
103 /**
104 * Initialize with standard view layout information and style.
105 * Sets the base to the current time.
106 */
Alan Viverette617feb92013-09-09 18:09:13 -0700107 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) {
108 this(context, attrs, defStyleAttr, 0);
109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Alan Viverette617feb92013-09-09 18:09:13 -0700111 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
112 super(context, attrs, defStyleAttr, defStyleRes);
113
114 final TypedArray a = context.obtainStyledAttributes(
115 attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
Selim Cineked1a33c2016-02-18 17:12:57 -0800116 setFormat(a.getString(R.styleable.Chronometer_format));
117 setCountDown(a.getBoolean(R.styleable.Chronometer_countDown, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 a.recycle();
119
120 init();
121 }
122
123 private void init() {
124 mBase = SystemClock.elapsedRealtime();
125 updateText(mBase);
126 }
127
128 /**
Selim Cineked1a33c2016-02-18 17:12:57 -0800129 * Set this view to count down to the base instead of counting up from it.
130 *
131 * @param countDown whether this view should count down
132 *
133 * @see #setBase(long)
134 */
135 @android.view.RemotableViewMethod
136 public void setCountDown(boolean countDown) {
137 mCountDown = countDown;
Selim Cineka2a01712016-05-18 16:59:07 -0700138 updateText(SystemClock.elapsedRealtime());
Selim Cineked1a33c2016-02-18 17:12:57 -0800139 }
140
141 /**
142 * @return whether this view counts down
143 *
144 * @see #setCountDown(boolean)
145 */
146 public boolean isCountDown() {
147 return mCountDown;
148 }
149
150 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * Set the time that the count-up timer is in reference to.
152 *
153 * @param base Use the {@link SystemClock#elapsedRealtime} time base.
154 */
155 @android.view.RemotableViewMethod
156 public void setBase(long base) {
157 mBase = base;
158 dispatchChronometerTick();
159 updateText(SystemClock.elapsedRealtime());
160 }
161
162 /**
163 * Return the base time as set through {@link #setBase}.
164 */
165 public long getBase() {
166 return mBase;
167 }
168
169 /**
170 * Sets the format string used for display. The Chronometer will display
171 * this string, with the first "%s" replaced by the current timer value in
172 * "MM:SS" or "H:MM:SS" form.
173 *
174 * If the format string is null, or if you never call setFormat(), the
175 * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
176 * form.
177 *
178 * @param format the format string.
179 */
180 @android.view.RemotableViewMethod
181 public void setFormat(String format) {
182 mFormat = format;
183 if (format != null && mFormatBuilder == null) {
184 mFormatBuilder = new StringBuilder(format.length() * 2);
185 }
186 }
187
188 /**
189 * Returns the current format string as set through {@link #setFormat}.
190 */
191 public String getFormat() {
192 return mFormat;
193 }
194
195 /**
196 * Sets the listener to be called when the chronometer changes.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700197 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 * @param listener The listener.
199 */
200 public void setOnChronometerTickListener(OnChronometerTickListener listener) {
201 mOnChronometerTickListener = listener;
202 }
203
204 /**
205 * @return The listener (may be null) that is listening for chronometer change
206 * events.
207 */
208 public OnChronometerTickListener getOnChronometerTickListener() {
209 return mOnChronometerTickListener;
210 }
211
212 /**
213 * Start counting up. This does not affect the base as set from {@link #setBase}, just
214 * the view display.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700215 *
216 * Chronometer works by regularly scheduling messages to the handler, even when the
217 * Widget is not visible. To make sure resource leaks do not occur, the user should
218 * make sure that each start() call has a reciprocal call to {@link #stop}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public void start() {
221 mStarted = true;
222 updateRunning();
223 }
224
225 /**
226 * Stop counting up. This does not affect the base as set from {@link #setBase}, just
227 * the view display.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700228 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * This stops the messages to the handler, effectively releasing resources that would
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700230 * be held as the chronometer is running, via {@link #start}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 */
232 public void stop() {
233 mStarted = false;
234 updateRunning();
235 }
236
237 /**
238 * The same as calling {@link #start} or {@link #stop}.
Jeffrey Sharkey3ff7eb92009-04-13 16:57:28 -0700239 * @hide pending API council approval
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 @android.view.RemotableViewMethod
242 public void setStarted(boolean started) {
243 mStarted = started;
244 updateRunning();
245 }
246
247 @Override
248 protected void onDetachedFromWindow() {
249 super.onDetachedFromWindow();
250 mVisible = false;
251 updateRunning();
252 }
253
254 @Override
255 protected void onWindowVisibilityChanged(int visibility) {
256 super.onWindowVisibilityChanged(visibility);
257 mVisible = visibility == VISIBLE;
258 updateRunning();
259 }
260
Simon Dubray814e1f52015-11-05 11:38:40 +0100261 @Override
262 protected void onVisibilityChanged(View changedView, int visibility) {
263 super.onVisibilityChanged(changedView, visibility);
264 updateRunning();
265 }
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 private synchronized void updateText(long now) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400268 mNow = now;
Selim Cineked1a33c2016-02-18 17:12:57 -0800269 long seconds = mCountDown ? mBase - now : now - mBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 seconds /= 1000;
Selim Cineked1a33c2016-02-18 17:12:57 -0800271 boolean negative = false;
272 if (seconds < 0) {
273 seconds = -seconds;
274 negative = true;
275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 String text = DateUtils.formatElapsedTime(mRecycle, seconds);
Selim Cineked1a33c2016-02-18 17:12:57 -0800277 if (negative) {
278 text = getResources().getString(R.string.negative_duration, text);
279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280
281 if (mFormat != null) {
282 Locale loc = Locale.getDefault();
283 if (mFormatter == null || !loc.equals(mFormatterLocale)) {
284 mFormatterLocale = loc;
285 mFormatter = new Formatter(mFormatBuilder, loc);
286 }
287 mFormatBuilder.setLength(0);
288 mFormatterArgs[0] = text;
289 try {
290 mFormatter.format(mFormat, mFormatterArgs);
291 text = mFormatBuilder.toString();
292 } catch (IllegalFormatException ex) {
293 if (!mLogged) {
294 Log.w(TAG, "Illegal format string: " + mFormat);
295 mLogged = true;
296 }
297 }
298 }
299 setText(text);
300 }
301
302 private void updateRunning() {
Simon Dubray814e1f52015-11-05 11:38:40 +0100303 boolean running = mVisible && mStarted && isShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 if (running != mRunning) {
305 if (running) {
306 updateText(SystemClock.elapsedRealtime());
307 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700308 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 } else {
John Reckd0374c62015-10-20 13:25:01 -0700310 removeCallbacks(mTickRunnable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312 mRunning = running;
313 }
314 }
John Reckd0374c62015-10-20 13:25:01 -0700315
316 private final Runnable mTickRunnable = new Runnable() {
317 @Override
318 public void run() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 if (mRunning) {
320 updateText(SystemClock.elapsedRealtime());
321 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700322 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324 }
325 };
326
327 void dispatchChronometerTick() {
328 if (mOnChronometerTickListener != null) {
329 mOnChronometerTickListener.onChronometerTick(this);
330 }
331 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800332
Dan Sandlera79a7472015-06-05 16:52:22 -0400333 private static final int MIN_IN_SEC = 60;
334 private static final int HOUR_IN_SEC = MIN_IN_SEC*60;
335 private static String formatDuration(long ms) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400336 int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS);
337 if (duration < 0) {
338 duration = -duration;
339 }
340
341 int h = 0;
342 int m = 0;
343
344 if (duration >= HOUR_IN_SEC) {
345 h = duration / HOUR_IN_SEC;
346 duration -= h * HOUR_IN_SEC;
347 }
348 if (duration >= MIN_IN_SEC) {
349 m = duration / MIN_IN_SEC;
350 duration -= m * MIN_IN_SEC;
351 }
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800352 final int s = duration;
Dan Sandlera79a7472015-06-05 16:52:22 -0400353
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800354 final ArrayList<Measure> measures = new ArrayList<Measure>();
355 if (h > 0) {
356 measures.add(new Measure(h, MeasureUnit.HOUR));
Dan Sandlera79a7472015-06-05 16:52:22 -0400357 }
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800358 if (m > 0) {
359 measures.add(new Measure(m, MeasureUnit.MINUTE));
360 }
361 measures.add(new Measure(s, MeasureUnit.SECOND));
362
363 return MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE)
364 .formatMeasures((Measure[]) measures.toArray());
Dan Sandlera79a7472015-06-05 16:52:22 -0400365 }
366
367 @Override
368 public CharSequence getContentDescription() {
369 return formatDuration(mNow - mBase);
370 }
371
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800372 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800373 public CharSequence getAccessibilityClassName() {
374 return Chronometer.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}