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; |
Aurimas Liutikas | ba590a6 | 2017-04-14 14:51:59 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 22 | import android.icu.text.MeasureFormat; |
| 23 | import android.icu.text.MeasureFormat.FormatWidth; |
| 24 | import android.icu.util.Measure; |
| 25 | import android.icu.util.MeasureUnit; |
Aurimas Liutikas | ba590a6 | 2017-04-14 14:51:59 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.SystemClock; |
| 28 | import android.text.format.DateUtils; |
| 29 | import android.util.AttributeSet; |
| 30 | import android.util.Log; |
Simon Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 31 | import android.view.View; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame^] | 32 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.widget.RemoteViews.RemoteView; |
| 34 | |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 35 | import com.android.internal.R; |
| 36 | |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import java.util.Formatter; |
| 39 | import java.util.IllegalFormatException; |
| 40 | import java.util.Locale; |
| 41 | |
| 42 | /** |
| 43 | * Class that implements a simple timer. |
| 44 | * <p> |
| 45 | * You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase, |
| 46 | * 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] | 47 | * time at which you call {@link #start}. |
| 48 | * |
| 49 | * <p>The timer can also count downward towards the base time by |
| 50 | * setting {@link #setCountDown(boolean)} to true. |
| 51 | * |
| 52 | * <p>By default it will display the current |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | * timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat} |
| 54 | * to format the timer value into an arbitrary string. |
| 55 | * |
| 56 | * @attr ref android.R.styleable#Chronometer_format |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 57 | * @attr ref android.R.styleable#Chronometer_countDown |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | */ |
| 59 | @RemoteView |
| 60 | public class Chronometer extends TextView { |
| 61 | private static final String TAG = "Chronometer"; |
| 62 | |
| 63 | /** |
| 64 | * A callback that notifies when the chronometer has incremented on its own. |
| 65 | */ |
| 66 | public interface OnChronometerTickListener { |
| 67 | |
| 68 | /** |
| 69 | * Notification that the chronometer has changed. |
| 70 | */ |
| 71 | void onChronometerTick(Chronometer chronometer); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | private long mBase; |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 76 | private long mNow; // the currently displayed time |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | private boolean mVisible; |
| 78 | private boolean mStarted; |
| 79 | private boolean mRunning; |
| 80 | private boolean mLogged; |
| 81 | private String mFormat; |
| 82 | private Formatter mFormatter; |
| 83 | private Locale mFormatterLocale; |
| 84 | private Object[] mFormatterArgs = new Object[1]; |
| 85 | private StringBuilder mFormatBuilder; |
| 86 | private OnChronometerTickListener mOnChronometerTickListener; |
| 87 | private StringBuilder mRecycle = new StringBuilder(8); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 88 | private boolean mCountDown; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 89 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Initialize this Chronometer object. |
| 92 | * Sets the base to the current time. |
| 93 | */ |
| 94 | public Chronometer(Context context) { |
| 95 | this(context, null, 0); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Initialize with standard view layout information. |
| 100 | * Sets the base to the current time. |
| 101 | */ |
| 102 | public Chronometer(Context context, AttributeSet attrs) { |
| 103 | this(context, attrs, 0); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Initialize with standard view layout information and style. |
| 108 | * Sets the base to the current time. |
| 109 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 110 | public Chronometer(Context context, AttributeSet attrs, int defStyleAttr) { |
| 111 | this(context, attrs, defStyleAttr, 0); |
| 112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 114 | public Chronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 115 | super(context, attrs, defStyleAttr, defStyleRes); |
| 116 | |
| 117 | final TypedArray a = context.obtainStyledAttributes( |
| 118 | attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 119 | setFormat(a.getString(R.styleable.Chronometer_format)); |
| 120 | setCountDown(a.getBoolean(R.styleable.Chronometer_countDown, false)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | a.recycle(); |
| 122 | |
| 123 | init(); |
| 124 | } |
| 125 | |
| 126 | private void init() { |
| 127 | mBase = SystemClock.elapsedRealtime(); |
| 128 | updateText(mBase); |
| 129 | } |
| 130 | |
| 131 | /** |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 132 | * Set this view to count down to the base instead of counting up from it. |
| 133 | * |
| 134 | * @param countDown whether this view should count down |
| 135 | * |
| 136 | * @see #setBase(long) |
| 137 | */ |
| 138 | @android.view.RemotableViewMethod |
| 139 | public void setCountDown(boolean countDown) { |
| 140 | mCountDown = countDown; |
Selim Cinek | a2a0171 | 2016-05-18 16:59:07 -0700 | [diff] [blame] | 141 | updateText(SystemClock.elapsedRealtime()); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return whether this view counts down |
| 146 | * |
| 147 | * @see #setCountDown(boolean) |
| 148 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame^] | 149 | @InspectableProperty |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 150 | public boolean isCountDown() { |
| 151 | return mCountDown; |
| 152 | } |
| 153 | |
| 154 | /** |
Aurimas Liutikas | ba590a6 | 2017-04-14 14:51:59 -0700 | [diff] [blame] | 155 | * @return whether this is the final countdown |
| 156 | */ |
| 157 | public boolean isTheFinalCountDown() { |
| 158 | try { |
| 159 | getContext().startActivity( |
| 160 | new Intent(Intent.ACTION_VIEW, Uri.parse("https://youtu.be/9jK-NcRmVcw")) |
| 161 | .addCategory(Intent.CATEGORY_BROWSABLE) |
| 162 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
| 163 | | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)); |
| 164 | return true; |
| 165 | } catch (Exception e) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | * Set the time that the count-up timer is in reference to. |
| 172 | * |
| 173 | * @param base Use the {@link SystemClock#elapsedRealtime} time base. |
| 174 | */ |
| 175 | @android.view.RemotableViewMethod |
| 176 | public void setBase(long base) { |
| 177 | mBase = base; |
| 178 | dispatchChronometerTick(); |
| 179 | updateText(SystemClock.elapsedRealtime()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Return the base time as set through {@link #setBase}. |
| 184 | */ |
| 185 | public long getBase() { |
| 186 | return mBase; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Sets the format string used for display. The Chronometer will display |
| 191 | * this string, with the first "%s" replaced by the current timer value in |
| 192 | * "MM:SS" or "H:MM:SS" form. |
| 193 | * |
| 194 | * If the format string is null, or if you never call setFormat(), the |
| 195 | * Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS" |
| 196 | * form. |
| 197 | * |
| 198 | * @param format the format string. |
| 199 | */ |
| 200 | @android.view.RemotableViewMethod |
| 201 | public void setFormat(String format) { |
| 202 | mFormat = format; |
| 203 | if (format != null && mFormatBuilder == null) { |
| 204 | mFormatBuilder = new StringBuilder(format.length() * 2); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns the current format string as set through {@link #setFormat}. |
| 210 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame^] | 211 | @InspectableProperty |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | public String getFormat() { |
| 213 | return mFormat; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sets the listener to be called when the chronometer changes. |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 218 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | * @param listener The listener. |
| 220 | */ |
| 221 | public void setOnChronometerTickListener(OnChronometerTickListener listener) { |
| 222 | mOnChronometerTickListener = listener; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @return The listener (may be null) that is listening for chronometer change |
| 227 | * events. |
| 228 | */ |
| 229 | public OnChronometerTickListener getOnChronometerTickListener() { |
| 230 | return mOnChronometerTickListener; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Start counting up. This does not affect the base as set from {@link #setBase}, just |
| 235 | * the view display. |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 236 | * |
| 237 | * Chronometer works by regularly scheduling messages to the handler, even when the |
| 238 | * Widget is not visible. To make sure resource leaks do not occur, the user should |
| 239 | * 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] | 240 | */ |
| 241 | public void start() { |
| 242 | mStarted = true; |
| 243 | updateRunning(); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Stop counting up. This does not affect the base as set from {@link #setBase}, just |
| 248 | * the view display. |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 249 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | * This stops the messages to the handler, effectively releasing resources that would |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 251 | * 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] | 252 | */ |
| 253 | public void stop() { |
| 254 | mStarted = false; |
| 255 | updateRunning(); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * The same as calling {@link #start} or {@link #stop}. |
Jeffrey Sharkey | 3ff7eb9 | 2009-04-13 16:57:28 -0700 | [diff] [blame] | 260 | * @hide pending API council approval |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | */ |
| 262 | @android.view.RemotableViewMethod |
| 263 | public void setStarted(boolean started) { |
| 264 | mStarted = started; |
| 265 | updateRunning(); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | protected void onDetachedFromWindow() { |
| 270 | super.onDetachedFromWindow(); |
| 271 | mVisible = false; |
| 272 | updateRunning(); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | protected void onWindowVisibilityChanged(int visibility) { |
| 277 | super.onWindowVisibilityChanged(visibility); |
| 278 | mVisible = visibility == VISIBLE; |
| 279 | updateRunning(); |
| 280 | } |
| 281 | |
Simon Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 282 | @Override |
| 283 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 284 | super.onVisibilityChanged(changedView, visibility); |
| 285 | updateRunning(); |
| 286 | } |
| 287 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | private synchronized void updateText(long now) { |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 289 | mNow = now; |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 290 | long seconds = mCountDown ? mBase - now : now - mBase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | seconds /= 1000; |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 292 | boolean negative = false; |
| 293 | if (seconds < 0) { |
| 294 | seconds = -seconds; |
| 295 | negative = true; |
| 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | String text = DateUtils.formatElapsedTime(mRecycle, seconds); |
Selim Cinek | ed1a33c | 2016-02-18 17:12:57 -0800 | [diff] [blame] | 298 | if (negative) { |
| 299 | text = getResources().getString(R.string.negative_duration, text); |
| 300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
| 302 | if (mFormat != null) { |
| 303 | Locale loc = Locale.getDefault(); |
| 304 | if (mFormatter == null || !loc.equals(mFormatterLocale)) { |
| 305 | mFormatterLocale = loc; |
| 306 | mFormatter = new Formatter(mFormatBuilder, loc); |
| 307 | } |
| 308 | mFormatBuilder.setLength(0); |
| 309 | mFormatterArgs[0] = text; |
| 310 | try { |
| 311 | mFormatter.format(mFormat, mFormatterArgs); |
| 312 | text = mFormatBuilder.toString(); |
| 313 | } catch (IllegalFormatException ex) { |
| 314 | if (!mLogged) { |
| 315 | Log.w(TAG, "Illegal format string: " + mFormat); |
| 316 | mLogged = true; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | setText(text); |
| 321 | } |
| 322 | |
| 323 | private void updateRunning() { |
Simon Dubray | 814e1f5 | 2015-11-05 11:38:40 +0100 | [diff] [blame] | 324 | boolean running = mVisible && mStarted && isShown(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | if (running != mRunning) { |
| 326 | if (running) { |
| 327 | updateText(SystemClock.elapsedRealtime()); |
| 328 | dispatchChronometerTick(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 329 | postDelayed(mTickRunnable, 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } else { |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 331 | removeCallbacks(mTickRunnable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } |
| 333 | mRunning = running; |
| 334 | } |
| 335 | } |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 336 | |
| 337 | private final Runnable mTickRunnable = new Runnable() { |
| 338 | @Override |
| 339 | public void run() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | if (mRunning) { |
| 341 | updateText(SystemClock.elapsedRealtime()); |
| 342 | dispatchChronometerTick(); |
John Reck | d0374c6 | 2015-10-20 13:25:01 -0700 | [diff] [blame] | 343 | postDelayed(mTickRunnable, 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | void dispatchChronometerTick() { |
| 349 | if (mOnChronometerTickListener != null) { |
| 350 | mOnChronometerTickListener.onChronometerTick(this); |
| 351 | } |
| 352 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 353 | |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 354 | private static final int MIN_IN_SEC = 60; |
| 355 | private static final int HOUR_IN_SEC = MIN_IN_SEC*60; |
| 356 | private static String formatDuration(long ms) { |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 357 | int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS); |
| 358 | if (duration < 0) { |
| 359 | duration = -duration; |
| 360 | } |
| 361 | |
| 362 | int h = 0; |
| 363 | int m = 0; |
| 364 | |
| 365 | if (duration >= HOUR_IN_SEC) { |
| 366 | h = duration / HOUR_IN_SEC; |
| 367 | duration -= h * HOUR_IN_SEC; |
| 368 | } |
| 369 | if (duration >= MIN_IN_SEC) { |
| 370 | m = duration / MIN_IN_SEC; |
| 371 | duration -= m * MIN_IN_SEC; |
| 372 | } |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 373 | final int s = duration; |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 374 | |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 375 | final ArrayList<Measure> measures = new ArrayList<Measure>(); |
| 376 | if (h > 0) { |
| 377 | measures.add(new Measure(h, MeasureUnit.HOUR)); |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 378 | } |
Roozbeh Pournader | 241872a | 2016-12-15 15:17:49 -0800 | [diff] [blame] | 379 | if (m > 0) { |
| 380 | measures.add(new Measure(m, MeasureUnit.MINUTE)); |
| 381 | } |
| 382 | measures.add(new Measure(s, MeasureUnit.SECOND)); |
| 383 | |
| 384 | return MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE) |
Roozbeh Pournader | 63a13cf | 2016-12-19 15:56:40 -0800 | [diff] [blame] | 385 | .formatMeasures(measures.toArray(new Measure[measures.size()])); |
Dan Sandler | a79a747 | 2015-06-05 16:52:22 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public CharSequence getContentDescription() { |
| 390 | return formatDuration(mNow - mBase); |
| 391 | } |
| 392 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 393 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 394 | public CharSequence getAccessibilityClassName() { |
| 395 | return Chronometer.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 396 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | } |