FilteredCountryTimeZones.java revision 490e46e01b789cf3487c60b781a353e30ae7d225
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
17package com.android.settings.datetime.timezone.model;
18
19import libcore.util.CountryTimeZones;
20
21import java.util.Collections;
22import java.util.List;
23import java.util.stream.Collectors;
24
25/**
26 * Wrap {@class CountryTimeZones} to filter time zone that are shown in the picker.
27 */
28public class FilteredCountryTimeZones {
29
30    // New timezone list and the meta data of time zone, notUsedAfter, is introduced in Android P
31    // in 2018. Only show time zone used in or after 2018.
32    private static final long MIN_USE_DATE_OF_TIMEZONE = 1514764800000L; // 1/1/2018 00:00 UTC
33
34    private final CountryTimeZones mCountryTimeZones;
35    private final List<String> mTimeZoneIds;
36
37    public FilteredCountryTimeZones(CountryTimeZones countryTimeZones) {
38        mCountryTimeZones = countryTimeZones;
39        List<String> timeZoneIds = countryTimeZones.getTimeZoneMappings().stream()
40                .filter(timeZoneMapping ->
41                        timeZoneMapping.showInPicker && (timeZoneMapping.notUsedAfter == null
42                                || timeZoneMapping.notUsedAfter >= MIN_USE_DATE_OF_TIMEZONE))
43                .map(timeZoneMapping -> timeZoneMapping.timeZoneId)
44                .collect(Collectors.toList());
45        mTimeZoneIds = Collections.unmodifiableList(timeZoneIds);
46    }
47
48    public List<String> getTimeZoneIds() {
49        return mTimeZoneIds;
50    }
51
52    public CountryTimeZones getCountryTimeZones() {
53        return mCountryTimeZones;
54    }
55
56    public String getRegionId() {
57        return TimeZoneData.normalizeRegionId(mCountryTimeZones.getCountryIso());
58    }
59}
60