TimeZonePickerUtilspublic class TimeZonePickerUtils extends Object
Fields Summary |
---|
private static final String | TAG | public static final int | GMT_TEXT_COLOR | public static final int | DST_SYMBOL_COLOR | private static final android.text.Spannable.Factory | mSpannableFactory | private Locale | mDefaultLocale | private String[] | mOverrideIds | private String[] | mOverrideLabels |
Constructors Summary |
---|
public TimeZonePickerUtils(android.content.Context context)This needs to be an instantiated class so that it doesn't need to continuously re-load the
list of timezone IDs that need to be overridden.
// Instead of saving a reference to the context (because we might need to look up the
// labels every time getGmtDisplayName is called), we'll cache the lists of override IDs
// and labels now.
cacheOverrides(context);
|
Methods Summary |
---|
public static void | appendGmtOffset(java.lang.StringBuilder sb, int gmtOffset)
sb.append("GMT");
if (gmtOffset < 0) {
sb.append('-");
} else {
sb.append('+");
}
final int p = Math.abs(gmtOffset);
sb.append(p / DateUtils.HOUR_IN_MILLIS); // Hour
final int min = (p / (int) DateUtils.MINUTE_IN_MILLIS) % 60;
if (min != 0) { // Show minutes if non-zero
sb.append(':");
if (min < 10) {
sb.append('0");
}
sb.append(min);
}
| private java.lang.CharSequence | buildGmtDisplayName(java.util.TimeZone tz, long timeMillis, boolean grayGmt)
Time time = new Time(tz.getID());
time.set(timeMillis);
StringBuilder sb = new StringBuilder();
String displayName = getDisplayName(tz, time.isDst != 0);
sb.append(displayName);
sb.append(" ");
final int gmtOffset = tz.getOffset(timeMillis);
int gmtStart = sb.length();
appendGmtOffset(sb, gmtOffset);
int gmtEnd = sb.length();
int symbolStart = 0;
int symbolEnd = 0;
if (tz.useDaylightTime()) {
sb.append(" ");
symbolStart = sb.length();
sb.append(getDstSymbol()); // Sun symbol
symbolEnd = sb.length();
}
// Set the gray colors.
Spannable spannableText = mSpannableFactory.newSpannable(sb);
if (grayGmt) {
spannableText.setSpan(new ForegroundColorSpan(GMT_TEXT_COLOR),
gmtStart, gmtEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (tz.useDaylightTime()) {
spannableText.setSpan(new ForegroundColorSpan(DST_SYMBOL_COLOR),
symbolStart, symbolEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
CharSequence gmtDisplayName = spannableText;
return gmtDisplayName;
| private void | cacheOverrides(android.content.Context context)
Resources res = context.getResources();
mOverrideIds = res.getStringArray(R.array.timezone_rename_ids);
mOverrideLabels = res.getStringArray(R.array.timezone_rename_labels);
| private java.lang.String | getDisplayName(java.util.TimeZone tz, boolean daylightTime)Gets the display name for the specified Timezone ID. If the ID matches the list of IDs that
need to be have their default display names overriden, use the pre-set display name from
R.arrays.
if (mOverrideIds == null || mOverrideLabels == null) {
// Just in case they somehow didn't get loaded correctly.
return tz.getDisplayName(daylightTime, TimeZone.LONG, Locale.getDefault());
}
for (int i = 0; i < mOverrideIds.length; i++) {
if (tz.getID().equals(mOverrideIds[i])) {
if (mOverrideLabels.length > i) {
return mOverrideLabels[i];
}
Log.e(TAG, "timezone_rename_ids len=" + mOverrideIds.length +
" timezone_rename_labels len=" + mOverrideLabels.length);
break;
}
}
// If the ID doesn't need to have the display name overridden, or if the labels were
// malformed, just use the default.
return tz.getDisplayName(daylightTime, TimeZone.LONG, Locale.getDefault());
| public static char | getDstSymbol()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return '\u2600"; // The Sun emoji icon.
} else {
return '*";
}
| public java.lang.CharSequence | getGmtDisplayName(android.content.Context context, java.lang.String id, long millis, boolean grayGmt)Given a timezone id (e.g. America/Los_Angeles), returns the corresponding timezone
display name (e.g. Pacific Time GMT-7).
TimeZone timezone = TimeZone.getTimeZone(id);
if (timezone == null) {
return null;
}
final Locale defaultLocale = Locale.getDefault();
if (!defaultLocale.equals(mDefaultLocale)) {
// If the IDs and labels haven't been set yet, or if the locale has been changed
// recently, we'll need to re-cache them.
mDefaultLocale = defaultLocale;
cacheOverrides(context);
}
return buildGmtDisplayName(timezone, millis, grayGmt);
|
|