Merge "Revert "Stop showing long names for local timezones when it is ambiguous""

This commit is contained in:
Neil Fuller
2015-06-08 17:13:42 +00:00
committed by Gerrit Code Review
2 changed files with 84 additions and 143 deletions

View File

@@ -29,6 +29,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle; import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
@@ -163,7 +164,7 @@ public class DateTimeSettings extends SettingsPreferenceFragment
Date dummyDate = mDummyDate.getTime(); Date dummyDate = mDummyDate.getTime();
mDatePref.setSummary(DateFormat.getLongDateFormat(context).format(now.getTime())); mDatePref.setSummary(DateFormat.getLongDateFormat(context).format(now.getTime()));
mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime())); mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime()));
mTimeZone.setSummary(getTimeZoneText(now.getTimeZone(), now.getTime())); mTimeZone.setSummary(getTimeZoneText(now.getTimeZone(), true));
mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate)); mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate));
} }
@@ -343,37 +344,32 @@ public class DateTimeSettings extends SettingsPreferenceFragment
} }
} }
private static String getTimeZoneText(TimeZone tz, Date now) { public static String getTimeZoneText(TimeZone tz, boolean includeName) {
Locale locale = Locale.getDefault(); Date now = new Date();
String gmtString = getGmtOffsetString(locale, tz);
String zoneNameString = getZoneLongName(locale, tz, now);
if (zoneNameString == null) {
return gmtString;
}
// We don't use punctuation here to avoid having to worry about localizing that too!
return gmtString + " " + zoneNameString;
}
public static String getZoneLongName(Locale locale, TimeZone tz, Date now) {
boolean daylight = tz.inDaylightTime(now);
// This returns a name if it can, or will fall back to GMT+0X:00 format.
return tz.getDisplayName(daylight, TimeZone.LONG, locale);
}
public static String getGmtOffsetString(Locale locale, TimeZone tz) {
// Use SimpleDateFormat to format the GMT+00:00 string. // Use SimpleDateFormat to format the GMT+00:00 string.
SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ"); SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
gmtFormatter.setTimeZone(tz); gmtFormatter.setTimeZone(tz);
Date now = new Date();
String gmtString = gmtFormatter.format(now); String gmtString = gmtFormatter.format(now);
// Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL. // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
BidiFormatter bidiFormatter = BidiFormatter.getInstance(); BidiFormatter bidiFormatter = BidiFormatter.getInstance();
boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL; Locale l = Locale.getDefault();
boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL;
gmtString = bidiFormatter.unicodeWrap(gmtString, gmtString = bidiFormatter.unicodeWrap(gmtString,
isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR); isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
return gmtString;
if (!includeName) {
return gmtString;
}
// Optionally append the time zone name.
SimpleDateFormat zoneNameFormatter = new SimpleDateFormat("zzzz");
zoneNameFormatter.setTimeZone(tz);
String zoneNameString = zoneNameFormatter.format(now);
// We don't use punctuation here to avoid having to worry about localizing that too!
return gmtString + " " + zoneNameString;
} }
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {

View File

@@ -34,17 +34,19 @@ import android.widget.SimpleAdapter;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.TreeSet; import libcore.icu.ICU;
import libcore.icu.TimeZoneNames; import libcore.icu.TimeZoneNames;
/** /**
@@ -102,7 +104,8 @@ public class ZonePicker extends ListFragment {
final String sortKey = (sortedByName ? KEY_DISPLAYNAME : KEY_OFFSET); final String sortKey = (sortedByName ? KEY_DISPLAYNAME : KEY_OFFSET);
final MyComparator comparator = new MyComparator(sortKey); final MyComparator comparator = new MyComparator(sortKey);
final List<Map<String, Object>> sortedList = getZones(context); ZoneGetter zoneGetter = new ZoneGetter();
final List<HashMap<String, Object>> sortedList = zoneGetter.getZones(context);
Collections.sort(sortedList, comparator); Collections.sort(sortedList, comparator);
final SimpleAdapter adapter = new SimpleAdapter(context, final SimpleAdapter adapter = new SimpleAdapter(context,
sortedList, sortedList,
@@ -221,132 +224,74 @@ public class ZonePicker extends ListFragment {
} }
} }
private static List<Map<String, Object>> getZones(Context context) { static class ZoneGetter {
final Locale locale = Locale.getDefault(); private final List<HashMap<String, Object>> mZones =
final Date now = new Date(); new ArrayList<HashMap<String, Object>>();
private final HashSet<String> mLocalZones = new HashSet<String>();
private final Date mNow = Calendar.getInstance().getTime();
private final SimpleDateFormat mZoneNameFormatter = new SimpleDateFormat("zzzz");
// The display name chosen for each zone entry depends on whether the zone is one associated private List<HashMap<String, Object>> getZones(Context context) {
// with the country of the user's chosen locale. For "local" zones we prefer the "long name" for (String olsonId : TimeZoneNames.forLocale(Locale.getDefault())) {
// (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local" mLocalZones.add(olsonId);
// zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English }
// speakers from outside the UK). This heuristic is based on the fact that people are try {
// typically familiar with their local timezones and exemplar locations don't always match XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
// modern-day expectations for people living in the country covered. Large countries like while (xrp.next() != XmlResourceParser.START_TAG) {
// China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near continue;
// "Shanghai" and prefer the long name over the exemplar location. The only time we don't
// follow this policy for local zones is when Android supplies multiple olson IDs to choose
// from and the use of a zone's long name leads to ambiguity. For example, at the time of
// writing Android lists 5 olson ids for Australia which collapse to 2 different zone names
// in winter but 4 different zone names in summer. The ambiguity leads to the users
// selecting the wrong olson ids.
// Get the list of olson ids to display to the user.
List<String> olsonIdsToDisplay = readTimezonesToDisplay(context);
// Create a lookup of local zone IDs.
Set<String> localZoneIds = new TreeSet<String>();
for (String olsonId : TimeZoneNames.forLocale(locale)) {
localZoneIds.add(olsonId);
}
// Work out whether the long names for the local entries that we would show by default would
// be ambiguous.
Set<String> localZoneNames = new TreeSet<String>();
boolean localLongNamesAreAmbiguous = false;
for (String olsonId : olsonIdsToDisplay) {
if (localZoneIds.contains(olsonId)) {
TimeZone tz = TimeZone.getTimeZone(olsonId);
String zoneLongName = DateTimeSettings.getZoneLongName(locale, tz, now);
boolean longNameIsUnique = localZoneNames.add(zoneLongName);
if (!longNameIsUnique) {
localLongNamesAreAmbiguous = true;
break;
} }
} xrp.next();
} while (xrp.getEventType() != XmlResourceParser.END_TAG) {
while (xrp.getEventType() != XmlResourceParser.START_TAG) {
// Generate the list of zone entries to return. if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>(); return mZones;
for (String olsonId : olsonIdsToDisplay) { }
final TimeZone tz = TimeZone.getTimeZone(olsonId); xrp.next();
// Exemplar location display is the default. The only time we intend to display the long }
// name is when the olsonId is local AND long names are not ambiguous. if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
boolean isLocalZoneId = localZoneIds.contains(olsonId); String olsonId = xrp.getAttributeValue(0);
boolean preferLongName = isLocalZoneId && !localLongNamesAreAmbiguous; addTimeZone(olsonId);
String displayName = getZoneDisplayName(locale, tz, now, preferLongName); }
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
String gmtOffsetString = DateTimeSettings.getGmtOffsetString(locale, tz); xrp.next();
int offsetMillis = tz.getOffset(now.getTime());
Map<String, Object> displayEntry =
createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis);
zones.add(displayEntry);
}
return zones;
}
private static Map<String, Object> createDisplayEntry(
TimeZone tz, String gmtOffsetString, String displayName, int offsetMillis) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(KEY_ID, tz.getID());
map.put(KEY_DISPLAYNAME, displayName);
map.put(KEY_GMT, gmtOffsetString);
map.put(KEY_OFFSET, offsetMillis);
return map;
}
private static List<String> readTimezonesToDisplay(Context context) {
List<String> olsonIds = new ArrayList<String>();
try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) {
while (xrp.next() != XmlResourceParser.START_TAG) {
continue;
}
xrp.next();
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
while (xrp.getEventType() != XmlResourceParser.START_TAG) {
if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
return olsonIds;
} }
xrp.next(); xrp.next();
} }
if (xrp.getName().equals(XMLTAG_TIMEZONE)) { xrp.close();
String olsonId = xrp.getAttributeValue(0); } catch (XmlPullParserException xppe) {
olsonIds.add(olsonId); Log.e(TAG, "Ill-formatted timezones.xml file");
} } catch (java.io.IOException ioe) {
while (xrp.getEventType() != XmlResourceParser.END_TAG) { Log.e(TAG, "Unable to read timezones.xml file");
xrp.next();
}
xrp.next();
} }
} catch (XmlPullParserException xppe) { return mZones;
Log.e(TAG, "Ill-formatted timezones.xml file");
} catch (java.io.IOException ioe) {
Log.e(TAG, "Unable to read timezones.xml file");
} }
return olsonIds;
}
/** private void addTimeZone(String olsonId) {
* Returns a name for the specific zone. If {@code preferLongName} is {@code true} then the // We always need the "GMT-07:00" string.
* long display name for the timezone will be used, otherwise the exemplar location will be final TimeZone tz = TimeZone.getTimeZone(olsonId);
* preferred.
*/ // For the display name, we treat time zones within the country differently
private static String getZoneDisplayName(Locale locale, TimeZone tz, Date now, // from other countries' time zones. So in en_US you'd get "Pacific Daylight Time"
boolean preferLongName) { // but in de_DE you'd get "Los Angeles" for the same time zone.
String zoneNameString; String displayName;
if (preferLongName) { if (mLocalZones.contains(olsonId)) {
zoneNameString = DateTimeSettings.getZoneLongName(locale, tz, now); // Within a country, we just use the local name for the time zone.
} else { mZoneNameFormatter.setTimeZone(tz);
zoneNameString = getZoneExemplarLocation(locale, tz); displayName = mZoneNameFormatter.format(mNow);
if (zoneNameString == null || zoneNameString.isEmpty()) { } else {
// getZoneExemplarLocation can return null. // For other countries' time zones, we use the exemplar location.
zoneNameString = DateTimeSettings.getZoneLongName(locale, tz, now); final String localeName = Locale.getDefault().toString();
displayName = TimeZoneNames.getExemplarLocation(localeName, olsonId);
} }
}
return zoneNameString;
}
private static String getZoneExemplarLocation(Locale locale, TimeZone tz) { final HashMap<String, Object> map = new HashMap<String, Object>();
return TimeZoneNames.getExemplarLocation(locale.toString(), tz.getID()); map.put(KEY_ID, olsonId);
map.put(KEY_DISPLAYNAME, displayName);
map.put(KEY_GMT, DateTimeSettings.getTimeZoneText(tz, false));
map.put(KEY_OFFSET, tz.getOffset(mNow.getTime()));
mZones.add(map);
}
} }
@Override @Override
@@ -368,7 +313,7 @@ public class ZonePicker extends ListFragment {
} }
} }
private static class MyComparator implements Comparator<Map<?, ?>> { private static class MyComparator implements Comparator<HashMap<?, ?>> {
private String mSortingKey; private String mSortingKey;
public MyComparator(String sortingKey) { public MyComparator(String sortingKey) {
@@ -379,7 +324,7 @@ public class ZonePicker extends ListFragment {
mSortingKey = sortingKey; mSortingKey = sortingKey;
} }
public int compare(Map<?, ?> map1, Map<?, ?> map2) { public int compare(HashMap<?, ?> map1, HashMap<?, ?> map2) {
Object value1 = map1.get(mSortingKey); Object value1 = map1.get(mSortingKey);
Object value2 = map2.get(mSortingKey); Object value2 = map2.get(mSortingKey);