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; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 22 | import android.support.v4.text.BidiFormatter; |
| 23 | import android.support.v4.text.TextDirectionHeuristicsCompat; |
| 24 | import android.text.SpannableString; |
| 25 | import android.text.SpannableStringBuilder; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 27 | import android.text.format.DateUtils; |
| 28 | import android.text.style.TtsSpan; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.view.View; |
| 31 | |
| 32 | import com.android.settingslib.R; |
| 33 | |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 34 | import org.xmlpull.v1.XmlPullParserException; |
| 35 | |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 37 | import java.util.Date; |
| 38 | import java.util.HashMap; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 39 | import java.util.HashSet; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Locale; |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 42 | import java.util.Map; |
| 43 | import java.util.Set; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 44 | import java.util.TimeZone; |
| 45 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 46 | /** |
| 47 | * ZoneGetter is the utility class to get time zone and zone list, and both of them have display |
| 48 | * name in time zone. In this class, we will keep consistency about display names for all |
| 49 | * the methods. |
| 50 | * |
| 51 | * The display name chosen for each zone entry depends on whether the zone is one associated |
| 52 | * with the country of the user's chosen locale. For "local" zones we prefer the "long name" |
| 53 | * (e.g. "Europe/London" -> "British Summer Time" for people in the UK). For "non-local" |
| 54 | * zones we prefer the exemplar location (e.g. "Europe/London" -> "London" for English |
| 55 | * speakers from outside the UK). This heuristic is based on the fact that people are |
| 56 | * typically familiar with their local timezones and exemplar locations don't always match |
| 57 | * modern-day expectations for people living in the country covered. Large countries like |
| 58 | * China that mostly use a single timezone (olson id: "Asia/Shanghai") may not live near |
| 59 | * "Shanghai" and prefer the long name over the exemplar location. The only time we don't |
| 60 | * follow this policy for local zones is when Android supplies multiple olson IDs to choose |
| 61 | * from and the use of a zone's long name leads to ambiguity. For example, at the time of |
| 62 | * writing Android lists 5 olson ids for Australia which collapse to 2 different zone names |
| 63 | * in winter but 4 different zone names in summer. The ambiguity leads to the users |
| 64 | * selecting the wrong olson ids. |
| 65 | * |
| 66 | */ |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 67 | public class ZoneGetter { |
| 68 | private static final String TAG = "ZoneGetter"; |
| 69 | |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 70 | public static final String KEY_ID = "id"; // value: String |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 71 | |
| 72 | /** |
| 73 | * @deprecated Use {@link #KEY_DISPLAY_LABEL} instead. |
| 74 | */ |
| 75 | @Deprecated |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 76 | public static final String KEY_DISPLAYNAME = "name"; // value: String |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 77 | |
| 78 | public static final String KEY_DISPLAY_LABEL = "display_label"; // value: CharSequence |
| 79 | |
| 80 | /** |
| 81 | * @deprecated Use {@link #KEY_OFFSET_LABEL} instead. |
| 82 | */ |
| 83 | @Deprecated |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 84 | public static final String KEY_GMT = "gmt"; // value: String |
| 85 | public static final String KEY_OFFSET = "offset"; // value: int (Integer) |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 86 | public static final String KEY_OFFSET_LABEL = "offset_label"; // value: CharSequence |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 87 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 88 | private static final String XMLTAG_TIMEZONE = "timezone"; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 89 | |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 90 | public static CharSequence getTimeZoneOffsetAndName(Context context, TimeZone tz, Date now) { |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 91 | final Locale locale = Locale.getDefault(); |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 92 | final CharSequence gmtText = getGmtOffsetText(context, locale, tz, now); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 93 | final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale); |
| 94 | final ZoneGetterData data = new ZoneGetterData(context); |
| 95 | |
| 96 | final boolean useExemplarLocationForLocalNames = |
| 97 | shouldUseExemplarLocationForLocalNames(data, timeZoneNames); |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 98 | final CharSequence zoneName = getTimeZoneDisplayName(data, timeZoneNames, |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 99 | useExemplarLocationForLocalNames, tz, tz.getID()); |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 100 | if (zoneName == null) { |
| 101 | return gmtText; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 102 | } |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 103 | |
| 104 | // We don't use punctuation here to avoid having to worry about localizing that too! |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 105 | return TextUtils.concat(gmtText, " ", zoneName); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public static List<Map<String, Object>> getZonesList(Context context) { |
| 109 | final Locale locale = Locale.getDefault(); |
| 110 | final Date now = new Date(); |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 111 | final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 112 | final ZoneGetterData data = new ZoneGetterData(context); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 113 | |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 114 | // 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] | 115 | final boolean useExemplarLocationForLocalNames = |
| 116 | shouldUseExemplarLocationForLocalNames(data, timeZoneNames); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 117 | |
| 118 | // Generate the list of zone entries to return. |
| 119 | List<Map<String, Object>> zones = new ArrayList<Map<String, Object>>(); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 120 | for (int i = 0; i < data.zoneCount; i++) { |
| 121 | TimeZone tz = data.timeZones[i]; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 122 | CharSequence gmtOffsetText = data.gmtOffsetTexts[i]; |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 123 | |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 124 | CharSequence displayName = getTimeZoneDisplayName(data, timeZoneNames, |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 125 | useExemplarLocationForLocalNames, tz, data.olsonIdsToDisplay[i]); |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 126 | if (TextUtils.isEmpty(displayName)) { |
| 127 | displayName = gmtOffsetText; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 130 | int offsetMillis = tz.getOffset(now.getTime()); |
| 131 | Map<String, Object> displayEntry = |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 132 | createDisplayEntry(tz, gmtOffsetText, displayName, offsetMillis); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 133 | zones.add(displayEntry); |
| 134 | } |
| 135 | return zones; |
| 136 | } |
| 137 | |
| 138 | private static Map<String, Object> createDisplayEntry( |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 139 | TimeZone tz, CharSequence gmtOffsetText, CharSequence displayName, int offsetMillis) { |
| 140 | Map<String, Object> map = new HashMap<>(); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 141 | map.put(KEY_ID, tz.getID()); |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 142 | map.put(KEY_DISPLAYNAME, displayName.toString()); |
| 143 | map.put(KEY_DISPLAY_LABEL, displayName); |
| 144 | map.put(KEY_GMT, gmtOffsetText.toString()); |
| 145 | map.put(KEY_OFFSET_LABEL, gmtOffsetText); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 146 | map.put(KEY_OFFSET, offsetMillis); |
| 147 | return map; |
| 148 | } |
| 149 | |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 150 | private static List<String> readTimezonesToDisplay(Context context) { |
| 151 | List<String> olsonIds = new ArrayList<String>(); |
| 152 | try (XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones)) { |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 153 | while (xrp.next() != XmlResourceParser.START_TAG) { |
| 154 | continue; |
| 155 | } |
| 156 | xrp.next(); |
| 157 | while (xrp.getEventType() != XmlResourceParser.END_TAG) { |
| 158 | while (xrp.getEventType() != XmlResourceParser.START_TAG) { |
| 159 | if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) { |
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 | xrp.next(); |
| 163 | } |
| 164 | if (xrp.getName().equals(XMLTAG_TIMEZONE)) { |
| 165 | String olsonId = xrp.getAttributeValue(0); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 166 | olsonIds.add(olsonId); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 167 | } |
| 168 | while (xrp.getEventType() != XmlResourceParser.END_TAG) { |
| 169 | xrp.next(); |
| 170 | } |
| 171 | xrp.next(); |
| 172 | } |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 173 | } catch (XmlPullParserException xppe) { |
| 174 | Log.e(TAG, "Ill-formatted timezones.xml file"); |
| 175 | } catch (java.io.IOException ioe) { |
| 176 | Log.e(TAG, "Unable to read timezones.xml file"); |
| 177 | } |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 178 | return olsonIds; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 179 | } |
| 180 | |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 181 | private static boolean shouldUseExemplarLocationForLocalNames(ZoneGetterData data, |
| 182 | TimeZoneNames timeZoneNames) { |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 183 | final Set<CharSequence> localZoneNames = new HashSet<>(); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 184 | final Date now = new Date(); |
| 185 | for (int i = 0; i < data.zoneCount; i++) { |
| 186 | final String olsonId = data.olsonIdsToDisplay[i]; |
| 187 | if (data.localZoneIds.contains(olsonId)) { |
| 188 | final TimeZone tz = data.timeZones[i]; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 189 | CharSequence displayName = getZoneLongName(timeZoneNames, tz, now); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 190 | if (displayName == null) { |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 191 | displayName = data.gmtOffsetTexts[i]; |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 192 | } |
| 193 | final boolean nameIsUnique = localZoneNames.add(displayName); |
| 194 | if (!nameIsUnique) { |
| 195 | return true; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return false; |
| 201 | } |
| 202 | |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 203 | private static CharSequence getTimeZoneDisplayName(ZoneGetterData data, |
| 204 | TimeZoneNames timeZoneNames, boolean useExemplarLocationForLocalNames, TimeZone tz, |
| 205 | String olsonId) { |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 206 | final Date now = new Date(); |
| 207 | final boolean isLocalZoneId = data.localZoneIds.contains(olsonId); |
| 208 | final boolean preferLongName = isLocalZoneId && !useExemplarLocationForLocalNames; |
| 209 | String displayName; |
| 210 | |
| 211 | if (preferLongName) { |
| 212 | displayName = getZoneLongName(timeZoneNames, tz, now); |
| 213 | } else { |
| 214 | displayName = timeZoneNames.getExemplarLocationName(tz.getID()); |
| 215 | if (displayName == null || displayName.isEmpty()) { |
| 216 | // getZoneExemplarLocation can return null. Fall back to the long name. |
| 217 | displayName = getZoneLongName(timeZoneNames, tz, now); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return displayName; |
| 222 | } |
| 223 | |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Returns the long name for the timezone for the given locale at the time specified. |
| 226 | * Can return {@code null}. |
| 227 | */ |
| 228 | private static String getZoneLongName(TimeZoneNames names, TimeZone tz, Date now) { |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 229 | final TimeZoneNames.NameType nameType = |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 230 | tz.inDaylightTime(now) ? TimeZoneNames.NameType.LONG_DAYLIGHT |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 231 | : TimeZoneNames.NameType.LONG_STANDARD; |
Neil Fuller | 4a18012 | 2015-12-16 18:50:07 +0000 | [diff] [blame] | 232 | return names.getDisplayName(tz.getID(), nameType, now.getTime()); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 235 | private static void appendWithTtsSpan(SpannableStringBuilder builder, CharSequence content, |
| 236 | TtsSpan span) { |
| 237 | int start = builder.length(); |
| 238 | builder.append(content); |
| 239 | builder.setSpan(span, start, builder.length(), 0); |
| 240 | } |
| 241 | |
| 242 | private static String twoDigits(int input) { |
| 243 | StringBuilder builder = new StringBuilder(3); |
| 244 | if (input < 0) builder.append('-'); |
| 245 | String string = Integer.toString(Math.abs(input)); |
| 246 | if (string.length() == 1) builder.append("0"); |
| 247 | builder.append(string); |
| 248 | return builder.toString(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Get the GMT offset text label for the given time zone, in the format "GMT-08:00". This will |
| 253 | * also add TTS spans to give hints to the text-to-speech engine for the type of data it is. |
| 254 | * |
| 255 | * @param context The context which the string is displayed in. |
| 256 | * @param locale The locale which the string is displayed in. This should be the same as the |
| 257 | * locale of the context. |
| 258 | * @param tz Time zone to get the GMT offset from. |
| 259 | * @param now The current time, used to tell whether daylight savings is active. |
| 260 | * @return A CharSequence suitable for display as the offset label of {@code tz}. |
| 261 | */ |
| 262 | private static CharSequence getGmtOffsetText(Context context, Locale locale, TimeZone tz, |
| 263 | Date now) { |
| 264 | SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 265 | |
| 266 | appendWithTtsSpan(builder, "GMT", |
| 267 | new TtsSpan.TextBuilder(context.getString(R.string.time_zone_gmt)).build()); |
| 268 | |
| 269 | int offsetMillis = tz.getOffset(now.getTime()); |
| 270 | if (offsetMillis >= 0) { |
| 271 | appendWithTtsSpan(builder, "+", new TtsSpan.VerbatimBuilder("+").build()); |
| 272 | } |
| 273 | |
| 274 | final int offsetHours = (int) (offsetMillis / DateUtils.HOUR_IN_MILLIS); |
| 275 | appendWithTtsSpan(builder, twoDigits(offsetHours), |
| 276 | new TtsSpan.MeasureBuilder().setNumber(offsetHours).setUnit("hour").build()); |
| 277 | |
| 278 | builder.append(":"); |
| 279 | |
| 280 | final int offsetMinutes = (int) (offsetMillis / DateUtils.MINUTE_IN_MILLIS); |
| 281 | final int offsetMinutesRemaining = Math.abs(offsetMinutes) % 60; |
| 282 | appendWithTtsSpan(builder, twoDigits(offsetMinutesRemaining), |
| 283 | new TtsSpan.MeasureBuilder().setNumber(offsetMinutesRemaining) |
| 284 | .setUnit("minute").build()); |
| 285 | |
| 286 | CharSequence gmtText = new SpannableString(builder); |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 287 | |
| 288 | // 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] | 289 | final BidiFormatter bidiFormatter = BidiFormatter.getInstance(); |
Neil Fuller | 6394a39 | 2015-06-09 10:04:43 +0100 | [diff] [blame] | 290 | boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 291 | gmtText = bidiFormatter.unicodeWrap(gmtText, |
| 292 | isRtl ? TextDirectionHeuristicsCompat.RTL : TextDirectionHeuristicsCompat.LTR); |
| 293 | return gmtText; |
Tony Mantler | b3543e0 | 2015-05-28 14:48:00 -0700 | [diff] [blame] | 294 | } |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 295 | |
| 296 | private static final class ZoneGetterData { |
| 297 | public final String[] olsonIdsToDisplay; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 298 | public final CharSequence[] gmtOffsetTexts; |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 299 | public final TimeZone[] timeZones; |
| 300 | public final Set<String> localZoneIds; |
| 301 | public final int zoneCount; |
| 302 | |
| 303 | public ZoneGetterData(Context context) { |
| 304 | final Locale locale = Locale.getDefault(); |
| 305 | final Date now = new Date(); |
| 306 | final List<String> olsonIdsToDisplayList = readTimezonesToDisplay(context); |
| 307 | |
| 308 | // Load all the data needed to display time zones |
| 309 | zoneCount = olsonIdsToDisplayList.size(); |
| 310 | olsonIdsToDisplay = new String[zoneCount]; |
| 311 | timeZones = new TimeZone[zoneCount]; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 312 | gmtOffsetTexts = new CharSequence[zoneCount]; |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 313 | for (int i = 0; i < zoneCount; i++) { |
| 314 | final String olsonId = olsonIdsToDisplayList.get(i); |
| 315 | olsonIdsToDisplay[i] = olsonId; |
| 316 | final TimeZone tz = TimeZone.getTimeZone(olsonId); |
| 317 | timeZones[i] = tz; |
Maurice Lam | ebc050f | 2016-10-31 16:17:53 -0700 | [diff] [blame^] | 318 | gmtOffsetTexts[i] = getGmtOffsetText(context, locale, tz, now); |
jackqdyulei | c6a3274 | 2016-09-27 15:58:51 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Create a lookup of local zone IDs. |
| 322 | localZoneIds = new HashSet<String>(); |
| 323 | for (String olsonId : libcore.icu.TimeZoneNames.forLocale(locale)) { |
| 324 | localZoneIds.add(olsonId); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |