blob: 151326449011cd7ad9254b9d123c4d3b142f08bc [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.Handler;
23import android.os.Message;
24import android.os.SystemClock;
25import android.text.format.DateUtils;
26import android.util.AttributeSet;
27import android.util.Log;
Simon Dubray814e1f52015-11-05 11:38:40 +010028import android.view.View;
Dan Sandlera79a7472015-06-05 16:52:22 -040029import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.widget.RemoteViews.RemoteView;
31
32import 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
41 * time at which you call {@link #start}. By default it will display the current
42 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
43 * to format the timer value into an arbitrary string.
44 *
45 * @attr ref android.R.styleable#Chronometer_format
46 */
47@RemoteView
48public class Chronometer extends TextView {
49 private static final String TAG = "Chronometer";
50
51 /**
52 * A callback that notifies when the chronometer has incremented on its own.
53 */
54 public interface OnChronometerTickListener {
55
56 /**
57 * Notification that the chronometer has changed.
58 */
59 void onChronometerTick(Chronometer chronometer);
60
61 }
62
63 private long mBase;
Dan Sandlera79a7472015-06-05 16:52:22 -040064 private long mNow; // the currently displayed time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private boolean mVisible;
66 private boolean mStarted;
67 private boolean mRunning;
68 private boolean mLogged;
69 private String mFormat;
70 private Formatter mFormatter;
71 private Locale mFormatterLocale;
72 private Object[] mFormatterArgs = new Object[1];
73 private StringBuilder mFormatBuilder;
74 private OnChronometerTickListener mOnChronometerTickListener;
75 private StringBuilder mRecycle = new StringBuilder(8);
76
77 private static final int TICK_WHAT = 2;
78
79 /**
80 * Initialize this Chronometer object.
81 * Sets the base to the current time.
82 */
83 public Chronometer(Context context) {
84 this(context, null, 0);
85 }
86
87 /**
88 * Initialize with standard view layout information.
89 * Sets the base to the current time.
90 */
91 public Chronometer(Context context, AttributeSet attrs) {
92 this(context, attrs, 0);
93 }
94
95 /**
96 * Initialize with standard view layout information and style.
97 * Sets the base to the current time.
98 */
Alan Viverette617feb92013-09-09 18:09:13 -070099 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) {
100 this(context, attrs, defStyleAttr, 0);
101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Alan Viverette617feb92013-09-09 18:09:13 -0700103 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
104 super(context, attrs, defStyleAttr, defStyleRes);
105
106 final TypedArray a = context.obtainStyledAttributes(
107 attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
109 a.recycle();
110
111 init();
112 }
113
114 private void init() {
115 mBase = SystemClock.elapsedRealtime();
116 updateText(mBase);
117 }
118
119 /**
120 * Set the time that the count-up timer is in reference to.
121 *
122 * @param base Use the {@link SystemClock#elapsedRealtime} time base.
123 */
124 @android.view.RemotableViewMethod
125 public void setBase(long base) {
126 mBase = base;
127 dispatchChronometerTick();
128 updateText(SystemClock.elapsedRealtime());
129 }
130
131 /**
132 * Return the base time as set through {@link #setBase}.
133 */
134 public long getBase() {
135 return mBase;
136 }
137
138 /**
139 * Sets the format string used for display. The Chronometer will display
140 * this string, with the first "%s" replaced by the current timer value in
141 * "MM:SS" or "H:MM:SS" form.
142 *
143 * If the format string is null, or if you never call setFormat(), the
144 * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
145 * form.
146 *
147 * @param format the format string.
148 */
149 @android.view.RemotableViewMethod
150 public void setFormat(String format) {
151 mFormat = format;
152 if (format != null && mFormatBuilder == null) {
153 mFormatBuilder = new StringBuilder(format.length() * 2);
154 }
155 }
156
157 /**
158 * Returns the current format string as set through {@link #setFormat}.
159 */
160 public String getFormat() {
161 return mFormat;
162 }
163
164 /**
165 * Sets the listener to be called when the chronometer changes.
166 *
167 * @param listener The listener.
168 */
169 public void setOnChronometerTickListener(OnChronometerTickListener listener) {
170 mOnChronometerTickListener = listener;
171 }
172
173 /**
174 * @return The listener (may be null) that is listening for chronometer change
175 * events.
176 */
177 public OnChronometerTickListener getOnChronometerTickListener() {
178 return mOnChronometerTickListener;
179 }
180
181 /**
182 * Start counting up. This does not affect the base as set from {@link #setBase}, just
183 * the view display.
184 *
185 * Chronometer works by regularly scheduling messages to the handler, even when the
186 * Widget is not visible. To make sure resource leaks do not occur, the user should
187 * make sure that each start() call has a reciprocal call to {@link #stop}.
188 */
189 public void start() {
190 mStarted = true;
191 updateRunning();
192 }
193
194 /**
195 * Stop counting up. This does not affect the base as set from {@link #setBase}, just
196 * the view display.
197 *
198 * This stops the messages to the handler, effectively releasing resources that would
199 * be held as the chronometer is running, via {@link #start}.
200 */
201 public void stop() {
202 mStarted = false;
203 updateRunning();
204 }
205
206 /**
207 * The same as calling {@link #start} or {@link #stop}.
Jeffrey Sharkey3ff7eb92009-04-13 16:57:28 -0700208 * @hide pending API council approval
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 @android.view.RemotableViewMethod
211 public void setStarted(boolean started) {
212 mStarted = started;
213 updateRunning();
214 }
215
216 @Override
217 protected void onDetachedFromWindow() {
218 super.onDetachedFromWindow();
219 mVisible = false;
220 updateRunning();
221 }
222
223 @Override
224 protected void onWindowVisibilityChanged(int visibility) {
225 super.onWindowVisibilityChanged(visibility);
226 mVisible = visibility == VISIBLE;
227 updateRunning();
228 }
229
Simon Dubray814e1f52015-11-05 11:38:40 +0100230 @Override
231 protected void onVisibilityChanged(View changedView, int visibility) {
232 super.onVisibilityChanged(changedView, visibility);
233 updateRunning();
234 }
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 private synchronized void updateText(long now) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400237 mNow = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 long seconds = now - mBase;
239 seconds /= 1000;
240 String text = DateUtils.formatElapsedTime(mRecycle, seconds);
241
242 if (mFormat != null) {
243 Locale loc = Locale.getDefault();
244 if (mFormatter == null || !loc.equals(mFormatterLocale)) {
245 mFormatterLocale = loc;
246 mFormatter = new Formatter(mFormatBuilder, loc);
247 }
248 mFormatBuilder.setLength(0);
249 mFormatterArgs[0] = text;
250 try {
251 mFormatter.format(mFormat, mFormatterArgs);
252 text = mFormatBuilder.toString();
253 } catch (IllegalFormatException ex) {
254 if (!mLogged) {
255 Log.w(TAG, "Illegal format string: " + mFormat);
256 mLogged = true;
257 }
258 }
259 }
260 setText(text);
261 }
262
263 private void updateRunning() {
Simon Dubray814e1f52015-11-05 11:38:40 +0100264 boolean running = mVisible && mStarted && isShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 if (running != mRunning) {
266 if (running) {
267 updateText(SystemClock.elapsedRealtime());
268 dispatchChronometerTick();
269 mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
270 } else {
271 mHandler.removeMessages(TICK_WHAT);
272 }
273 mRunning = running;
274 }
275 }
276
277 private Handler mHandler = new Handler() {
278 public void handleMessage(Message m) {
279 if (mRunning) {
280 updateText(SystemClock.elapsedRealtime());
281 dispatchChronometerTick();
282 sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
283 }
284 }
285 };
286
287 void dispatchChronometerTick() {
288 if (mOnChronometerTickListener != null) {
289 mOnChronometerTickListener.onChronometerTick(this);
290 }
291 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800292
Dan Sandlera79a7472015-06-05 16:52:22 -0400293 private static final int MIN_IN_SEC = 60;
294 private static final int HOUR_IN_SEC = MIN_IN_SEC*60;
295 private static String formatDuration(long ms) {
296 final Resources res = Resources.getSystem();
297 final StringBuilder text = new StringBuilder();
298
299 int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS);
300 if (duration < 0) {
301 duration = -duration;
302 }
303
304 int h = 0;
305 int m = 0;
306
307 if (duration >= HOUR_IN_SEC) {
308 h = duration / HOUR_IN_SEC;
309 duration -= h * HOUR_IN_SEC;
310 }
311 if (duration >= MIN_IN_SEC) {
312 m = duration / MIN_IN_SEC;
313 duration -= m * MIN_IN_SEC;
314 }
315 int s = duration;
316
317 try {
318 if (h > 0) {
319 text.append(res.getQuantityString(
320 com.android.internal.R.plurals.duration_hours, h, h));
321 }
322 if (m > 0) {
323 if (text.length() > 0) {
324 text.append(' ');
325 }
326 text.append(res.getQuantityString(
327 com.android.internal.R.plurals.duration_minutes, m, m));
328 }
329
330 if (text.length() > 0) {
331 text.append(' ');
332 }
333 text.append(res.getQuantityString(
334 com.android.internal.R.plurals.duration_seconds, s, s));
335 } catch (Resources.NotFoundException e) {
336 // Ignore; plurals throws an exception for an untranslated quantity for a given locale.
337 return null;
338 }
339 return text.toString();
340 }
341
342 @Override
343 public CharSequence getContentDescription() {
344 return formatDuration(mNow - mBase);
345 }
346
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800347 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800348 public CharSequence getAccessibilityClassName() {
349 return Chronometer.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351}