blob: adcb012674a8727105bc0bcd860b408598d7e3d3 [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;
Dan Sandlera79a7472015-06-05 16:52:22 -040020import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.os.SystemClock;
23import android.text.format.DateUtils;
24import android.util.AttributeSet;
25import android.util.Log;
Simon Dubray814e1f52015-11-05 11:38:40 +010026import android.view.View;
Dan Sandlera79a7472015-06-05 16:52:22 -040027import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.widget.RemoteViews.RemoteView;
29
Selim Cineked1a33c2016-02-18 17:12:57 -080030import com.android.internal.R;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import java.util.Formatter;
33import java.util.IllegalFormatException;
34import java.util.Locale;
35
36/**
37 * Class that implements a simple timer.
38 * <p>
39 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
40 * 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 -080041 * time at which you call {@link #start}.
42 *
43 * <p>The timer can also count downward towards the base time by
44 * setting {@link #setCountDown(boolean)} to true.
45 *
46 * <p>By default it will display the current
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
48 * to format the timer value into an arbitrary string.
49 *
50 * @attr ref android.R.styleable#Chronometer_format
Selim Cineked1a33c2016-02-18 17:12:57 -080051 * @attr ref android.R.styleable#Chronometer_countDown
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 */
53@RemoteView
54public class Chronometer extends TextView {
55 private static final String TAG = "Chronometer";
56
57 /**
58 * A callback that notifies when the chronometer has incremented on its own.
59 */
60 public interface OnChronometerTickListener {
61
62 /**
63 * Notification that the chronometer has changed.
64 */
65 void onChronometerTick(Chronometer chronometer);
66
67 }
68
69 private long mBase;
Dan Sandlera79a7472015-06-05 16:52:22 -040070 private long mNow; // the currently displayed time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 private boolean mVisible;
72 private boolean mStarted;
73 private boolean mRunning;
74 private boolean mLogged;
75 private String mFormat;
76 private Formatter mFormatter;
77 private Locale mFormatterLocale;
78 private Object[] mFormatterArgs = new Object[1];
79 private StringBuilder mFormatBuilder;
80 private OnChronometerTickListener mOnChronometerTickListener;
81 private StringBuilder mRecycle = new StringBuilder(8);
Selim Cineked1a33c2016-02-18 17:12:57 -080082 private boolean mCountDown;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * Initialize this Chronometer object.
86 * Sets the base to the current time.
87 */
88 public Chronometer(Context context) {
89 this(context, null, 0);
90 }
91
92 /**
93 * Initialize with standard view layout information.
94 * Sets the base to the current time.
95 */
96 public Chronometer(Context context, AttributeSet attrs) {
97 this(context, attrs, 0);
98 }
99
100 /**
101 * Initialize with standard view layout information and style.
102 * Sets the base to the current time.
103 */
Alan Viverette617feb92013-09-09 18:09:13 -0700104 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) {
105 this(context, attrs, defStyleAttr, 0);
106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Alan Viverette617feb92013-09-09 18:09:13 -0700108 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
109 super(context, attrs, defStyleAttr, defStyleRes);
110
111 final TypedArray a = context.obtainStyledAttributes(
112 attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
Selim Cineked1a33c2016-02-18 17:12:57 -0800113 setFormat(a.getString(R.styleable.Chronometer_format));
114 setCountDown(a.getBoolean(R.styleable.Chronometer_countDown, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 a.recycle();
116
117 init();
118 }
119
120 private void init() {
121 mBase = SystemClock.elapsedRealtime();
122 updateText(mBase);
123 }
124
125 /**
Selim Cineked1a33c2016-02-18 17:12:57 -0800126 * Set this view to count down to the base instead of counting up from it.
127 *
128 * @param countDown whether this view should count down
129 *
130 * @see #setBase(long)
131 */
132 @android.view.RemotableViewMethod
133 public void setCountDown(boolean countDown) {
134 mCountDown = countDown;
Selim Cineka2a01712016-05-18 16:59:07 -0700135 updateText(SystemClock.elapsedRealtime());
Selim Cineked1a33c2016-02-18 17:12:57 -0800136 }
137
138 /**
139 * @return whether this view counts down
140 *
141 * @see #setCountDown(boolean)
142 */
143 public boolean isCountDown() {
144 return mCountDown;
145 }
146
147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * Set the time that the count-up timer is in reference to.
149 *
150 * @param base Use the {@link SystemClock#elapsedRealtime} time base.
151 */
152 @android.view.RemotableViewMethod
153 public void setBase(long base) {
154 mBase = base;
155 dispatchChronometerTick();
156 updateText(SystemClock.elapsedRealtime());
157 }
158
159 /**
160 * Return the base time as set through {@link #setBase}.
161 */
162 public long getBase() {
163 return mBase;
164 }
165
166 /**
167 * Sets the format string used for display. The Chronometer will display
168 * this string, with the first "%s" replaced by the current timer value in
169 * "MM:SS" or "H:MM:SS" form.
170 *
171 * If the format string is null, or if you never call setFormat(), the
172 * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
173 * form.
174 *
175 * @param format the format string.
176 */
177 @android.view.RemotableViewMethod
178 public void setFormat(String format) {
179 mFormat = format;
180 if (format != null && mFormatBuilder == null) {
181 mFormatBuilder = new StringBuilder(format.length() * 2);
182 }
183 }
184
185 /**
186 * Returns the current format string as set through {@link #setFormat}.
187 */
188 public String getFormat() {
189 return mFormat;
190 }
191
192 /**
193 * Sets the listener to be called when the chronometer changes.
194 *
195 * @param listener The listener.
196 */
197 public void setOnChronometerTickListener(OnChronometerTickListener listener) {
198 mOnChronometerTickListener = listener;
199 }
200
201 /**
202 * @return The listener (may be null) that is listening for chronometer change
203 * events.
204 */
205 public OnChronometerTickListener getOnChronometerTickListener() {
206 return mOnChronometerTickListener;
207 }
208
209 /**
210 * Start counting up. This does not affect the base as set from {@link #setBase}, just
211 * the view display.
212 *
213 * Chronometer works by regularly scheduling messages to the handler, even when the
214 * Widget is not visible. To make sure resource leaks do not occur, the user should
215 * make sure that each start() call has a reciprocal call to {@link #stop}.
216 */
217 public void start() {
218 mStarted = true;
219 updateRunning();
220 }
221
222 /**
223 * Stop counting up. This does not affect the base as set from {@link #setBase}, just
224 * the view display.
225 *
226 * This stops the messages to the handler, effectively releasing resources that would
227 * be held as the chronometer is running, via {@link #start}.
228 */
229 public void stop() {
230 mStarted = false;
231 updateRunning();
232 }
233
234 /**
235 * The same as calling {@link #start} or {@link #stop}.
Jeffrey Sharkey3ff7eb92009-04-13 16:57:28 -0700236 * @hide pending API council approval
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 @android.view.RemotableViewMethod
239 public void setStarted(boolean started) {
240 mStarted = started;
241 updateRunning();
242 }
243
244 @Override
245 protected void onDetachedFromWindow() {
246 super.onDetachedFromWindow();
247 mVisible = false;
248 updateRunning();
249 }
250
251 @Override
252 protected void onWindowVisibilityChanged(int visibility) {
253 super.onWindowVisibilityChanged(visibility);
254 mVisible = visibility == VISIBLE;
255 updateRunning();
256 }
257
Simon Dubray814e1f52015-11-05 11:38:40 +0100258 @Override
259 protected void onVisibilityChanged(View changedView, int visibility) {
260 super.onVisibilityChanged(changedView, visibility);
261 updateRunning();
262 }
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 private synchronized void updateText(long now) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400265 mNow = now;
Selim Cineked1a33c2016-02-18 17:12:57 -0800266 long seconds = mCountDown ? mBase - now : now - mBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 seconds /= 1000;
Selim Cineked1a33c2016-02-18 17:12:57 -0800268 boolean negative = false;
269 if (seconds < 0) {
270 seconds = -seconds;
271 negative = true;
272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 String text = DateUtils.formatElapsedTime(mRecycle, seconds);
Selim Cineked1a33c2016-02-18 17:12:57 -0800274 if (negative) {
275 text = getResources().getString(R.string.negative_duration, text);
276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
278 if (mFormat != null) {
279 Locale loc = Locale.getDefault();
280 if (mFormatter == null || !loc.equals(mFormatterLocale)) {
281 mFormatterLocale = loc;
282 mFormatter = new Formatter(mFormatBuilder, loc);
283 }
284 mFormatBuilder.setLength(0);
285 mFormatterArgs[0] = text;
286 try {
287 mFormatter.format(mFormat, mFormatterArgs);
288 text = mFormatBuilder.toString();
289 } catch (IllegalFormatException ex) {
290 if (!mLogged) {
291 Log.w(TAG, "Illegal format string: " + mFormat);
292 mLogged = true;
293 }
294 }
295 }
296 setText(text);
297 }
298
299 private void updateRunning() {
Simon Dubray814e1f52015-11-05 11:38:40 +0100300 boolean running = mVisible && mStarted && isShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 if (running != mRunning) {
302 if (running) {
303 updateText(SystemClock.elapsedRealtime());
304 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700305 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 } else {
John Reckd0374c62015-10-20 13:25:01 -0700307 removeCallbacks(mTickRunnable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
309 mRunning = running;
310 }
311 }
John Reckd0374c62015-10-20 13:25:01 -0700312
313 private final Runnable mTickRunnable = new Runnable() {
314 @Override
315 public void run() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 if (mRunning) {
317 updateText(SystemClock.elapsedRealtime());
318 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700319 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 }
321 }
322 };
323
324 void dispatchChronometerTick() {
325 if (mOnChronometerTickListener != null) {
326 mOnChronometerTickListener.onChronometerTick(this);
327 }
328 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800329
Dan Sandlera79a7472015-06-05 16:52:22 -0400330 private static final int MIN_IN_SEC = 60;
331 private static final int HOUR_IN_SEC = MIN_IN_SEC*60;
332 private static String formatDuration(long ms) {
333 final Resources res = Resources.getSystem();
334 final StringBuilder text = new StringBuilder();
335
336 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 }
352 int s = duration;
353
354 try {
355 if (h > 0) {
356 text.append(res.getQuantityString(
357 com.android.internal.R.plurals.duration_hours, h, h));
358 }
359 if (m > 0) {
360 if (text.length() > 0) {
361 text.append(' ');
362 }
363 text.append(res.getQuantityString(
364 com.android.internal.R.plurals.duration_minutes, m, m));
365 }
366
367 if (text.length() > 0) {
368 text.append(' ');
369 }
370 text.append(res.getQuantityString(
371 com.android.internal.R.plurals.duration_seconds, s, s));
372 } catch (Resources.NotFoundException e) {
373 // Ignore; plurals throws an exception for an untranslated quantity for a given locale.
374 return null;
375 }
376 return text.toString();
377 }
378
379 @Override
380 public CharSequence getContentDescription() {
381 return formatDuration(mNow - mBase);
382 }
383
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800384 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800385 public CharSequence getAccessibilityClassName() {
386 return Chronometer.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388}