TextClockpublic class TextClock extends TextView TextClock can display the current date and/or time as
a formatted string.
This view honors the 24-hour format system setting. As such, it is
possible and recommended to provide two different formatting patterns:
one to display the date/time in 24-hour mode and one to display the
date/time in 12-hour mode. Most callers will want to use the defaults,
though, which will be appropriate for the user's locale.
It is possible to determine whether the system is currently in
24-hour mode by calling {@link #is24HourModeEnabled()}.
The rules used by this widget to decide how to format the date and
time are the following:
- In 24-hour mode:
- Use the value returned by {@link #getFormat24Hour()} when non-null
- Otherwise, use the value returned by {@link #getFormat12Hour()} when non-null
- Otherwise, use a default value appropriate for the user's locale, such as {@code h:mm a}
- In 12-hour mode:
- Use the value returned by {@link #getFormat12Hour()} when non-null
- Otherwise, use the value returned by {@link #getFormat24Hour()} when non-null
- Otherwise, use a default value appropriate for the user's locale, such as {@code HH:mm}
The {@link CharSequence} instances used as formatting patterns when calling either
{@link #setFormat24Hour(CharSequence)} or {@link #setFormat12Hour(CharSequence)} can
contain styling information. To do so, use a {@link android.text.Spanned} object.
Note that if you customize these strings, it is your responsibility to supply strings
appropriate for formatting dates and/or times in the user's locale. |
Fields Summary |
---|
public static final CharSequence | DEFAULT_FORMAT_12_HOURThe default formatting pattern in 12-hour mode. This pattern is used
if {@link #setFormat12Hour(CharSequence)} is called with a null pattern
or if no pattern was specified when creating an instance of this class.
This default pattern shows only the time, hours and minutes, and an am/pm
indicator. | public static final CharSequence | DEFAULT_FORMAT_24_HOURThe default formatting pattern in 24-hour mode. This pattern is used
if {@link #setFormat24Hour(CharSequence)} is called with a null pattern
or if no pattern was specified when creating an instance of this class.
This default pattern shows only the time, hours and minutes. | private CharSequence | mFormat12 | private CharSequence | mFormat24 | private CharSequence | mFormat | private boolean | mHasSeconds | private boolean | mAttached | private Calendar | mTime | private String | mTimeZone | private boolean | mShowCurrentUserTime | private final android.database.ContentObserver | mFormatChangeObserver | private final android.content.BroadcastReceiver | mIntentReceiver | private final Runnable | mTicker |
Constructors Summary |
---|
public TextClock(android.content.Context context)Creates a new clock using the default patterns for the current locale.
super(context);
init();
| public TextClock(android.content.Context context, android.util.AttributeSet attrs)Creates a new clock inflated from XML. This object's properties are
intialized from the attributes specified in XML.
This constructor uses a default style of 0, so the only attribute values
applied are those in the Context's Theme and the given AttributeSet.
this(context, attrs, 0);
| public TextClock(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr)Creates a new clock inflated from XML. This object's properties are
intialized from the attributes specified in XML.
this(context, attrs, defStyleAttr, 0);
| public TextClock(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.TextClock, defStyleAttr, defStyleRes);
try {
mFormat12 = a.getText(R.styleable.TextClock_format12Hour);
mFormat24 = a.getText(R.styleable.TextClock_format24Hour);
mTimeZone = a.getString(R.styleable.TextClock_timeZone);
} finally {
a.recycle();
}
init();
|
Methods Summary |
---|
private static java.lang.CharSequence | abc(java.lang.CharSequence a, java.lang.CharSequence b, java.lang.CharSequence c)Returns a if not null, else return b if not null, else return c.
return a == null ? (b == null ? c : b) : a;
| private void | chooseFormat()Selects either one of {@link #getFormat12Hour()} or {@link #getFormat24Hour()}
depending on whether the user has selected 24-hour format.
Calling this method does not schedule or unschedule the time ticker.
chooseFormat(true);
| private void | chooseFormat(boolean handleTicker)Selects either one of {@link #getFormat12Hour()} or {@link #getFormat24Hour()}
depending on whether the user has selected 24-hour format.
final boolean format24Requested = is24HourModeEnabled();
LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale);
if (format24Requested) {
mFormat = abc(mFormat24, mFormat12, ld.timeFormat24);
} else {
mFormat = abc(mFormat12, mFormat24, ld.timeFormat12);
}
boolean hadSeconds = mHasSeconds;
mHasSeconds = DateFormat.hasSeconds(mFormat);
if (handleTicker && mAttached && hadSeconds != mHasSeconds) {
if (hadSeconds) getHandler().removeCallbacks(mTicker);
else mTicker.run();
}
| private void | createTime(java.lang.String timeZone)
if (timeZone != null) {
mTime = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
} else {
mTime = Calendar.getInstance();
}
| public java.lang.CharSequence | getFormat()Returns the current format string. Always valid after constructor has
finished, and will never be {@code null}.
return mFormat;
| public java.lang.CharSequence | getFormat12Hour()Returns the formatting pattern used to display the date and/or time
in 12-hour mode. The formatting pattern syntax is described in
{@link DateFormat}.
return mFormat12;
| public java.lang.CharSequence | getFormat24Hour()Returns the formatting pattern used to display the date and/or time
in 24-hour mode. The formatting pattern syntax is described in
{@link DateFormat}.
return mFormat24;
| public java.lang.String | getTimeZone()Indicates which time zone is currently used by this view.
return mTimeZone;
| private void | init()
if (mFormat12 == null || mFormat24 == null) {
LocaleData ld = LocaleData.get(getContext().getResources().getConfiguration().locale);
if (mFormat12 == null) {
mFormat12 = ld.timeFormat12;
}
if (mFormat24 == null) {
mFormat24 = ld.timeFormat24;
}
}
createTime(mTimeZone);
// Wait until onAttachedToWindow() to handle the ticker
chooseFormat(false);
| public boolean | is24HourModeEnabled()Indicates whether the system is currently using the 24-hour mode.
When the system is in 24-hour mode, this view will use the pattern
returned by {@link #getFormat24Hour()}. In 12-hour mode, the pattern
returned by {@link #getFormat12Hour()} is used instead.
If either one of the formats is null, the other format is used. If
both formats are null, the default formats for the current locale are used.
if (mShowCurrentUserTime) {
return DateFormat.is24HourFormat(getContext(), ActivityManager.getCurrentUser());
} else {
return DateFormat.is24HourFormat(getContext());
}
| protected void | onAttachedToWindow()
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
registerReceiver();
registerObserver();
createTime(mTimeZone);
if (mHasSeconds) {
mTicker.run();
} else {
onTimeChanged();
}
}
| protected void | onDetachedFromWindow()
super.onDetachedFromWindow();
if (mAttached) {
unregisterReceiver();
unregisterObserver();
getHandler().removeCallbacks(mTicker);
mAttached = false;
}
| private void | onTimeChanged()
mTime.setTimeInMillis(System.currentTimeMillis());
setText(DateFormat.format(mFormat, mTime));
| private void | registerObserver()
final ContentResolver resolver = getContext().getContentResolver();
if (mShowCurrentUserTime) {
resolver.registerContentObserver(Settings.System.CONTENT_URI, true,
mFormatChangeObserver, UserHandle.USER_ALL);
} else {
resolver.registerContentObserver(Settings.System.CONTENT_URI, true,
mFormatChangeObserver);
}
| private void | registerReceiver()
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
| public void | setFormat12Hour(java.lang.CharSequence format)Specifies the formatting pattern used to display the date and/or time
in 12-hour mode. The formatting pattern syntax is described in
{@link DateFormat}.
If this pattern is set to null, {@link #getFormat24Hour()} will be used
even in 12-hour mode. If both 24-hour and 12-hour formatting patterns
are set to null, the default pattern for the current locale will be used
instead.
Note: if styling is not needed, it is highly recommended
you supply a format string generated by
{@link DateFormat#getBestDateTimePattern(java.util.Locale, String)}. This method
takes care of generating a format string adapted to the desired locale.
mFormat12 = format;
chooseFormat();
onTimeChanged();
| public void | setFormat24Hour(java.lang.CharSequence format)Specifies the formatting pattern used to display the date and/or time
in 24-hour mode. The formatting pattern syntax is described in
{@link DateFormat}.
If this pattern is set to null, {@link #getFormat24Hour()} will be used
even in 12-hour mode. If both 24-hour and 12-hour formatting patterns
are set to null, the default pattern for the current locale will be used
instead.
Note: if styling is not needed, it is highly recommended
you supply a format string generated by
{@link DateFormat#getBestDateTimePattern(java.util.Locale, String)}. This method
takes care of generating a format string adapted to the desired locale.
mFormat24 = format;
chooseFormat();
onTimeChanged();
| public void | setShowCurrentUserTime(boolean showCurrentUserTime)Sets whether this clock should always track the current user and not the user of the
current process. This is used for single instance processes like the systemUI who need
to display time for different users.
mShowCurrentUserTime = showCurrentUserTime;
chooseFormat();
onTimeChanged();
unregisterObserver();
registerObserver();
| public void | setTimeZone(java.lang.String timeZone)Sets the specified time zone to use in this clock. When the time zone
is set through this method, system time zone changes (when the user
sets the time zone in settings for instance) will be ignored.
mTimeZone = timeZone;
createTime(timeZone);
onTimeChanged();
| private void | unregisterObserver()
final ContentResolver resolver = getContext().getContentResolver();
resolver.unregisterContentObserver(mFormatChangeObserver);
| private void | unregisterReceiver()
getContext().unregisterReceiver(mIntentReceiver);
|
|