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.model;
18 
19 import android.util.ArraySet;
20 import android.util.TimeUtils;
21 
22 import com.android.i18n.timezone.CountryTimeZones;
23 
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Set;
28 
29 /**
30  * Wrap {@class CountryTimeZones} to filter time zone that are shown in the picker.
31  */
32 public class FilteredCountryTimeZones {
33 
34     private final CountryTimeZones mCountryTimeZones;
35     private final List<String> mPreferredTimeZoneIds;
36     private final Set<String> mAlternativeTimeZoneIds;
37 
FilteredCountryTimeZones(CountryTimeZones countryTimeZones)38     public FilteredCountryTimeZones(CountryTimeZones countryTimeZones) {
39         mCountryTimeZones = countryTimeZones;
40         List<String> timeZoneIds = new ArrayList<>();
41         Set<String> alternativeTimeZoneIds = new ArraySet<>();
42         for (CountryTimeZones.TimeZoneMapping timeZoneMapping :
43                 countryTimeZones.getTimeZoneMappings()) {
44             if (timeZoneMapping.isShownInPickerAt(TimeUtils.MIN_USE_DATE_OF_TIMEZONE)) {
45                 String timeZoneId = timeZoneMapping.getTimeZoneId();
46                 timeZoneIds.add(timeZoneId);
47                 alternativeTimeZoneIds.addAll(timeZoneMapping.getAlternativeIds());
48             }
49         }
50         mPreferredTimeZoneIds = Collections.unmodifiableList(timeZoneIds);
51         mAlternativeTimeZoneIds = Collections.unmodifiableSet(alternativeTimeZoneIds);
52     }
53 
getPreferredTimeZoneIds()54     public List<String> getPreferredTimeZoneIds() {
55         return mPreferredTimeZoneIds;
56     }
57 
getCountryTimeZones()58     public CountryTimeZones getCountryTimeZones() {
59         return mCountryTimeZones;
60     }
61 
62     /**
63      * Returns whether {@code timeZoneId} is currently used in the country or is an alternative
64      * name of a currently used time zone.
65      */
matches(String timeZoneId)66     public boolean matches(String timeZoneId) {
67         return mPreferredTimeZoneIds.contains(timeZoneId)
68                 || mAlternativeTimeZoneIds.contains(timeZoneId);
69     }
70 
getRegionId()71     public String getRegionId() {
72         return TimeZoneData.normalizeRegionId(mCountryTimeZones.getCountryIso());
73     }
74 }
75