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