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