blob: d11c03ad687863a1544552166a2ccfd18525780d [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;
Aurimas Liutikasba590a62017-04-14 14:51:59 -070020import android.content.Intent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.res.TypedArray;
Roozbeh Pournader241872a2016-12-15 15:17:49 -080022import android.icu.text.MeasureFormat;
23import android.icu.text.MeasureFormat.FormatWidth;
24import android.icu.util.Measure;
25import android.icu.util.MeasureUnit;
Aurimas Liutikasba590a62017-04-14 14:51:59 -070026import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.SystemClock;
28import android.text.format.DateUtils;
29import android.util.AttributeSet;
30import android.util.Log;
Simon Dubray814e1f52015-11-05 11:38:40 +010031import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.widget.RemoteViews.RemoteView;
33
Selim Cineked1a33c2016-02-18 17:12:57 -080034import com.android.internal.R;
35
Roozbeh Pournader241872a2016-12-15 15:17:49 -080036import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import java.util.Formatter;
38import java.util.IllegalFormatException;
39import java.util.Locale;
40
41/**
42 * Class that implements a simple timer.
43 * <p>
44 * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase,
45 * 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 -080046 * time at which you call {@link #start}.
47 *
48 * <p>The timer can also count downward towards the base time by
49 * setting {@link #setCountDown(boolean)} to true.
50 *
51 * <p>By default it will display the current
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat}
53 * to format the timer value into an arbitrary string.
54 *
55 * @attr ref android.R.styleable#Chronometer_format
Selim Cineked1a33c2016-02-18 17:12:57 -080056 * @attr ref android.R.styleable#Chronometer_countDown
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 */
58@RemoteView
59public class Chronometer extends TextView {
60 private static final String TAG = "Chronometer";
61
62 /**
63 * A callback that notifies when the chronometer has incremented on its own.
64 */
65 public interface OnChronometerTickListener {
66
67 /**
68 * Notification that the chronometer has changed.
69 */
70 void onChronometerTick(Chronometer chronometer);
71
72 }
73
74 private long mBase;
Dan Sandlera79a7472015-06-05 16:52:22 -040075 private long mNow; // the currently displayed time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 private boolean mVisible;
77 private boolean mStarted;
78 private boolean mRunning;
79 private boolean mLogged;
80 private String mFormat;
81 private Formatter mFormatter;
82 private Locale mFormatterLocale;
83 private Object[] mFormatterArgs = new Object[1];
84 private StringBuilder mFormatBuilder;
85 private OnChronometerTickListener mOnChronometerTickListener;
86 private StringBuilder mRecycle = new StringBuilder(8);
Selim Cineked1a33c2016-02-18 17:12:57 -080087 private boolean mCountDown;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
90 * Initialize this Chronometer object.
91 * Sets the base to the current time.
92 */
93 public Chronometer(Context context) {
94 this(context, null, 0);
95 }
96
97 /**
98 * Initialize with standard view layout information.
99 * Sets the base to the current time.
100 */
101 public Chronometer(Context context, AttributeSet attrs) {
102 this(context, attrs, 0);
103 }
104
105 /**
106 * Initialize with standard view layout information and style.
107 * Sets the base to the current time.
108 */
Alan Viverette617feb92013-09-09 18:09:13 -0700109 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) {
110 this(context, attrs, defStyleAttr, 0);
111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Alan Viverette617feb92013-09-09 18:09:13 -0700113 public Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
114 super(context, attrs, defStyleAttr, defStyleRes);
115
116 final TypedArray a = context.obtainStyledAttributes(
117 attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
Selim Cineked1a33c2016-02-18 17:12:57 -0800118 setFormat(a.getString(R.styleable.Chronometer_format));
119 setCountDown(a.getBoolean(R.styleable.Chronometer_countDown, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 a.recycle();
121
122 init();
123 }
124
125 private void init() {
126 mBase = SystemClock.elapsedRealtime();
127 updateText(mBase);
128 }
129
130 /**
Selim Cineked1a33c2016-02-18 17:12:57 -0800131 * Set this view to count down to the base instead of counting up from it.
132 *
133 * @param countDown whether this view should count down
134 *
135 * @see #setBase(long)
136 */
137 @android.view.RemotableViewMethod
138 public void setCountDown(boolean countDown) {
139 mCountDown = countDown;
Selim Cineka2a01712016-05-18 16:59:07 -0700140 updateText(SystemClock.elapsedRealtime());
Selim Cineked1a33c2016-02-18 17:12:57 -0800141 }
142
143 /**
144 * @return whether this view counts down
145 *
146 * @see #setCountDown(boolean)
147 */
148 public boolean isCountDown() {
149 return mCountDown;
150 }
151
152 /**
Aurimas Liutikasba590a62017-04-14 14:51:59 -0700153 * @return whether this is the final countdown
154 */
155 public boolean isTheFinalCountDown() {
156 try {
157 getContext().startActivity(
158 new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtu.be/9jK-NcRmVcw"))
159 .addCategory(Intent.CATEGORY_BROWSABLE)
160 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT
161 | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT));
162 return true;
163 } catch (Exception e) {
164 return false;
165 }
166 }
167
168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * Set the time that the count-up timer is in reference to.
170 *
171 * @param base Use the {@link SystemClock#elapsedRealtime} time base.
172 */
173 @android.view.RemotableViewMethod
174 public void setBase(long base) {
175 mBase = base;
176 dispatchChronometerTick();
177 updateText(SystemClock.elapsedRealtime());
178 }
179
180 /**
181 * Return the base time as set through {@link #setBase}.
182 */
183 public long getBase() {
184 return mBase;
185 }
186
187 /**
188 * Sets the format string used for display. The Chronometer will display
189 * this string, with the first "%s" replaced by the current timer value in
190 * "MM:SS" or "H:MM:SS" form.
191 *
192 * If the format string is null, or if you never call setFormat(), the
193 * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
194 * form.
195 *
196 * @param format the format string.
197 */
198 @android.view.RemotableViewMethod
199 public void setFormat(String format) {
200 mFormat = format;
201 if (format != null && mFormatBuilder == null) {
202 mFormatBuilder = new StringBuilder(format.length() * 2);
203 }
204 }
205
206 /**
207 * Returns the current format string as set through {@link #setFormat}.
208 */
209 public String getFormat() {
210 return mFormat;
211 }
212
213 /**
214 * Sets the listener to be called when the chronometer changes.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700215 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 * @param listener The listener.
217 */
218 public void setOnChronometerTickListener(OnChronometerTickListener listener) {
219 mOnChronometerTickListener = listener;
220 }
221
222 /**
223 * @return The listener (may be null) that is listening for chronometer change
224 * events.
225 */
226 public OnChronometerTickListener getOnChronometerTickListener() {
227 return mOnChronometerTickListener;
228 }
229
230 /**
231 * Start counting up. This does not affect the base as set from {@link #setBase}, just
232 * the view display.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700233 *
234 * Chronometer works by regularly scheduling messages to the handler, even when the
235 * Widget is not visible. To make sure resource leaks do not occur, the user should
236 * make sure that each start() call has a reciprocal call to {@link #stop}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 */
238 public void start() {
239 mStarted = true;
240 updateRunning();
241 }
242
243 /**
244 * Stop counting up. This does not affect the base as set from {@link #setBase}, just
245 * the view display.
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700246 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 * This stops the messages to the handler, effectively releasing resources that would
Aurimas Liutikas99441c52016-10-11 16:48:32 -0700248 * be held as the chronometer is running, via {@link #start}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public void stop() {
251 mStarted = false;
252 updateRunning();
253 }
254
255 /**
256 * The same as calling {@link #start} or {@link #stop}.
Jeffrey Sharkey3ff7eb92009-04-13 16:57:28 -0700257 * @hide pending API council approval
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 */
259 @android.view.RemotableViewMethod
260 public void setStarted(boolean started) {
261 mStarted = started;
262 updateRunning();
263 }
264
265 @Override
266 protected void onDetachedFromWindow() {
267 super.onDetachedFromWindow();
268 mVisible = false;
269 updateRunning();
270 }
271
272 @Override
273 protected void onWindowVisibilityChanged(int visibility) {
274 super.onWindowVisibilityChanged(visibility);
275 mVisible = visibility == VISIBLE;
276 updateRunning();
277 }
278
Simon Dubray814e1f52015-11-05 11:38:40 +0100279 @Override
280 protected void onVisibilityChanged(View changedView, int visibility) {
281 super.onVisibilityChanged(changedView, visibility);
282 updateRunning();
283 }
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 private synchronized void updateText(long now) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400286 mNow = now;
Selim Cineked1a33c2016-02-18 17:12:57 -0800287 long seconds = mCountDown ? mBase - now : now - mBase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 seconds /= 1000;
Selim Cineked1a33c2016-02-18 17:12:57 -0800289 boolean negative = false;
290 if (seconds < 0) {
291 seconds = -seconds;
292 negative = true;
293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 String text = DateUtils.formatElapsedTime(mRecycle, seconds);
Selim Cineked1a33c2016-02-18 17:12:57 -0800295 if (negative) {
296 text = getResources().getString(R.string.negative_duration, text);
297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
299 if (mFormat != null) {
300 Locale loc = Locale.getDefault();
301 if (mFormatter == null || !loc.equals(mFormatterLocale)) {
302 mFormatterLocale = loc;
303 mFormatter = new Formatter(mFormatBuilder, loc);
304 }
305 mFormatBuilder.setLength(0);
306 mFormatterArgs[0] = text;
307 try {
308 mFormatter.format(mFormat, mFormatterArgs);
309 text = mFormatBuilder.toString();
310 } catch (IllegalFormatException ex) {
311 if (!mLogged) {
312 Log.w(TAG, "Illegal format string: " + mFormat);
313 mLogged = true;
314 }
315 }
316 }
317 setText(text);
318 }
319
320 private void updateRunning() {
Simon Dubray814e1f52015-11-05 11:38:40 +0100321 boolean running = mVisible && mStarted && isShown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 if (running != mRunning) {
323 if (running) {
324 updateText(SystemClock.elapsedRealtime());
325 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700326 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 } else {
John Reckd0374c62015-10-20 13:25:01 -0700328 removeCallbacks(mTickRunnable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330 mRunning = running;
331 }
332 }
John Reckd0374c62015-10-20 13:25:01 -0700333
334 private final Runnable mTickRunnable = new Runnable() {
335 @Override
336 public void run() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 if (mRunning) {
338 updateText(SystemClock.elapsedRealtime());
339 dispatchChronometerTick();
John Reckd0374c62015-10-20 13:25:01 -0700340 postDelayed(mTickRunnable, 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 }
342 }
343 };
344
345 void dispatchChronometerTick() {
346 if (mOnChronometerTickListener != null) {
347 mOnChronometerTickListener.onChronometerTick(this);
348 }
349 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800350
Dan Sandlera79a7472015-06-05 16:52:22 -0400351 private static final int MIN_IN_SEC = 60;
352 private static final int HOUR_IN_SEC = MIN_IN_SEC*60;
353 private static String formatDuration(long ms) {
Dan Sandlera79a7472015-06-05 16:52:22 -0400354 int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS);
355 if (duration < 0) {
356 duration = -duration;
357 }
358
359 int h = 0;
360 int m = 0;
361
362 if (duration >= HOUR_IN_SEC) {
363 h = duration / HOUR_IN_SEC;
364 duration -= h * HOUR_IN_SEC;
365 }
366 if (duration >= MIN_IN_SEC) {
367 m = duration / MIN_IN_SEC;
368 duration -= m * MIN_IN_SEC;
369 }
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800370 final int s = duration;
Dan Sandlera79a7472015-06-05 16:52:22 -0400371
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800372 final ArrayList<Measure> measures = new ArrayList<Measure>();
373 if (h > 0) {
374 measures.add(new Measure(h, MeasureUnit.HOUR));
Dan Sandlera79a7472015-06-05 16:52:22 -0400375 }
Roozbeh Pournader241872a2016-12-15 15:17:49 -0800376 if (m > 0) {
377 measures.add(new Measure(m, MeasureUnit.MINUTE));
378 }
379 measures.add(new Measure(s, MeasureUnit.SECOND));
380
381 return MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE)
Roozbeh Pournader63a13cf2016-12-19 15:56:40 -0800382 .formatMeasures(measures.toArray(new Measure[measures.size()]));
Dan Sandlera79a7472015-06-05 16:52:22 -0400383 }
384
385 @Override
386 public CharSequence getContentDescription() {
387 return formatDuration(mNow - mBase);
388 }
389
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800390 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800391 public CharSequence getAccessibilityClassName() {
392 return Chronometer.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394}