blob: 26e8303d2bb846f58eadb517e30ee71e274fed6c [file] [log] [blame]
Tony Mantlerb3543e02015-05-28 14:48:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.settingslib.datetime;
18
19import android.content.Context;
20import android.content.res.XmlResourceParser;
Neil Fuller4a180122015-12-16 18:50:07 +000021import android.icu.text.TimeZoneNames;
Tony Mantlerb3543e02015-05-28 14:48:00 -070022import android.text.BidiFormatter;
23import android.text.TextDirectionHeuristics;
24import android.text.TextUtils;
25import android.util.Log;
26import android.view.View;
27
28import com.android.settingslib.R;
29
Tony Mantlerb3543e02015-05-28 14:48:00 -070030import org.xmlpull.v1.XmlPullParserException;
31
32import java.text.SimpleDateFormat;
33import java.util.ArrayList;
Tony Mantlerb3543e02015-05-28 14:48:00 -070034import java.util.Date;
35import java.util.HashMap;
Neil Fuller4a180122015-12-16 18:50:07 +000036import java.util.HashSet;
Tony Mantlerb3543e02015-05-28 14:48:00 -070037import java.util.List;
38import java.util.Locale;
Neil Fuller6394a392015-06-09 10:04:43 +010039import java.util.Map;
40import java.util.Set;
Tony Mantlerb3543e02015-05-28 14:48:00 -070041import java.util.TimeZone;
42
43public class ZoneGetter {
44 private static final String TAG = "ZoneGetter";
45
46 private static final String XMLTAG_TIMEZONE = "timezone";
47
48 public static final String KEY_ID = "id"; // value: String
49 public static final String KEY_DISPLAYNAME = "name"; // value: String
50 public static final String KEY_GMT = "gmt"; // value: String
51 public static final String KEY_OFFSET = "offset"; // value: int (Integer)
52
Neil Fuller6394a392015-06-09 10:04:43 +010053 private ZoneGetter() {}
Tony Mantlerb3543e02015-05-28 14:48:00 -070054
Neil Fuller6394a392015-06-09 10:04:43 +010055 public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) {
56 Locale locale = Locale.getDefault();
57 String gmtString = getGmtOffsetString(locale, tz, now);
Neil Fuller4a180122015-12-16 18:50:07 +000058 TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
59 String zoneNameString = getZoneLongName(timeZoneNames, tz, now);
Neil Fuller6394a392015-06-09 10:04:43 +010060 if (zoneNameString == null) {
61 return gmtString;
Tony Mantlerb3543e02015-05-28 14:48:00 -070062 }
Neil Fuller6394a392015-06-09 10:04:43 +010063
64 // We don't use punctuation here to avoid having to worry about localizing that too!
65 return gmtString + " " + zoneNameString;
66 }
67
68 public static List<Map<String, Object>> getZonesList(Context context) {
69 final Locale locale = Locale.getDefault();
70 final Date now = new Date();
Neil Fuller4a180122015-12-16 18:50:07 +000071 final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
Neil Fuller6394a392015-06-09 10:04:43 +010072
73 // The display name chosen for each zone entry depends on whether the zone is one associated
74 // with the country of the user's chosen locale. For "local" zones we prefer the "long name"
75 // (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local"
76 // zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English
77 // speakers from outside the UK). This heuristic is based on the fact that people are
78 // typically familiar with their local timezones and exemplar locations don't always match
79 // modern-day expectations for people living in the country covered. Large countries like
80 // China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near
81 // "Shanghai" and prefer the long name over the exemplar location. The only time we don't
82 // follow this policy for local zones is when Android supplies multiple olson IDs to choose
83 // from and the use of a zone's long name leads to ambiguity. For example, at the time of
84 // writing Android lists 5 olson ids for Australia which collapse to 2 different zone names
85 // in winter but 4 different zone names in summer. The ambiguity leads to the users
86 // selecting the wrong olson ids.
87
88 // Get the list of olson ids to display to the user.
Neil Fuller4a180122015-12-16 18:50:07 +000089 List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context);
90
91 // Store the information we are going to need more than once.
92 final int zoneCount = olsonIdsToDisplayList.size();
93 final String[] olsonIdsToDisplay = new String[zoneCount];
94 final TimeZone[] timeZones = new TimeZone[zoneCount];
95 final String[] gmtOffsetStrings = new String[zoneCount];
96 for (int i = 0; i < zoneCount; i++) {
97 String olsonId = olsonIdsToDisplayList.get(i);
98 olsonIdsToDisplay[i] = olsonId;
99 TimeZone tz = TimeZone.getTimeZone(olsonId);
100 timeZones[i] = tz;
101 gmtOffsetStrings[i] = getGmtOffsetString(locale, tz, now);
102 }
Neil Fuller6394a392015-06-09 10:04:43 +0100103
104 // Create a lookup of local zone IDs.
Neil Fuller4a180122015-12-16 18:50:07 +0000105 Set<String> localZoneIds = new HashSet<String>();
106 for (String olsonId : libcore.icu.TimeZoneNames.forLocale(locale)) {
Neil Fuller6394a392015-06-09 10:04:43 +0100107 localZoneIds.add(olsonId);
108 }
109
Neil Fuller4a180122015-12-16 18:50:07 +0000110 // Work out whether the display names we would show by default would be ambiguous.
111 Set<String> localZoneNames = new HashSet<String>();
112 boolean useExemplarLocationForLocalNames = false;
113 for (int i = 0; i < zoneCount; i++) {
114 String olsonId = olsonIdsToDisplay[i];
Neil Fuller6394a392015-06-09 10:04:43 +0100115 if (localZoneIds.contains(olsonId)) {
Neil Fuller4a180122015-12-16 18:50:07 +0000116 TimeZone tz = timeZones[i];
117 String displayName = getZoneLongName(timeZoneNames, tz, now);
118 if (displayName == null) {
119 displayName = gmtOffsetStrings[i];
120 }
121 boolean nameIsUnique = localZoneNames.add(displayName);
122 if (!nameIsUnique) {
123 useExemplarLocationForLocalNames = true;
Neil Fuller6394a392015-06-09 10:04:43 +0100124 break;
125 }
126 }
127 }
128
129 // Generate the list of zone entries to return.
130 List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>();
Neil Fuller4a180122015-12-16 18:50:07 +0000131 for (int i = 0; i < zoneCount; i++) {
132 String olsonId = olsonIdsToDisplay[i];
133 TimeZone tz = timeZones[i];
134 String gmtOffsetString = gmtOffsetStrings[i];
Neil Fuller6394a392015-06-09 10:04:43 +0100135
Neil Fuller4a180122015-12-16 18:50:07 +0000136 boolean isLocalZoneId = localZoneIds.contains(olsonId);
137 boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames;
138 String displayName;
139 if (preferLongName) {
140 displayName = getZoneLongName(timeZoneNames, tz, now);
141 } else {
142 displayName = timeZoneNames.getExemplarLocationName(tz.getID());
143 if (displayName == null || displayName.isEmpty()) {
144 // getZoneExemplarLocation can return null. Fall back to the long name.
145 displayName = getZoneLongName(timeZoneNames, tz, now);
146 }
147 }
148 if (displayName == null || displayName.isEmpty()) {
149 displayName = gmtOffsetString;
150 }
151
Neil Fuller6394a392015-06-09 10:04:43 +0100152 int offsetMillis = tz.getOffset(now.getTime());
153 Map<String, Object> displayEntry =
154 createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis);
155 zones.add(displayEntry);
156 }
157 return zones;
158 }
159
160 private static Map<String, Object> createDisplayEntry(
161 TimeZone tz, String gmtOffsetString, String displayName, int offsetMillis) {
162 Map<String, Object> map = new HashMap<String, Object>();
163 map.put(KEY_ID, tz.getID());
164 map.put(KEY_DISPLAYNAME, displayName);
165 map.put(KEY_GMT, gmtOffsetString);
166 map.put(KEY_OFFSET, offsetMillis);
167 return map;
168 }
169
Neil Fuller6394a392015-06-09 10:04:43 +0100170 private static List<String> readTimezonesToDisplay(Context context) {
171 List<String> olsonIds = new ArrayList<String>();
172 try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) {
Tony Mantlerb3543e02015-05-28 14:48:00 -0700173 while (xrp.next() != XmlResourceParser.START_TAG) {
174 continue;
175 }
176 xrp.next();
177 while (xrp.getEventType() != XmlResourceParser.END_TAG) {
178 while (xrp.getEventType() != XmlResourceParser.START_TAG) {
179 if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
Neil Fuller6394a392015-06-09 10:04:43 +0100180 return olsonIds;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700181 }
182 xrp.next();
183 }
184 if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
185 String olsonId = xrp.getAttributeValue(0);
Neil Fuller6394a392015-06-09 10:04:43 +0100186 olsonIds.add(olsonId);
Tony Mantlerb3543e02015-05-28 14:48:00 -0700187 }
188 while (xrp.getEventType() != XmlResourceParser.END_TAG) {
189 xrp.next();
190 }
191 xrp.next();
192 }
Tony Mantlerb3543e02015-05-28 14:48:00 -0700193 } catch (XmlPullParserException xppe) {
194 Log.e(TAG, "Ill-formatted timezones.xml file");
195 } catch (java.io.IOException ioe) {
196 Log.e(TAG, "Unable to read timezones.xml file");
197 }
Neil Fuller6394a392015-06-09 10:04:43 +0100198 return olsonIds;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700199 }
200
Neil Fuller4a180122015-12-16 18:50:07 +0000201 /**
202 * Returns the long name for the timezone for the given locale at the time specified.
203 * Can return {@code null}.
204 */
205 private static String getZoneLongName(TimeZoneNames names, TimeZone tz, Date now) {
206 TimeZoneNames.NameType nameType =
207 tz.inDaylightTime(now) ? TimeZoneNames.NameType.LONG_DAYLIGHT
208 : TimeZoneNames.NameType.LONG_STANDARD;
209 return names.getDisplayName(tz.getID(), nameType, now.getTime());
Tony Mantlerb3543e02015-05-28 14:48:00 -0700210 }
211
Neil Fuller6394a392015-06-09 10:04:43 +0100212 private static String getGmtOffsetString(Locale locale, TimeZone tz, Date now) {
Tony Mantlerb3543e02015-05-28 14:48:00 -0700213 // Use SimpleDateFormat to format the GMT+00:00 string.
214 SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
215 gmtFormatter.setTimeZone(tz);
216 String gmtString = gmtFormatter.format(now);
217
218 // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
219 BidiFormatter bidiFormatter = BidiFormatter.getInstance();
Neil Fuller6394a392015-06-09 10:04:43 +0100220 boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700221 gmtString = bidiFormatter.unicodeWrap(gmtString,
222 isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
Neil Fuller6394a392015-06-09 10:04:43 +0100223 return gmtString;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700224 }
225}