2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget;
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Canvas;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.os.SystemClock;
25 import android.text.format.DateUtils;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 import android.widget.RemoteViews.RemoteView;
30 import java.util.Formatter;
31 import java.util.IllegalFormatException;
32 import java.util.Locale;
35 * Class that implements a simple timer.
37 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
38 * and it counts up from that, or if you don't give it a base time, it will use the
39 * time at which you call {@link #start}. By default it will display the current
40 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
41 * to format the timer value into an arbitrary string.
43 * @attr ref android.R.styleable#Chronometer_format
46 public class Chronometer extends TextView {
47 private static final String TAG = "Chronometer";
50 * A callback that notifies when the chronometer has incremented on its own.
52 public interface OnChronometerTickListener {
55 * Notification that the chronometer has changed.
57 void onChronometerTick(Chronometer chronometer);
62 private boolean mVisible;
63 private boolean mStarted;
64 private boolean mRunning;
65 private boolean mLogged;
66 private String mFormat;
67 private Formatter mFormatter;
68 private Locale mFormatterLocale;
69 private Object[] mFormatterArgs = new Object[1];
70 private StringBuilder mFormatBuilder;
71 private OnChronometerTickListener mOnChronometerTickListener;
72 private StringBuilder mRecycle = new StringBuilder(8);
74 private static final int TICK_WHAT = 2;
77 * Initialize this Chronometer object.
78 * Sets the base to the current time.
80 public Chronometer(Context context) {
81 this(context, null, 0);
85 * Initialize with standard view layout information.
86 * Sets the base to the current time.
88 public Chronometer(Context context, AttributeSet attrs) {
89 this(context, attrs, 0);
93 * Initialize with standard view layout information and style.
94 * Sets the base to the current time.
96 public Chronometer(Context context, AttributeSet attrs, int defStyle) {
97 super(context, attrs, defStyle);
99 TypedArray a = context.obtainStyledAttributes(
101 com.android.internal.R.styleable.Chronometer, defStyle, 0);
102 setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
108 private void init() {
109 mBase = SystemClock.elapsedRealtime();
114 * Set the time that the count-up timer is in reference to.
116 * @param base Use the {@link SystemClock#elapsedRealtime} time base.
118 @android.view.RemotableViewMethod
119 public void setBase(long base) {
121 dispatchChronometerTick();
122 updateText(SystemClock.elapsedRealtime());
126 * Return the base time as set through {@link #setBase}.
128 public long getBase() {
133 * Sets the format string used for display. The Chronometer will display
134 * this string, with the first "%s" replaced by the current timer value in
135 * "MM:SS" or "H:MM:SS" form.
137 * If the format string is null, or if you never call setFormat(), the
138 * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
141 * @param format the format string.
143 @android.view.RemotableViewMethod
144 public void setFormat(String format) {
146 if (format != null && mFormatBuilder == null) {
147 mFormatBuilder = new StringBuilder(format.length() * 2);
152 * Returns the current format string as set through {@link #setFormat}.
154 public String getFormat() {
159 * Sets the listener to be called when the chronometer changes.
161 * @param listener The listener.
163 public void setOnChronometerTickListener(OnChronometerTickListener listener) {
164 mOnChronometerTickListener = listener;
168 * @return The listener (may be null) that is listening for chronometer change
171 public OnChronometerTickListener getOnChronometerTickListener() {
172 return mOnChronometerTickListener;
176 * Start counting up. This does not affect the base as set from {@link #setBase}, just
179 * Chronometer works by regularly scheduling messages to the handler, even when the
180 * Widget is not visible. To make sure resource leaks do not occur, the user should
181 * make sure that each start() call has a reciprocal call to {@link #stop}.
183 public void start() {
189 * Stop counting up. This does not affect the base as set from {@link #setBase}, just
192 * This stops the messages to the handler, effectively releasing resources that would
193 * be held as the chronometer is running, via {@link #start}.
201 * The same as calling {@link #start} or {@link #stop}.
202 * @hide pending API council approval
204 @android.view.RemotableViewMethod
205 public void setStarted(boolean started) {
211 protected void onDetachedFromWindow() {
212 super.onDetachedFromWindow();
218 protected void onWindowVisibilityChanged(int visibility) {
219 super.onWindowVisibilityChanged(visibility);
220 mVisible = visibility == VISIBLE;
224 private synchronized void updateText(long now) {
225 long seconds = now - mBase;
227 String text = DateUtils.formatElapsedTime(mRecycle, seconds);
229 if (mFormat != null) {
230 Locale loc = Locale.getDefault();
231 if (mFormatter == null || !loc.equals(mFormatterLocale)) {
232 mFormatterLocale = loc;
233 mFormatter = new Formatter(mFormatBuilder, loc);
235 mFormatBuilder.setLength(0);
236 mFormatterArgs[0] = text;
238 mFormatter.format(mFormat, mFormatterArgs);
239 text = mFormatBuilder.toString();
240 } catch (IllegalFormatException ex) {
242 Log.w(TAG, "Illegal format string: " + mFormat);
250 private void updateRunning() {
251 boolean running = mVisible && mStarted;
252 if (running != mRunning) {
254 updateText(SystemClock.elapsedRealtime());
255 dispatchChronometerTick();
256 mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
258 mHandler.removeMessages(TICK_WHAT);
264 private Handler mHandler = new Handler() {
265 public void handleMessage(Message m) {
267 updateText(SystemClock.elapsedRealtime());
268 dispatchChronometerTick();
269 sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
274 void dispatchChronometerTick() {
275 if (mOnChronometerTickListener != null) {
276 mOnChronometerTickListener.onChronometerTick(this);