From 0c3149191fbbfdc5242f8dd8a02f561f1ce604c3 Mon Sep 17 00:00:00 2001 From: Hidenari Koshimae Date: Tue, 18 Oct 2016 20:43:16 +0900 Subject: [PATCH] CountDownTimer: not skip onTick() CoundDownTimer internally performs counting down with a handler and fires onTick() callback at countdown intervals. At this internal counting down, firing onTick() is skipped if the account of time until finished is less than countdown interval. Due to this skip of onTick(), user will miss last onTick() callback. This change ensures CountDownTimer to fires every onTick() callback until the time is up. Bug: 32392931 Change-Id: I951f5cd46743873d7f8c353f8cf8c700999d8ae0 --- core/java/android/os/CountDownTimer.java | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/core/java/android/os/CountDownTimer.java b/core/java/android/os/CountDownTimer.java index 58acbcf5684ba..c7bf0fd6ba49a 100644 --- a/core/java/android/os/CountDownTimer.java +++ b/core/java/android/os/CountDownTimer.java @@ -125,19 +125,28 @@ public abstract class CountDownTimer { if (millisLeft <= 0) { onFinish(); - } else if (millisLeft < mCountdownInterval) { - // no tick, just delay until done - sendMessageDelayed(obtainMessage(MSG), millisLeft); } else { long lastTickStart = SystemClock.elapsedRealtime(); onTick(millisLeft); // take into account user's onTick taking time to execute - long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime(); + long lastTickDuration = SystemClock.elapsedRealtime() - lastTickStart; + long delay; - // special case: user's onTick took more than interval to - // complete, skip to next interval - while (delay < 0) delay += mCountdownInterval; + if (millisLeft < mCountdownInterval) { + // just delay until done + delay = millisLeft - lastTickDuration; + + // special case: user's onTick took more than interval to + // complete, trigger onFinish without delay + if (delay < 0) delay = 0; + } else { + delay = mCountdownInterval - lastTickDuration; + + // special case: user's onTick took more than interval to + // complete, skip to next interval + while (delay < 0) delay += mCountdownInterval; + } sendMessageDelayed(obtainMessage(MSG), delay); }