Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settingslib.datetime; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.XmlResourceParser; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 21 | import android.icu.text.TimeZoneNames; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 22 | import android.text.BidiFormatter; |
| 23 | import android.text.TextDirectionHeuristics; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Log; |
| 26 | import android.view.View; |
| 27 | |
| 28 | import com.android.settingslib.R; |
| 29 | |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 30 | import org.xmlpull.v1.XmlPullParserException; |
| 31 | |
| 32 | import java.text.SimpleDateFormat; |
| 33 | import java.util.ArrayList; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 34 | import java.util.Date; |
| 35 | import java.util.HashMap; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 36 | import java.util.HashSet; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 37 | import java.util.List; |
| 38 | import java.util.Locale; |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 39 | import java.util.Map; |
| 40 | import java.util.Set; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 41 | import java.util.TimeZone; |
| 42 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 43 | /** |
| 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 Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 64 | public class ZoneGetter { |
| 65 | private static final String TAG = "ZoneGetter"; |
| 66 | |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 67 | 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 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 72 | private static final String XMLTAG_TIMEZONE = "timezone"; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 73 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 74 | 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 Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 84 | if (zoneNameString == null) { |
| 85 | return gmtString; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 86 | } |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 87 | |
| 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 Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 95 | final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 96 | final ZoneGetterData data = new ZoneGetterData(context); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 97 | |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 98 | // Work out whether the display names we would show by default would be ambiguous. |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 99 | final boolean useExemplarLocationForLocalNames = |
| 100 | shouldUseExemplarLocationForLocalNames(data, timeZoneNames); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 101 | |
| 102 | // Generate the list of zone entries to return. |
| 103 | List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>(); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 104 | for (int i = 0; i < data.zoneCount; i++) { |
| 105 | TimeZone tz = data.timeZones[i]; |
| 106 | String gmtOffsetString = data.gmtOffsetStrings[i]; |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 107 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 108 | String displayName = getTimeZoneDisplayName(data, timeZoneNames, |
| 109 | useExemplarLocationForLocalNames, tz, data.olsonIdsToDisplay[i]); |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 110 | if (displayName == null || displayName.isEmpty()) { |
| 111 | displayName = gmtOffsetString; |
| 112 | } |
| 113 | |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 114 | 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 Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 132 | 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 Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 135 | 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 Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 142 | return olsonIds; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 143 | } |
| 144 | xrp.next(); |
| 145 | } |
| 146 | if (xrp.getName().equals(XMLTAG_TIMEZONE)) { |
| 147 | String olsonId = xrp.getAttributeValue(0); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 148 | olsonIds.add(olsonId); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 149 | } |
| 150 | while (xrp.getEventType() != XmlResourceParser.END_TAG) { |
| 151 | xrp.next(); |
| 152 | } |
| 153 | xrp.next(); |
| 154 | } |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 155 | } 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 Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 160 | return olsonIds; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 161 | } |
| 162 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 163 | 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 Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 205 | /** |
| 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) { |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 210 | final TimeZoneNames.NameType nameType = |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 211 | tz.inDaylightTime(now) ? TimeZoneNames.NameType.LONG_DAYLIGHT |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 212 | : TimeZoneNames.NameType.LONG_STANDARD; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 213 | return names.getDisplayName(tz.getID(), nameType, now.getTime()); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 216 | private static String getGmtOffsetString(Locale locale, TimeZone tz, Date now) { |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 217 | // Use SimpleDateFormat to format the GMT+00:00 string. |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 218 | final SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ"); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 219 | 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. |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 223 | final BidiFormatter bidiFormatter = BidiFormatter.getInstance(); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 224 | boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 225 | gmtString = bidiFormatter.unicodeWrap(gmtString, |
| 226 | isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 227 | return gmtString; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 228 | } |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame^] | 229 | |
| 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 | } |