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.tv.settings.system;
18 
19 import android.app.Activity;
20 import android.app.timezonedetector.ManualTimeZoneSuggestion;
21 import android.app.timezonedetector.TimeZoneDetector;
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.os.Bundle;
27 import android.text.TextUtils;
28 
29 import androidx.annotation.Keep;
30 import androidx.preference.CheckBoxPreference;
31 import androidx.preference.Preference;
32 import androidx.preference.PreferenceScreen;
33 
34 import com.android.internal.logging.nano.MetricsProto;
35 import com.android.settingslib.datetime.ZoneGetter;
36 import com.android.tv.settings.R;
37 import com.android.tv.settings.SettingsPreferenceFragment;
38 
39 import java.util.ArrayList;
40 import java.util.Comparator;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.TimeZone;
44 
45 @Keep
46 public class TimeZoneFragment extends SettingsPreferenceFragment {
47 
48     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
49         @Override
50         public void onReceive(Context context, Intent intent) {
51             final Activity activity = getActivity();
52             if (activity != null) {
53                 updateZones();
54             }
55         }
56     };
57 
58     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)59     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
60         final Context themedContext = getPreferenceManager().getContext();
61         final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(
62                 themedContext);
63         screen.setTitle(R.string.system_set_time_zone);
64         setPreferenceScreen(screen);
65 
66         final List<Map<String, Object>> zoneList = ZoneGetter.getZonesList(getActivity());
67         final List<ZonePreference> zonePrefs = new ArrayList<>(zoneList.size());
68         for (final Map<String, Object> zone : zoneList) {
69             zonePrefs.add(new ZonePreference(themedContext, zone));
70         }
71         zonePrefs.sort(new ZonePrefComparator());
72         for (final Preference zonePref : zonePrefs) {
73             screen.addPreference(zonePref);
74         }
75     }
76 
77     @Override
onResume()78     public void onResume() {
79         super.onResume();
80         // Register for zone changes
81         IntentFilter filter = new IntentFilter();
82         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
83         getActivity().registerReceiver(mIntentReceiver, filter, null, null);
84         updateZones();
85     }
86 
87     @Override
onPause()88     public void onPause() {
89         super.onPause();
90         getActivity().unregisterReceiver(mIntentReceiver);
91     }
92 
93     @Override
onPreferenceTreeClick(Preference preference)94     public boolean onPreferenceTreeClick(Preference preference) {
95         if (preference instanceof ZonePreference) {
96             // Update the system timezone value
97             final TimeZoneDetector timeZoneDetector =
98                     getActivity().getSystemService(TimeZoneDetector.class);
99             ManualTimeZoneSuggestion suggestion = TimeZoneDetector.createManualTimeZoneSuggestion(
100                     preference.getKey(), "Settings: Set time zone");
101             timeZoneDetector.suggestManualTimeZone(suggestion);
102             if (!getFragmentManager().popBackStackImmediate()) {
103                 getActivity().finish();
104             }
105         }
106         return super.onPreferenceTreeClick(preference);
107     }
108 
updateZones()109     private void updateZones() {
110         final String id = TimeZone.getDefault().getID();
111         final PreferenceScreen screen = getPreferenceScreen();
112         final int count = screen.getPreferenceCount();
113         for (int i = 0; i < count; i++) {
114             final Preference pref = screen.getPreference(i);
115             if (!(pref instanceof ZonePreference)) {
116                 continue;
117             }
118             final ZonePreference zonePref = (ZonePreference) pref;
119             zonePref.setChecked(TextUtils.equals(zonePref.getKey(), id));
120         }
121     }
122 
123     private static class ZonePreference extends CheckBoxPreference {
124         Integer offset;
125 
ZonePreference(Context context, Map<? extends String, ?> zone)126         public ZonePreference(Context context, Map<? extends String, ?> zone) {
127             super(context);
128             setWidgetLayoutResource(R.layout.radio_preference_widget);
129             setKey((String) zone.get(ZoneGetter.KEY_ID));
130             setPersistent(false);
131             setTitle((String) zone.get(ZoneGetter.KEY_DISPLAYNAME));
132             setSummary((String) zone.get(ZoneGetter.KEY_GMT));
133             offset = (Integer) zone.get(ZoneGetter.KEY_OFFSET);
134         }
135     }
136 
137     private static class ZonePrefComparator implements Comparator<ZonePreference> {
compare(ZonePreference zone1, ZonePreference zone2)138         public int compare(ZonePreference zone1, ZonePreference zone2) {
139             final int firstResult = zone1.offset.compareTo(zone2.offset);
140             if (firstResult != 0) {
141                 return firstResult;
142             }
143             return zone1.getTitle().toString().compareTo(zone2.getTitle().toString());
144         }
145     }
146 
147     @Override
getMetricsCategory()148     public int getMetricsCategory() {
149         return MetricsProto.MetricsEvent.ZONE_PICKER;
150     }
151 }
152