下面列出了android.text.format.Time#normalize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Announce the currently-selected time when launched.
*/
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
/**
* Announce the currently-selected time when launched.
*/
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
/**
* Announce the currently-selected time when launched.
*/
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
/**
* Announce the currently-selected time when launched.
*/
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
@Override
protected void onHandleIntent(Intent intent) {
mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
// Query calendar events in the next 24 hours.
Time time = new Time();
time.setToNow();
long beginTime = time.toMillis(true);
time.monthDay++;
time.normalize(true);
long endTime = time.normalize(true);
List<Event> events = queryEvents(this, beginTime, endTime);
for (Event event : events) {
final PutDataMapRequest putDataMapRequest = event.toPutDataMapRequest();
if (mGoogleApiClient.isConnected()) {
Wearable.DataApi.putDataItem(
mGoogleApiClient, putDataMapRequest.asPutDataRequest()).await();
} else {
Log.e(TAG, "Failed to send data item: " + putDataMapRequest
+ " - Client disconnected from Google Play Services");
}
}
mGoogleApiClient.disconnect();
}
/**
* Announce the currently-selected time when launched.
*/
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
/**
* Announce the currently-selected time when launched.
*/
@Override public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// Clear the event's current text so that only the current time will be spoken.
event.getText().clear();
Time time = new Time();
time.hour = getHours();
time.minute = getMinutes();
long millis = time.normalize(true);
int flags = DateUtils.FORMAT_SHOW_TIME;
if (mIs24HourMode) {
flags |= DateUtils.FORMAT_24HOUR;
}
String timeString = DateUtils.formatDateTime(getContext(), millis, flags);
event.getText().add(timeString);
return true;
}
return super.dispatchPopulateAccessibilityEvent(event);
}
@Override
public Time getCustomDefault(ContentSet currentValues, Time genericDefault)
{
Time reference = mReferenceAdapter != null ? mReferenceAdapter.get(currentValues) : null;
boolean useReference = reference != null && !genericDefault.before(reference);
Time value = new Time(useReference ? reference : genericDefault);
if (value.allDay)
{
value.set(value.monthDay - (useReference ? 1 : 0), value.month, value.year);
}
else
{
value.minute--;
value.normalize(false);
value.second = 0;
value.minute = 0;
}
return value;
}
@Override
public Time getCustomDefault(ContentSet currentValues, Time genericDefault)
{
Time reference = mReferenceAdapter != null ? mReferenceAdapter.get(currentValues) : null;
boolean useReference = reference != null && !genericDefault.after(reference);
Time value = new Time(useReference ? reference : genericDefault);
if (value.allDay)
{
value.monthDay++;
}
else
{
value.second = 0;
value.minute = 0;
value.hour++;
}
value.normalize(false);
return value;
}
@Override
public void onClick(View v)
{
int id = v.getId();
Time time = mAdapter.get(mValues);
if (id == R.id.button_add_one_day)
{
time.monthDay++;
time.normalize(true);
}
else if (id == R.id.button_add_one_hour)
{
time.hour++;
time.normalize(false);
}
mAdapter.validateAndSet(mValues, time);
}
void update() {
if (mTime == null || getVisibility() == GONE) {
return;
}
if (mShowRelativeTime) {
updateRelativeTime();
return;
}
int display;
Date time = mTime;
Time t = new Time();
t.set(mTimeMillis);
t.second = 0;
t.hour -= 12;
long twelveHoursBefore = t.toMillis(false);
t.hour += 12;
long twelveHoursAfter = t.toMillis(false);
t.hour = 0;
t.minute = 0;
long midnightBefore = t.toMillis(false);
t.monthDay++;
long midnightAfter = t.toMillis(false);
long nowMillis = System.currentTimeMillis();
t.set(nowMillis);
t.second = 0;
nowMillis = t.normalize(false);
// Choose the display mode
choose_display: {
if ((nowMillis >= midnightBefore && nowMillis < midnightAfter)
|| (nowMillis >= twelveHoursBefore && nowMillis < twelveHoursAfter)) {
display = SHOW_TIME;
break choose_display;
}
// Else, show month day and year.
display = SHOW_MONTH_DAY_YEAR;
break choose_display;
}
// Choose the format
DateFormat format;
if (display == mLastDisplay && mLastFormat != null) {
// use cached format
format = mLastFormat;
} else {
switch (display) {
case SHOW_TIME:
format = getTimeFormat();
break;
case SHOW_MONTH_DAY_YEAR:
format = DateFormat.getDateInstance(DateFormat.SHORT);
break;
default:
throw new RuntimeException("unknown display value: " + display);
}
mLastFormat = format;
}
// Set the text
String text = format.format(mTime);
setText(text);
// Schedule the next update
if (display == SHOW_TIME) {
// Currently showing the time, update at the later of twelve hours after or midnight.
mUpdateTimeMillis = twelveHoursAfter > midnightAfter ? twelveHoursAfter : midnightAfter;
} else {
// Currently showing the date
if (mTimeMillis < nowMillis) {
// If the time is in the past, don't schedule an update
mUpdateTimeMillis = 0;
} else {
// If hte time is in the future, schedule one at the earlier of twelve hours
// before or midnight before.
mUpdateTimeMillis = twelveHoursBefore < midnightBefore
? twelveHoursBefore : midnightBefore;
}
}
}
private void configureConferenceDate() {
conferenceTime.set(second, minute, hour, monthDay, month, year);
conferenceTime.normalize(true);
long confMillis = conferenceTime.toMillis(true);
Time nowTime = new Time(Time.getCurrentTimezone());
nowTime.setToNow();
nowTime.normalize(true);
long nowMillis = nowTime.toMillis(true);
long milliDiff = confMillis - nowMillis;
new CountDownTimer(milliDiff, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// decompose difference into days, hours, minutes and seconds
CountdownTimerActivity.this.mDisplayDays = (int) ((millisUntilFinished / 1000) / 86400);
CountdownTimerActivity.this.mDisplayHours = (int) (((millisUntilFinished / 1000) - (CountdownTimerActivity.this.mDisplayDays * 86400)) / 3600);
CountdownTimerActivity.this.mDisplayMinutes = (int) (((millisUntilFinished / 1000) - ((CountdownTimerActivity.this.mDisplayDays * 86400) + (CountdownTimerActivity.this.mDisplayHours * 3600))) / 60);
CountdownTimerActivity.this.mDisplaySeconds = (int) ((millisUntilFinished / 1000) % 60);
CountdownTimerActivity.this.mDaysWheel.setText(String.valueOf(CountdownTimerActivity.this.mDisplayDays));
CountdownTimerActivity.this.mDaysWheel.setProgress(CountdownTimerActivity.this.mDisplayDays);
CountdownTimerActivity.this.mHoursWheel.setText(String.valueOf(CountdownTimerActivity.this.mDisplayHours));
CountdownTimerActivity.this.mHoursWheel.setProgress(CountdownTimerActivity.this.mDisplayHours * 15);
CountdownTimerActivity.this.mMinutesWheel.setText(String.valueOf(CountdownTimerActivity.this.mDisplayMinutes));
CountdownTimerActivity.this.mMinutesWheel.setProgress(CountdownTimerActivity.this.mDisplayMinutes * 6);
Animation an = new RotateAnimation(0.0f, 90.0f, 250f, 273f);
an.setFillAfter(true);
CountdownTimerActivity.this.mSecondsWheel.setText(String.valueOf(CountdownTimerActivity.this.mDisplaySeconds));
CountdownTimerActivity.this.mSecondsWheel.setProgress(CountdownTimerActivity.this.mDisplaySeconds * 6);
}
@Override
public void onFinish() {
//TODO: this is where you would launch a subsequent activity if you'd like. I'm currently just setting the seconds to zero
Logger.d(TAG, "Timer Finished...");
CountdownTimerActivity.this.mSecondsWheel.setText("0");
CountdownTimerActivity.this.mSecondsWheel.setProgress(0);
}
}.start();
}
/**
* Format the given due date. The result depends on the current date and on the all-day flag of the due date.
*
* @param date
* The due date to format.
* @return A string with the formatted due date.
*/
public String format(Time date, Time now, DateFormatContext dateContext)
{
// normalize time to ensure yearDay is set properly
date.normalize(false);
if (dateContext.useRelative(now, date))
{
long delta = Math.abs(now.toMillis(false) - date.toMillis(false));
if (date.allDay)
{
Time allDayNow = new Time("UTC");
allDayNow.set(now.monthDay, now.month, now.year);
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), allDayNow.toMillis(false), DAY_IN_MILLIS).toString();
}
else if (delta < 60 * 1000)
{
// the time is within this minute, show "now"
return mContext.getString(R.string.now);
}
else if (delta < 60 * 60 * 1000)
{
// time is within this hour, show number of minutes left
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), now.toMillis(false), DateUtils.MINUTE_IN_MILLIS).toString();
}
else if (delta < 24 * 60 * 60 * 1000)
{
// time is within 24 hours, show relative string with time
// FIXME: instead of using a fixed 24 hour interval this should be aligned to midnight tomorrow and yesterday
return routingGetRelativeDateTimeString(mContext, date.toMillis(false), DAY_IN_MILLIS, WEEK_IN_MILLIS,
dateContext.getDateUtilsFlags(now, date)).toString();
}
else
{
return DateUtils.getRelativeTimeSpanString(date.toMillis(false), now.toMillis(false), DAY_IN_MILLIS).toString();
}
}
return date.allDay ? formatAllDay(date, now, dateContext) : formatNonAllDay(date, now, dateContext);
}
@Override
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
Time time = new Time(mTimezone.getID());
time.set(mTime.monthDay + 1, mTime.month, mTime.year);
// today row (including overdue)
long t2 = time.toMillis(false);
result.addRow(makeRow(1, TYPE_END_OF_TODAY, MIN_TIME, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(2, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_IN_7_DAYS, t3, t4));
time.monthDay += 1;
time.normalize(true);
// open future for future tasks (including tasks without dates)
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(4, TYPE_NO_END, t4, null));
}
return result;
}
@Override
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
// get time of today 00:00:00
Time time = new Time(mTime.timezone);
time.set(mTime.monthDay, mTime.month, mTime.year);
// already started row
long t1 = time.toMillis(false);
result.addRow(makeRow(1, TYPE_OVERDUE, MIN_TIME, t1));
time.hour = 0;
time.minute = 0;
time.second = 0;
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// today row
long t2 = time.toMillis(false);
result.addRow(makeRow(2, TYPE_END_OF_TODAY, t1, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(4, TYPE_END_IN_7_DAYS, t3, t4));
time.monthDay += 1;
time.normalize(true);
// open past future for future tasks (including tasks without dates)
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(5, TYPE_NO_END, t4, MAX_TIME));
}
return result;
}
public Cursor getCursor()
{
mTime.setToNow();
MatrixCursor result = new MatrixCursor(mProjection);
// get time of today 00:00:00
Time time = new Time(mTimezone.getID());
time.set(mTime.monthDay, mTime.month, mTime.year);
// null row, for tasks without due date
if (mProjectionList.contains(RANGE_NULL_ROW))
{
result.addRow(makeRow(1, 0, null, null));
}
long t1 = time.toMillis(false);
// open past row for overdue tasks
if (mProjectionList.contains(RANGE_OPEN_PAST))
{
result.addRow(makeRow(2, TYPE_END_OF_YESTERDAY, MIN_TIME, t1));
}
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// today row
long t2 = time.toMillis(false);
result.addRow(makeRow(3, TYPE_END_OF_TODAY, t1, t2));
time.monthDay += 1;
time.yearDay += 1;
time.normalize(true);
// tomorrow row
long t3 = time.toMillis(false);
result.addRow(makeRow(4, TYPE_END_OF_TOMORROW, t2, t3));
time.monthDay += 5;
time.yearDay += 5;
time.normalize(true);
// next week row
long t4 = time.toMillis(false);
result.addRow(makeRow(5, TYPE_END_IN_7_DAYS, t3, t4));
time.set(1, time.month + 1, time.year);
time.normalize(true);
// month row
long t5 = time.toMillis(false);
result.addRow(makeRow(6, TYPE_END_OF_A_MONTH, t4, t5));
time.set(1, 0, time.year + 1);
// rest of year row
long t6 = time.toMillis(false);
result.addRow(makeRow(7, TYPE_END_OF_A_YEAR, t5, t6));
// open future for future tasks
if (mProjectionList.contains(RANGE_OPEN_FUTURE))
{
result.addRow(makeRow(8, TYPE_NO_END, t6, MAX_TIME));
}
return result;
}
protected void setDueDate(TextView view, ImageView dueIcon, Time dueDate, boolean isClosed)
{
if (view != null && dueDate != null)
{
Time now = mNow;
if (now == null)
{
now = mNow = new Time();
}
if (!now.timezone.equals(TimeZone.getDefault().getID()))
{
now.clear(TimeZone.getDefault().getID());
}
if (Math.abs(now.toMillis(false) - System.currentTimeMillis()) > 5000)
{
now.setToNow();
now.normalize(true);
}
dueDate.normalize(true);
view.setText(new DateFormatter(view.getContext()).format(dueDate, now, DateFormatContext.LIST_VIEW));
if (dueIcon != null)
{
dueIcon.setVisibility(View.VISIBLE);
}
// highlight overdue dates & times, handle allDay tasks separately
if ((!dueDate.allDay && dueDate.before(now) || dueDate.allDay
&& (dueDate.year < now.year || dueDate.yearDay <= now.yearDay && dueDate.year == now.year))
&& !isClosed)
{
view.setTextAppearance(view.getContext(), R.style.task_list_overdue_text);
}
else
{
view.setTextAppearance(view.getContext(), R.style.task_list_due_text);
}
}
else if (view != null)
{
view.setText("");
if (dueIcon != null)
{
dueIcon.setVisibility(View.GONE);
}
}
}