Add methods for Time_Delegate [DO NOT MERGE]
Bug: http://b.android.com/65359
Change-Id: I7c2d09286d6bcd9899444aaa5a4a5a342e39d923
(cherry-picked from ddb7f3273c)
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package android.text.format;
|
package android.text.format;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
import java.util.UnknownFormatConversionException;
|
import java.util.UnknownFormatConversionException;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@@ -35,6 +36,28 @@ public class Time_Delegate {
|
|||||||
// Regex to match odd number of '%'.
|
// Regex to match odd number of '%'.
|
||||||
private static final Pattern p = Pattern.compile("(?<!%)(%%)*%(?!%)");
|
private static final Pattern p = Pattern.compile("(?<!%)(%%)*%(?!%)");
|
||||||
|
|
||||||
|
// Format used by toString()
|
||||||
|
private static final String FORMAT = "%1$tY%1$tm%1$tdT%1$tH%1$tM%1$tS<%1$tZ>";
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static long normalize(Time thisTime, boolean ignoreDst) {
|
||||||
|
long millis = toMillis(thisTime, ignoreDst);
|
||||||
|
set(thisTime, millis);
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static void switchTimezone(Time thisTime, String timezone) {
|
||||||
|
Calendar c = timeToCalendar(thisTime);
|
||||||
|
c.setTimeZone(TimeZone.getTimeZone(timezone));
|
||||||
|
calendarToTime(c, thisTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static int nativeCompare(Time a, Time b) {
|
||||||
|
return timeToCalendar(a).compareTo(timeToCalendar(b));
|
||||||
|
}
|
||||||
|
|
||||||
@LayoutlibDelegate
|
@LayoutlibDelegate
|
||||||
/*package*/ static String format1(Time thisTime, String format) {
|
/*package*/ static String format1(Time thisTime, String format) {
|
||||||
|
|
||||||
@@ -46,16 +69,92 @@ public class Time_Delegate {
|
|||||||
// of $.
|
// of $.
|
||||||
return String.format(
|
return String.format(
|
||||||
p.matcher(format).replaceAll("$0\\1\\$t"),
|
p.matcher(format).replaceAll("$0\\1\\$t"),
|
||||||
timeToCalendar(thisTime, Calendar.getInstance()));
|
timeToCalendar(thisTime));
|
||||||
} catch (UnknownFormatConversionException e) {
|
} catch (UnknownFormatConversionException e) {
|
||||||
Bridge.getLog().fidelityWarning(LayoutLog.TAG_STRFTIME, "Unrecognized format", e, format);
|
Bridge.getLog().fidelityWarning(LayoutLog.TAG_STRFTIME, "Unrecognized format", e, format);
|
||||||
return format;
|
return format;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Calendar timeToCalendar(Time time, Calendar calendar) {
|
/**
|
||||||
|
* Return the current time in YYYYMMDDTHHMMSS<tz> format
|
||||||
|
*/
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static String toString(Time thisTime) {
|
||||||
|
Calendar c = timeToCalendar(thisTime);
|
||||||
|
return String.format(FORMAT, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static boolean nativeParse(Time thisTime, String s) {
|
||||||
|
Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
|
||||||
|
"android.text.format.Time.parse() not supported.", null);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static boolean nativeParse3339(Time thisTime, String s) {
|
||||||
|
Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
|
||||||
|
"android.text.format.Time.parse3339() not supported.", null);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static void setToNow(Time thisTime) {
|
||||||
|
calendarToTime(getCalendarInstance(thisTime), thisTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static long toMillis(Time thisTime, boolean ignoreDst) {
|
||||||
|
// TODO: Respect ignoreDst.
|
||||||
|
return timeToCalendar(thisTime).getTimeInMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static void set(Time thisTime, long millis) {
|
||||||
|
Calendar c = getCalendarInstance(thisTime);
|
||||||
|
c.setTimeInMillis(millis);
|
||||||
|
calendarToTime(c,thisTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@LayoutlibDelegate
|
||||||
|
/*package*/ static String format2445(Time thisTime) {
|
||||||
|
Bridge.getLog().error(LayoutLog.TAG_UNSUPPORTED,
|
||||||
|
"android.text.format.Time.format2445() not supported.", null);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- private helper methods ----
|
||||||
|
|
||||||
|
private static Calendar timeToCalendar(Time time) {
|
||||||
|
Calendar calendar = getCalendarInstance(time);
|
||||||
calendar.set(time.year, time.month, time.monthDay, time.hour, time.minute, time.second);
|
calendar.set(time.year, time.month, time.monthDay, time.hour, time.minute, time.second);
|
||||||
return calendar;
|
return calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void calendarToTime(Calendar c, Time time) {
|
||||||
|
time.timezone = c.getTimeZone().getID();
|
||||||
|
time.set(c.get(Calendar.SECOND), c.get(Calendar.MINUTE), c.get(Calendar.HOUR_OF_DAY),
|
||||||
|
c.get(Calendar.DATE), c.get(Calendar.MONTH), c.get(Calendar.YEAR));
|
||||||
|
time.weekDay = c.get(Calendar.DAY_OF_WEEK);
|
||||||
|
time.yearDay = c.get(Calendar.DAY_OF_YEAR);
|
||||||
|
time.isDst = c.getTimeZone().inDaylightTime(c.getTime()) ? 1 : 0;
|
||||||
|
// gmtoff is in seconds and TimeZone.getOffset() returns milliseconds.
|
||||||
|
time.gmtoff = c.getTimeZone().getOffset(c.getTimeInMillis()) / DateUtils.SECOND_IN_MILLIS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a calendar instance with the correct timezone.
|
||||||
|
*
|
||||||
|
* @param time Time to obtain the timezone from.
|
||||||
|
*/
|
||||||
|
private static Calendar getCalendarInstance(Time time) {
|
||||||
|
// TODO: Check platform code to make sure the behavior is same for null/invalid timezone.
|
||||||
|
if (time == null || time.timezone == null) {
|
||||||
|
// Default to local timezone.
|
||||||
|
return Calendar.getInstance();
|
||||||
|
}
|
||||||
|
// If timezone is invalid, use GMT.
|
||||||
|
return Calendar.getInstance(TimeZone.getTimeZone(time.timezone));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,7 +129,6 @@ public final class CreateInfo implements ICreateInfo {
|
|||||||
"android.os.HandlerThread#run",
|
"android.os.HandlerThread#run",
|
||||||
"android.os.Build#getString",
|
"android.os.Build#getString",
|
||||||
"android.text.format.DateFormat#is24HourFormat",
|
"android.text.format.DateFormat#is24HourFormat",
|
||||||
"android.text.format.Time#format1",
|
|
||||||
"android.view.Choreographer#getRefreshRate",
|
"android.view.Choreographer#getRefreshRate",
|
||||||
"android.view.Display#updateDisplayInfoLocked",
|
"android.view.Display#updateDisplayInfoLocked",
|
||||||
"android.view.LayoutInflater#rInflate",
|
"android.view.LayoutInflater#rInflate",
|
||||||
@@ -186,6 +185,7 @@ public final class CreateInfo implements ICreateInfo {
|
|||||||
"android.graphics.Xfermode",
|
"android.graphics.Xfermode",
|
||||||
"android.os.SystemClock",
|
"android.os.SystemClock",
|
||||||
"android.text.AndroidBidi",
|
"android.text.AndroidBidi",
|
||||||
|
"android.text.format.Time",
|
||||||
"android.util.FloatMath",
|
"android.util.FloatMath",
|
||||||
"android.view.Display",
|
"android.view.Display",
|
||||||
"libcore.icu.ICU",
|
"libcore.icu.ICU",
|
||||||
|
|||||||
Reference in New Issue
Block a user