blob: 857ca49eafc2e1569747d1301d6fe05a36f52be8 [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
jackqdyuleic6a32742016-09-27 15:58:51 -070043/**
44 * ZoneGetter is the utility class to get time zone and zone list, and both of them have display
45 * name in time zone. In this class, we will keep consistency about display names for all
46 * the methods.
47 *
48 * The display name chosen for each zone entry depends on whether the zone is one associated
49 * with the country of the user's chosen locale. For "local" zones we prefer the "long name"
50 * (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local"
51 * zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English
52 * speakers from outside the UK). This heuristic is based on the fact that people are
53 * typically familiar with their local timezones and exemplar locations don't always match
54 * modern-day expectations for people living in the country covered. Large countries like
55 * China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near
56 * "Shanghai" and prefer the long name over the exemplar location. The only time we don't
57 * follow this policy for local zones is when Android supplies multiple olson IDs to choose
58 * from and the use of a zone's long name leads to ambiguity. For example, at the time of
59 * writing Android lists 5 olson ids for Australia which collapse to 2 different zone names
60 * in winter but 4 different zone names in summer. The ambiguity leads to the users
61 * selecting the wrong olson ids.
62 *
63 */
Tony Mantlerb3543e02015-05-28 14:48:00 -070064public class ZoneGetter {
65 private static final String TAG = "ZoneGetter";
66
Tony Mantlerb3543e02015-05-28 14:48:00 -070067 public static final String KEY_ID = "id"; // value: String
68 public static final String KEY_DISPLAYNAME = "name"; // value: String
69 public static final String KEY_GMT = "gmt"; // value: String
70 public static final String KEY_OFFSET = "offset"; // value: int (Integer)
71
jackqdyuleic6a32742016-09-27 15:58:51 -070072 private static final String XMLTAG_TIMEZONE = "timezone";
Tony Mantlerb3543e02015-05-28 14:48:00 -070073
jackqdyuleic6a32742016-09-27 15:58:51 -070074 public static String getTimeZoneOffsetAndName(Context context, TimeZone tz, Date now) {
75 final Locale locale = Locale.getDefault();
76 final String gmtString = getGmtOffsetString(locale, tz, now);
77 final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
78 final ZoneGetterData data = new ZoneGetterData(context);
79
80 final boolean useExemplarLocationForLocalNames =
81 shouldUseExemplarLocationForLocalNames(data, timeZoneNames);
82 final String zoneNameString = getTimeZoneDisplayName(data, timeZoneNames,
83 useExemplarLocationForLocalNames, tz, tz.getID());
Neil Fuller6394a392015-06-09 10:04:43 +010084 if (zoneNameString == null) {
85 return gmtString;
Tony Mantlerb3543e02015-05-28 14:48:00 -070086 }
Neil Fuller6394a392015-06-09 10:04:43 +010087
88 // We don't use punctuation here to avoid having to worry about localizing that too!
89 return gmtString + " " + zoneNameString;
90 }
91
92 public static List<Map<String, Object>> getZonesList(Context context) {
93 final Locale locale = Locale.getDefault();
94 final Date now = new Date();
Neil Fuller4a180122015-12-16 18:50:07 +000095 final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
jackqdyuleic6a32742016-09-27 15:58:51 -070096 final ZoneGetterData data = new ZoneGetterData(context);
Neil Fuller6394a392015-06-09 10:04:43 +010097
Neil Fuller4a180122015-12-16 18:50:07 +000098 // Work out whether the display names we would show by default would be ambiguous.
jackqdyuleic6a32742016-09-27 15:58:51 -070099 final boolean useExemplarLocationForLocalNames =
100 shouldUseExemplarLocationForLocalNames(data, timeZoneNames);
Neil Fuller6394a392015-06-09 10:04:43 +0100101
102 // Generate the list of zone entries to return.
103 List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>();
jackqdyuleic6a32742016-09-27 15:58:51 -0700104 for (int i = 0; i < data.zoneCount; i++) {
105 TimeZone tz = data.timeZones[i];
106 String gmtOffsetString = data.gmtOffsetStrings[i];
Neil Fuller6394a392015-06-09 10:04:43 +0100107
jackqdyuleic6a32742016-09-27 15:58:51 -0700108 String displayName = getTimeZoneDisplayName(data, timeZoneNames,
109 useExemplarLocationForLocalNames, tz, data.olsonIdsToDisplay[i]);
Neil Fuller4a180122015-12-16 18:50:07 +0000110 if (displayName == null || displayName.isEmpty()) {
111 displayName = gmtOffsetString;
112 }
113
Neil Fuller6394a392015-06-09 10:04:43 +0100114 int offsetMillis = tz.getOffset(now.getTime());
115 Map<String, Object> displayEntry =
116 createDisplayEntry(tz, gmtOffsetString, displayName, offsetMillis);
117 zones.add(displayEntry);
118 }
119 return zones;
120 }
121
122 private static Map<String, Object> createDisplayEntry(
123 TimeZone tz, String gmtOffsetString, String displayName, int offsetMillis) {
124 Map<String, Object> map = new HashMap<String, Object>();
125 map.put(KEY_ID, tz.getID());
126 map.put(KEY_DISPLAYNAME, displayName);
127 map.put(KEY_GMT, gmtOffsetString);
128 map.put(KEY_OFFSET, offsetMillis);
129 return map;
130 }
131
Neil Fuller6394a392015-06-09 10:04:43 +0100132 private static List<String> readTimezonesToDisplay(Context context) {
133 List<String> olsonIds = new ArrayList<String>();
134 try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) {
Tony Mantlerb3543e02015-05-28 14:48:00 -0700135 while (xrp.next() != XmlResourceParser.START_TAG) {
136 continue;
137 }
138 xrp.next();
139 while (xrp.getEventType() != XmlResourceParser.END_TAG) {
140 while (xrp.getEventType() != XmlResourceParser.START_TAG) {
141 if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
Neil Fuller6394a392015-06-09 10:04:43 +0100142 return olsonIds;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700143 }
144 xrp.next();
145 }
146 if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
147 String olsonId = xrp.getAttributeValue(0);
Neil Fuller6394a392015-06-09 10:04:43 +0100148 olsonIds.add(olsonId);
Tony Mantlerb3543e02015-05-28 14:48:00 -0700149 }
150 while (xrp.getEventType() != XmlResourceParser.END_TAG) {
151 xrp.next();
152 }
153 xrp.next();
154 }
Tony Mantlerb3543e02015-05-28 14:48:00 -0700155 } catch (XmlPullParserException xppe) {
156 Log.e(TAG, "Ill-formatted timezones.xml file");
157 } catch (java.io.IOException ioe) {
158 Log.e(TAG, "Unable to read timezones.xml file");
159 }
Neil Fuller6394a392015-06-09 10:04:43 +0100160 return olsonIds;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700161 }
162
jackqdyuleic6a32742016-09-27 15:58:51 -0700163 private static boolean shouldUseExemplarLocationForLocalNames(ZoneGetterData data,
164 TimeZoneNames timeZoneNames) {
165 final Set<String> localZoneNames = new HashSet<String>();
166 final Date now = new Date();
167 for (int i = 0; i < data.zoneCount; i++) {
168 final String olsonId = data.olsonIdsToDisplay[i];
169 if (data.localZoneIds.contains(olsonId)) {
170 final TimeZone tz = data.timeZones[i];
171 String displayName = getZoneLongName(timeZoneNames, tz, now);
172 if (displayName == null) {
173 displayName = data.gmtOffsetStrings[i];
174 }
175 final boolean nameIsUnique = localZoneNames.add(displayName);
176 if (!nameIsUnique) {
177 return true;
178 }
179 }
180 }
181
182 return false;
183 }
184
185 private static String getTimeZoneDisplayName(ZoneGetterData data, TimeZoneNames timeZoneNames,
186 boolean useExemplarLocationForLocalNames, TimeZone tz, String olsonId) {
187 final Date now = new Date();
188 final boolean isLocalZoneId = data.localZoneIds.contains(olsonId);
189 final boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames;
190 String displayName;
191
192 if (preferLongName) {
193 displayName = getZoneLongName(timeZoneNames, tz, now);
194 } else {
195 displayName = timeZoneNames.getExemplarLocationName(tz.getID());
196 if (displayName == null || displayName.isEmpty()) {
197 // getZoneExemplarLocation can return null. Fall back to the long name.
198 displayName = getZoneLongName(timeZoneNames, tz, now);
199 }
200 }
201
202 return displayName;
203 }
204
Neil Fuller4a180122015-12-16 18:50:07 +0000205 /**
206 * Returns the long name for the timezone for the given locale at the time specified.
207 * Can return {@code null}.
208 */
209 private static String getZoneLongName(TimeZoneNames names, TimeZone tz, Date now) {
jackqdyuleic6a32742016-09-27 15:58:51 -0700210 final TimeZoneNames.NameType nameType =
Neil Fuller4a180122015-12-16 18:50:07 +0000211 tz.inDaylightTime(now) ? TimeZoneNames.NameType.LONG_DAYLIGHT
jackqdyuleic6a32742016-09-27 15:58:51 -0700212 : TimeZoneNames.NameType.LONG_STANDARD;
Neil Fuller4a180122015-12-16 18:50:07 +0000213 return names.getDisplayName(tz.getID(), nameType, now.getTime());
Tony Mantlerb3543e02015-05-28 14:48:00 -0700214 }
215
Neil Fuller6394a392015-06-09 10:04:43 +0100216 private static String getGmtOffsetString(Locale locale, TimeZone tz, Date now) {
Tony Mantlerb3543e02015-05-28 14:48:00 -0700217 // Use SimpleDateFormat to format the GMT+00:00 string.
jackqdyuleic6a32742016-09-27 15:58:51 -0700218 final SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
Tony Mantlerb3543e02015-05-28 14:48:00 -0700219 gmtFormatter.setTimeZone(tz);
220 String gmtString = gmtFormatter.format(now);
221
222 // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
jackqdyuleic6a32742016-09-27 15:58:51 -0700223 final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
Neil Fuller6394a392015-06-09 10:04:43 +0100224 boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700225 gmtString = bidiFormatter.unicodeWrap(gmtString,
226 isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
Neil Fuller6394a392015-06-09 10:04:43 +0100227 return gmtString;
Tony Mantlerb3543e02015-05-28 14:48:00 -0700228 }
jackqdyuleic6a32742016-09-27 15:58:51 -0700229
230 private static final class ZoneGetterData {
231 public final String[] olsonIdsToDisplay;
232 public final String[] gmtOffsetStrings;
233 public final TimeZone[] timeZones;
234 public final Set<String> localZoneIds;
235 public final int zoneCount;
236
237 public ZoneGetterData(Context context) {
238 final Locale locale = Locale.getDefault();
239 final Date now = new Date();
240 final List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context);
241
242 // Load all the data needed to display time zones
243 zoneCount = olsonIdsToDisplayList.size();
244 olsonIdsToDisplay = new String[zoneCount];
245 timeZones = new TimeZone[zoneCount];
246 gmtOffsetStrings = new String[zoneCount];
247 for (int i = 0; i < zoneCount; i++) {
248 final String olsonId = olsonIdsToDisplayList.get(i);
249 olsonIdsToDisplay[i] = olsonId;
250 final TimeZone tz = TimeZone.getTimeZone(olsonId);
251 timeZones[i] = tz;
252 gmtOffsetStrings[i] = getGmtOffsetString(locale, tz, now);
253 }
254
255 // Create a lookup of local zone IDs.
256 localZoneIds = new HashSet<String>();
257 for (String olsonId : libcore.icu.TimeZoneNames.forLocale(locale)) {
258 localZoneIds.add(olsonId);
259 }
260 }
261 }
262}