Replace CountdownDelegate with ChronometerDelegate. Create BaseChronometer as superclass of CountdownChronometer and ChronometerWithMillis.
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package com.philliphsu.clock2.timers;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Formatter;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 9/7/2016.
|
||||
*
|
||||
* A helper class for BaseChronometer that handles formatting the text.
|
||||
* Can also be used independent of Chronometer to format elapsed times and return the result
|
||||
* as a CharSequence.
|
||||
*/
|
||||
public final class ChronometerDelegate {
|
||||
private static final String TAG = "ChronometerDelegate";
|
||||
|
||||
private static final RelativeSizeSpan SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||
|
||||
private long mBase;
|
||||
private long mNow; // the currently displayed time
|
||||
private boolean mLogged;
|
||||
private String mFormat;
|
||||
private Formatter mFormatter;
|
||||
private Locale mFormatterLocale;
|
||||
private Object[] mFormatterArgs = new Object[1];
|
||||
private StringBuilder mFormatBuilder;
|
||||
private StringBuilder mRecycle = new StringBuilder(8);
|
||||
private boolean mCountDown;
|
||||
private boolean mShowCentiseconds;
|
||||
private boolean mApplySizeSpanOnCentiseconds;
|
||||
|
||||
public void init() {
|
||||
mBase = SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
public void setCountDown(boolean countDown) {
|
||||
mCountDown = countDown;
|
||||
}
|
||||
|
||||
public boolean isCountDown() {
|
||||
return mCountDown;
|
||||
}
|
||||
|
||||
public void setShowCentiseconds(boolean showCentiseconds, boolean applySizeSpan) {
|
||||
mShowCentiseconds = showCentiseconds;
|
||||
mApplySizeSpanOnCentiseconds = applySizeSpan;
|
||||
}
|
||||
|
||||
public boolean showsCentiseconds() {
|
||||
return mShowCentiseconds;
|
||||
}
|
||||
|
||||
public void setBase(long base) {
|
||||
mBase = base;
|
||||
}
|
||||
|
||||
public long getBase() {
|
||||
return mBase;
|
||||
}
|
||||
|
||||
public void setFormat(String format) {
|
||||
mFormat = format;
|
||||
if (format != null && mFormatBuilder == null) {
|
||||
mFormatBuilder = new StringBuilder(format.length() * 2);
|
||||
}
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return mFormat;
|
||||
}
|
||||
|
||||
public CharSequence formatElapsedTime(long now) {
|
||||
mNow = now;
|
||||
long millis = mCountDown ? mBase - now : now - mBase;
|
||||
String text = DateUtils.formatElapsedTime(mRecycle, millis / 1000);
|
||||
|
||||
Locale loc = Locale.getDefault();
|
||||
if (mFormat != null) {
|
||||
if (mFormatter == null || !loc.equals(mFormatterLocale)) {
|
||||
mFormatterLocale = loc;
|
||||
mFormatter = new Formatter(mFormatBuilder, loc);
|
||||
}
|
||||
mFormatBuilder.setLength(0);
|
||||
mFormatterArgs[0] = text;
|
||||
try {
|
||||
mFormatter.format(mFormat, mFormatterArgs);
|
||||
text = mFormatBuilder.toString();
|
||||
} catch (IllegalFormatException ex) {
|
||||
if (!mLogged) {
|
||||
Log.w(TAG, "Illegal format string: " + mFormat);
|
||||
mLogged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (mShowCentiseconds) {
|
||||
long centiseconds = (millis % 1000) / 10;
|
||||
String centisecondsText = String.format(loc,
|
||||
// TODO: Different locales use different decimal marks.
|
||||
// The two most common are . and ,
|
||||
// Consider removing the . and just let the size span
|
||||
// represent this as fractional seconds?
|
||||
// ...or figure out how to get the correct mark for the
|
||||
// current locale.
|
||||
// It looks like Google's Clock app strictly uses .
|
||||
".%02d", // The . before % is not a format specifier
|
||||
centiseconds);
|
||||
if (mApplySizeSpanOnCentiseconds) {
|
||||
SpannableString span = new SpannableString(centisecondsText);
|
||||
span.setSpan(SIZE_SPAN, 0, centisecondsText.length(),
|
||||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
return TextUtils.concat(text, span);
|
||||
} else {
|
||||
return text.concat(centisecondsText);
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,9 @@ package com.philliphsu.clock2.timers;
|
||||
|
||||
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;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.philliphsu.clock2.BaseChronometer;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 7/25/2016.
|
||||
@@ -32,285 +29,29 @@ import android.widget.TextView;
|
||||
* towards the base time. The ability to count down was added to Chronometer
|
||||
* in API 24.
|
||||
*/
|
||||
public class CountdownChronometer extends TextView {
|
||||
public class CountdownChronometer extends BaseChronometer {
|
||||
private static final String TAG = "CountdownChronometer";
|
||||
|
||||
/**
|
||||
* A callback that notifies when the chronometer has incremented on its own.
|
||||
*/
|
||||
public interface OnChronometerTickListener {
|
||||
|
||||
/**
|
||||
* Notification that the chronometer has changed.
|
||||
*/
|
||||
void onChronometerTick(CountdownChronometer chronometer);
|
||||
|
||||
}
|
||||
|
||||
private boolean mVisible;
|
||||
private boolean mStarted;
|
||||
private boolean mRunning;
|
||||
private final CountdownDelegate mDelegate = new CountdownDelegate();
|
||||
private OnChronometerTickListener mOnChronometerTickListener;
|
||||
|
||||
private static final int TICK_WHAT = 2;
|
||||
|
||||
/**
|
||||
* Initialize this Chronometer object.
|
||||
* Sets the base to the current time.
|
||||
*/
|
||||
public CountdownChronometer(Context context) {
|
||||
this(context, null, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize with standard view layout information.
|
||||
* Sets the base to the current time.
|
||||
*/
|
||||
public CountdownChronometer(Context context, AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize with standard view layout information and style.
|
||||
* Sets the base to the current time.
|
||||
*/
|
||||
public CountdownChronometer(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
|
||||
// final TypedArray a = context.obtainStyledAttributes(
|
||||
// attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, 0);
|
||||
// setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
|
||||
// a.recycle();
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
@TargetApi(21)
|
||||
public CountdownChronometer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
|
||||
// final TypedArray a = context.obtainStyledAttributes(
|
||||
// attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
|
||||
// setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
|
||||
// a.recycle();
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
mDelegate.init();
|
||||
updateText(SystemClock.elapsedRealtime());
|
||||
setCountDown(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time that the count-up timer is in reference to.
|
||||
*
|
||||
* @param base Use the {@link SystemClock#elapsedRealtime} time base.
|
||||
*/
|
||||
// @android.view.RemotableViewMethod
|
||||
public void setBase(long base) {
|
||||
mDelegate.setBase(base);
|
||||
dispatchChronometerTick();
|
||||
updateText(SystemClock.elapsedRealtime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the base time as set through {@link #setBase}.
|
||||
*/
|
||||
public long getBase() {
|
||||
return mDelegate.getBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to {@link #setBase(long) setBase(SystemClock.elapsedRealtime() + duration)}.
|
||||
*/
|
||||
public void setDuration(long duration) {
|
||||
setBase(SystemClock.elapsedRealtime() + duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the format string used for display. The Chronometer will display
|
||||
* this string, with the first "%s" replaced by the current timer value in
|
||||
* "MM:SS" or "H:MM:SS" form.
|
||||
*
|
||||
* If the format string is null, or if you never call setFormat(), the
|
||||
* Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS"
|
||||
* form.
|
||||
*
|
||||
* @param format the format string.
|
||||
*/
|
||||
// @android.view.RemotableViewMethod
|
||||
public void setFormat(String format) {
|
||||
mDelegate.setFormat(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current format string as set through {@link #setFormat}.
|
||||
*/
|
||||
public String getFormat() {
|
||||
return mDelegate.getFormat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the listener to be called when the chronometer changes.
|
||||
*
|
||||
* @param listener The listener.
|
||||
*/
|
||||
public void setOnChronometerTickListener(OnChronometerTickListener listener) {
|
||||
mOnChronometerTickListener = listener;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The listener (may be null) that is listening for chronometer change
|
||||
* events.
|
||||
*/
|
||||
public OnChronometerTickListener getOnChronometerTickListener() {
|
||||
return mOnChronometerTickListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start counting up. This does not affect the base as set from {@link #setBase}, just
|
||||
* the view display.
|
||||
*
|
||||
* Chronometer works by regularly scheduling messages to the handler, even when the
|
||||
* Widget is not visible. To make sure resource leaks do not occur, the user should
|
||||
* make sure that each start() call has a reciprocal call to {@link #stop}.
|
||||
*/
|
||||
public void start() {
|
||||
mStarted = true;
|
||||
updateRunning();
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop counting up. This does not affect the base as set from {@link #setBase}, just
|
||||
* the view display.
|
||||
*
|
||||
* This stops the messages to the handler, effectively releasing resources that would
|
||||
* be held as the chronometer is running, via {@link #start}.
|
||||
*/
|
||||
public void stop() {
|
||||
mStarted = false;
|
||||
updateRunning();
|
||||
}
|
||||
|
||||
/**
|
||||
* The same as calling {@link #start} or {@link #stop}.
|
||||
* @hide pending API council approval
|
||||
*/
|
||||
// @android.view.RemotableViewMethod
|
||||
public void setStarted(boolean started) {
|
||||
mStarted = started;
|
||||
updateRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
mVisible = false;
|
||||
updateRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
Log.d(TAG, "onWindowVisibilityChanged()");
|
||||
super.onWindowVisibilityChanged(visibility);
|
||||
mVisible = visibility == VISIBLE;
|
||||
updateRunning();
|
||||
}
|
||||
|
||||
private synchronized void updateText(long now) {
|
||||
setText(mDelegate.formatElapsedTime(now));
|
||||
}
|
||||
|
||||
private void updateRunning() {
|
||||
boolean running = mVisible && mStarted;
|
||||
if (running != mRunning) {
|
||||
if (running) {
|
||||
Log.d(TAG, "Running");
|
||||
updateText(SystemClock.elapsedRealtime());
|
||||
dispatchChronometerTick();
|
||||
mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
|
||||
} else {
|
||||
Log.d(TAG, "Not running anymore");
|
||||
mHandler.removeMessages(TICK_WHAT);
|
||||
}
|
||||
mRunning = running;
|
||||
}
|
||||
}
|
||||
|
||||
private Handler mHandler = new Handler() {
|
||||
public void handleMessage(Message m) {
|
||||
if (mRunning) {
|
||||
updateText(SystemClock.elapsedRealtime());
|
||||
dispatchChronometerTick();
|
||||
sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void dispatchChronometerTick() {
|
||||
if (mOnChronometerTickListener != null) {
|
||||
mOnChronometerTickListener.onChronometerTick(this);
|
||||
}
|
||||
}
|
||||
|
||||
private static final int MIN_IN_SEC = 60;
|
||||
private static final int HOUR_IN_SEC = MIN_IN_SEC*60;
|
||||
// private static String formatDuration(long ms) {
|
||||
// final Resources res = Resources.getSystem();
|
||||
// final StringBuilder text = new StringBuilder();
|
||||
//
|
||||
// int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS);
|
||||
// if (duration < 0) {
|
||||
// duration = -duration;
|
||||
// }
|
||||
//
|
||||
// int h = 0;
|
||||
// int m = 0;
|
||||
//
|
||||
// if (duration >= HOUR_IN_SEC) {
|
||||
// h = duration / HOUR_IN_SEC;
|
||||
// duration -= h * HOUR_IN_SEC;
|
||||
// }
|
||||
// if (duration >= MIN_IN_SEC) {
|
||||
// m = duration / MIN_IN_SEC;
|
||||
// duration -= m * MIN_IN_SEC;
|
||||
// }
|
||||
// int s = duration;
|
||||
//
|
||||
// try {
|
||||
// if (h > 0) {
|
||||
// text.append(res.getQuantityString(
|
||||
// com.android.internal.R.plurals.duration_hours, h, h));
|
||||
// }
|
||||
// if (m > 0) {
|
||||
// if (text.length() > 0) {
|
||||
// text.append(' ');
|
||||
// }
|
||||
// text.append(res.getQuantityString(
|
||||
// com.android.internal.R.plurals.duration_minutes, m, m));
|
||||
// }
|
||||
//
|
||||
// if (text.length() > 0) {
|
||||
// text.append(' ');
|
||||
// }
|
||||
// text.append(res.getQuantityString(
|
||||
// com.android.internal.R.plurals.duration_seconds, s, s));
|
||||
// } catch (Resources.NotFoundException e) {
|
||||
// // Ignore; plurals throws an exception for an untranslated quantity for a given locale.
|
||||
// return null;
|
||||
// }
|
||||
// return text.toString();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public CharSequence getContentDescription() {
|
||||
// return formatDuration(mNow - mBase);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public CharSequence getAccessibilityClassName() {
|
||||
// return CountdownChronometer.class.getName();
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.philliphsu.clock2.timers;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Formatter;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 9/7/2016.
|
||||
*
|
||||
* A helper class for CountdownChronometer that handles formatting the countdown text.
|
||||
* TODO: A similar delegate class can also be made for ChronometerWithMillis. However, try to
|
||||
* use a common base class between this and ChronometerWithMillis.
|
||||
*/
|
||||
final class CountdownDelegate {
|
||||
private static final String TAG = "CountdownDelegate";
|
||||
|
||||
private long mBase;
|
||||
private long mNow; // the currently displayed time
|
||||
private boolean mLogged;
|
||||
private String mFormat;
|
||||
private Formatter mFormatter;
|
||||
private Locale mFormatterLocale;
|
||||
private Object[] mFormatterArgs = new Object[1];
|
||||
private StringBuilder mFormatBuilder;
|
||||
private StringBuilder mRecycle = new StringBuilder(8);
|
||||
|
||||
void init() {
|
||||
mBase = SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
void setBase(long base) {
|
||||
mBase = base;
|
||||
}
|
||||
|
||||
long getBase() {
|
||||
return mBase;
|
||||
}
|
||||
|
||||
void setFormat(String format) {
|
||||
mFormat = format;
|
||||
if (format != null && mFormatBuilder == null) {
|
||||
mFormatBuilder = new StringBuilder(format.length() * 2);
|
||||
}
|
||||
}
|
||||
|
||||
String getFormat() {
|
||||
return mFormat;
|
||||
}
|
||||
|
||||
String formatElapsedTime(long now) {
|
||||
mNow = now;
|
||||
long seconds = mBase - now;
|
||||
seconds /= 1000;
|
||||
String text = DateUtils.formatElapsedTime(mRecycle, seconds);
|
||||
|
||||
if (mFormat != null) {
|
||||
Locale loc = Locale.getDefault();
|
||||
if (mFormatter == null || !loc.equals(mFormatterLocale)) {
|
||||
mFormatterLocale = loc;
|
||||
mFormatter = new Formatter(mFormatBuilder, loc);
|
||||
}
|
||||
mFormatBuilder.setLength(0);
|
||||
mFormatterArgs[0] = text;
|
||||
try {
|
||||
mFormatter.format(mFormat, mFormatterArgs);
|
||||
text = mFormatBuilder.toString();
|
||||
} catch (IllegalFormatException ex) {
|
||||
if (!mLogged) {
|
||||
Log.w(TAG, "Illegal format string: " + mFormat);
|
||||
mLogged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class TimerNotificationService extends Service {
|
||||
private TimerController mController;
|
||||
private NotificationCompat.Builder mNoteBuilder;
|
||||
private NotificationManager mNotificationManager;
|
||||
private final CountdownDelegate mCountdownDelegate = new CountdownDelegate();
|
||||
private final ChronometerDelegate mCountdownDelegate = new ChronometerDelegate();
|
||||
private MyHandlerThread mThread; // TODO: I think we may need a list of threads.
|
||||
|
||||
/**
|
||||
@@ -210,7 +210,7 @@ public class TimerNotificationService extends Service {
|
||||
}
|
||||
|
||||
private void updateNotification() {
|
||||
String text = mCountdownDelegate.formatElapsedTime(SystemClock.elapsedRealtime());
|
||||
CharSequence text = mCountdownDelegate.formatElapsedTime(SystemClock.elapsedRealtime());
|
||||
mNoteBuilder.setContentText(text);
|
||||
mNotificationManager.notify(TAG, mTimer.getIntId(), mNoteBuilder.build());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user