• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.settings.datetime.timezone;
18 
19 import android.app.settings.SettingsEnums;
20 import android.icu.util.TimeZone;
21 
22 import com.android.settings.R;
23 import com.android.settings.datetime.timezone.model.TimeZoneData;
24 
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Locale;
30 
31 /**
32  * Render a list of fixed offset time zone {@class TimeZoneInfo} into a list view.
33  */
34 public class FixedOffsetPicker extends BaseTimeZoneInfoPicker {
35     /**
36      * Range of integer fixed UTC offsets shown in the pickers.
37      */
38     private static final int MIN_HOURS_OFFSET = -14;
39     private static final int MAX_HOURS_OFFSET = +12;
40 
FixedOffsetPicker()41     public FixedOffsetPicker() {
42         super(R.string.date_time_select_fixed_offset_time_zones,
43                 R.string.search_settings, false, false);
44     }
45 
46     @Override
getMetricsCategory()47     public int getMetricsCategory() {
48         return SettingsEnums.SETTINGS_ZONE_PICKER_FIXED_OFFSET;
49     }
50 
51     @Override
getAllTimeZoneInfos(TimeZoneData timeZoneData)52     public List<TimeZoneInfo> getAllTimeZoneInfos(TimeZoneData timeZoneData) {
53         return loadFixedOffsets();
54     }
55 
56     /**
57      * Returns a {@link TimeZoneInfo} for each fixed offset time zone, such as UTC or GMT+4. The
58      * returned list will be sorted in a reasonable way for display.
59      */
loadFixedOffsets()60     private List<TimeZoneInfo> loadFixedOffsets() {
61         final TimeZoneInfo.Formatter formatter = new TimeZoneInfo.Formatter(getLocale(),
62                 new Date());
63         final List<TimeZoneInfo> timeZoneInfos = new ArrayList<>();
64         timeZoneInfos.add(formatter.format(TimeZone.getFrozenTimeZone("Etc/UTC")));
65         for (int hoursOffset = MAX_HOURS_OFFSET; hoursOffset >= MIN_HOURS_OFFSET; --hoursOffset) {
66             if (hoursOffset == 0) {
67                 // UTC is handled above, so don't add GMT +/-0 again.
68                 continue;
69             }
70             final String id = String.format(Locale.US, "Etc/GMT%+d", hoursOffset);
71             timeZoneInfos.add(formatter.format(TimeZone.getFrozenTimeZone(id)));
72         }
73         return Collections.unmodifiableList(timeZoneInfos);
74     }
75 }
76