Update BaseChronometer and ChronometerDelegate with Chronometer source code in API 24

This commit is contained in:
Phillip Hsu 2016-09-10 02:17:21 -07:00
parent cd18b33fcd
commit bf381a05f1
4 changed files with 46 additions and 18 deletions

View File

@ -2,8 +2,6 @@ package com.philliphsu.clock2;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
@ -34,11 +32,10 @@ public class BaseChronometer extends TextView {
private boolean mVisible;
private boolean mStarted;
private boolean mRunning;
private long mTickInterval;
private OnChronometerTickListener mOnChronometerTickListener;
private final ChronometerDelegate mDelegate = new ChronometerDelegate();
private static final int TICK_WHAT = 2;
/**
* Initialize this Chronometer object.
* Sets the base to the current time.
@ -85,6 +82,7 @@ public class BaseChronometer extends TextView {
private void init() {
mDelegate.init();
updateText(SystemClock.elapsedRealtime());
mTickInterval = 1000;
}
/**
@ -96,6 +94,7 @@ public class BaseChronometer extends TextView {
*/
public void setCountDown(boolean countDown) {
mDelegate.setCountDown(countDown);
updateText(SystemClock.elapsedRealtime());
}
/**
@ -119,7 +118,18 @@ public class BaseChronometer extends TextView {
*/
public void setShowCentiseconds(boolean showCentiseconds, boolean applySizeSpan) {
mDelegate.setShowCentiseconds(showCentiseconds, applySizeSpan);
init(); // Clear and update the text again
// Clear and update the text again. The reason we don't just update the text, as in
// setCountDown(), is that if showCentiseconds is true, we will be increasing the
// granularity of this time display; the time delta between the first
// setting of the base time (when this view is first instantiated), and the next call
// to updateText() is, while minuscule, significant enough that there would be some nonzero
// centiseconds value initially displayed. By resetting the timer, we minimize
// this time delta, and that should display an initial centiseconds value of zero.
init();
// ----------------------------------------------------------------------------
// TOneverDO: Precede init(), because init() resets mTickInterval to 1000.
mTickInterval = showCentiseconds ? 10 : 1000;
// ----------------------------------------------------------------------------
}
/**
@ -268,31 +278,34 @@ public class BaseChronometer extends TextView {
}
private synchronized void updateText(long now) {
setText(mDelegate.formatElapsedTime(now));
setText(mDelegate.formatElapsedTime(now, getResources()));
}
private void updateRunning() {
boolean running = mVisible && mStarted;
// The isShown() check is new to the Chronometer source in API 24.
// It is preventing the chronometer in TimerViewHolder from ticking, so leave it off.
boolean running = mVisible && mStarted /*&& isShown()*/;
if (running != mRunning) {
if (running) {
Log.d(TAG, "Running");
updateText(SystemClock.elapsedRealtime());
dispatchChronometerTick();
mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 10);
postDelayed(mTickRunnable, mTickInterval);
} else {
Log.d(TAG, "Not running anymore");
mHandler.removeMessages(TICK_WHAT);
removeCallbacks(mTickRunnable);
}
mRunning = running;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message m) {
private final Runnable mTickRunnable = new Runnable() {
@Override
public void run() {
if (mRunning) {
updateText(SystemClock.elapsedRealtime());
dispatchChronometerTick();
sendMessageDelayed(Message.obtain(this, TICK_WHAT), 10);
postDelayed(mTickRunnable, mTickInterval);
}
}
};

View File

@ -1,5 +1,6 @@
package com.philliphsu.clock2.timers;
import android.content.res.Resources;
import android.os.SystemClock;
import android.text.SpannableString;
import android.text.Spanned;
@ -8,6 +9,8 @@ import android.text.format.DateUtils;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import com.philliphsu.clock2.R;
import java.util.Formatter;
import java.util.IllegalFormatException;
import java.util.Locale;
@ -77,10 +80,18 @@ public final class ChronometerDelegate {
return mFormat;
}
public CharSequence formatElapsedTime(long now) {
public CharSequence formatElapsedTime(long now, Resources resources) {
mNow = now;
long millis = mCountDown ? mBase - now : now - mBase;
String text = DateUtils.formatElapsedTime(mRecycle, millis / 1000);
long seconds = mCountDown ? mBase - now : now - mBase;
boolean negative = false;
if (seconds < 0) {
seconds = -seconds;
negative = true;
}
String text = DateUtils.formatElapsedTime(mRecycle, seconds / 1000);
if (negative) {
text = resources.getString(R.string.negative_duration, text);
}
Locale loc = Locale.getDefault();
if (mFormat != null) {
@ -101,7 +112,7 @@ public final class ChronometerDelegate {
}
}
if (mShowCentiseconds) {
long centiseconds = (millis % 1000) / 10;
long centiseconds = (seconds % 1000) / 10;
String centisecondsText = String.format(loc,
// TODO: Different locales use different decimal marks.
// The two most common are . and ,

View File

@ -211,7 +211,7 @@ public class TimerNotificationService extends Service {
}
private void updateNotification() {
CharSequence text = mCountdownDelegate.formatElapsedTime(SystemClock.elapsedRealtime());
CharSequence text = mCountdownDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), getResources());
mNoteBuilder.setContentText(text);
mNotificationManager.notify(TAG, mTimer.getIntId(), mNoteBuilder.build());
}

View File

@ -1,4 +1,4 @@
<resources>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">Clock+2</string>
<string name="action_settings">Settings</string>
<string name="section_format">Hello World from section: %1$d</string>
@ -206,4 +206,8 @@
<string name="stopwatch">Stopwatch</string>
<string name="lap">Lap</string>
<!-- https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/strings.xml -->
<!-- The representation of a time duration when negative. An example is -1:14. This can be used with a countdown timer for example.-->
<string name="negative_duration">\u2212<xliff:g id="time" example="1:14">%1$s</xliff:g></string>
</resources>