The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 21 | import android.icu.text.MeasureFormat; |
| 22 | import android.icu.text.MeasureFormat.FormatWidth; |
| 23 | import android.icu.util.Measure; |
| 24 | import android.icu.util.MeasureUnit; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.SystemClock; |
| 26 | import android.text.format.DateUtils; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.util.Log; |
Simon Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 29 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.widget.RemoteViews.RemoteView; |
| 31 | |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 32 | import com.android.internal.R; |
| 33 | |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import java.util.Formatter; |
| 36 | import java.util.IllegalFormatException; |
| 37 | import 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 Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 44 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * 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 Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 54 | * @attr ref android.R.styleable#Chronometer_countDown |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | */ |
| 56 | @RemoteView |
| 57 | public 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 Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 73 | private long mNow; // the currently displayed time |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | 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 Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 85 | private boolean mCountDown; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
| 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 Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 107 | public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) { |
| 108 | this(context, attrs, defStyleAttr, 0); |
| 109 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 111 | 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 Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 116 | setFormat(a.getString(R.styleable.Chronometer_format)); |
| 117 | setCountDown(a.getBoolean(R.styleable.Chronometer_countDown, false)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | a.recycle(); |
| 119 | |
| 120 | init(); |
| 121 | } |
| 122 | |
| 123 | private void init() { |
| 124 | mBase = SystemClock.elapsedRealtime(); |
| 125 | updateText(mBase); |
| 126 | } |
| 127 | |
| 128 | /** |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 129 | * 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 Cinek | a2a0171 | 2016-05-18 16:59:07 -0700 | [diff] [blame] | 138 | updateText(SystemClock.elapsedRealtime()); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 139 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | * 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 Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 197 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | * @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 Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 215 | * |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | */ |
| 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 Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 228 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | * This stops the messages to the handler, effectively releasing resources that would |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 230 | * be held as the chronometer is running, via {@link #start}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | */ |
| 232 | public void stop() { |
| 233 | mStarted = false; |
| 234 | updateRunning(); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * The same as calling {@link #start} or {@link #stop}. |
Jeffrey Sharkey | 3ff7eb9 | 2009-04-13 16:57:28 -0700 | [diff] [blame] | 239 | * @hide pending API council approval |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | */ |
| 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 Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 261 | @Override |
| 262 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 263 | super.onVisibilityChanged(changedView, visibility); |
| 264 | updateRunning(); |
| 265 | } |
| 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | private synchronized void updateText(long now) { |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 268 | mNow = now; |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 269 | long seconds = mCountDown ? mBase - now : now - mBase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | seconds /= 1000; |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 271 | boolean negative = false; |
| 272 | if (seconds < 0) { |
| 273 | seconds = -seconds; |
| 274 | negative = true; |
| 275 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | String text = DateUtils.formatElapsedTime(mRecycle, seconds); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 277 | if (negative) { |
| 278 | text = getResources().getString(R.string.negative_duration, text); |
| 279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
| 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 Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 303 | boolean running = mVisible && mStarted && isShown(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | if (running != mRunning) { |
| 305 | if (running) { |
| 306 | updateText(SystemClock.elapsedRealtime()); |
| 307 | dispatchChronometerTick(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 308 | postDelayed(mTickRunnable, 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } else { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 310 | removeCallbacks(mTickRunnable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | mRunning = running; |
| 313 | } |
| 314 | } |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 315 | |
| 316 | private final Runnable mTickRunnable = new Runnable() { |
| 317 | @Override |
| 318 | public void run() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | if (mRunning) { |
| 320 | updateText(SystemClock.elapsedRealtime()); |
| 321 | dispatchChronometerTick(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 322 | postDelayed(mTickRunnable, 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | void dispatchChronometerTick() { |
| 328 | if (mOnChronometerTickListener != null) { |
| 329 | mOnChronometerTickListener.onChronometerTick(this); |
| 330 | } |
| 331 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 332 | |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 333 | 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 Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 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 | } |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 352 | final int s = duration; |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 353 | |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 354 | final ArrayList<Measure> measures = new ArrayList<Measure>(); |
| 355 | if (h > 0) { |
| 356 | measures.add(new Measure(h, MeasureUnit.HOUR)); |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 357 | } |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 358 | 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) |
Roozbeh Pournader | 63a13cf | 2016-12-19 15:56:40 -0800 | [diff] [blame^] | 364 | .formatMeasures(measures.toArray(new Measure[measures.size()])); |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public CharSequence getContentDescription() { |
| 369 | return formatDuration(mNow - mBase); |
| 370 | } |
| 371 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 372 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 373 | public CharSequence getAccessibilityClassName() { |
| 374 | return Chronometer.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |