• 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.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.Bundle;
25 import android.os.SystemProperties;
26 import android.provider.Settings;
27 import android.support.v14.preference.SwitchPreference;
28 import android.support.v17.preference.LeanbackPreferenceFragment;
29 import android.support.v7.preference.ListPreference;
30 import android.support.v7.preference.Preference;
31 import android.text.TextUtils;
32 import android.text.format.DateFormat;
33 
34 import com.android.settingslib.datetime.ZoneGetter;
35 import com.android.tv.settings.R;
36 
37 import java.util.Calendar;
38 import java.util.Date;
39 
40 public class DateTimeFragment extends LeanbackPreferenceFragment implements
41         Preference.OnPreferenceChangeListener {
42 
43     private static final String KEY_AUTO_DATE_TIME = "auto_date_time";
44     private static final String KEY_SET_DATE = "set_date";
45     private static final String KEY_SET_TIME = "set_time";
46     private static final String KEY_SET_TIME_ZONE = "set_time_zone";
47     private static final String KEY_USE_24_HOUR = "use_24_hour";
48 
49     private static final String AUTO_DATE_TIME_NTP = "network";
50     private static final String AUTO_DATE_TIME_TS = "transport_stream";
51     private static final String AUTO_DATE_TIME_OFF = "off";
52 
53     private static final String HOURS_12 = "12";
54     private static final String HOURS_24 = "24";
55 
56     //    private TvInputManager mTvInputManager;
57     private final Calendar mDummyDate = Calendar.getInstance();
58 
59     private Preference mDatePref;
60     private Preference mTimePref;
61     private Preference mTimeZone;
62     private Preference mTime24Pref;
63 
64     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
65         @Override
66         public void onReceive(Context context, Intent intent) {
67             final Activity activity = getActivity();
68             if (activity != null) {
69                 updateTimeAndDateDisplay(activity);
70             }
71         }
72     };
73 
newInstance()74     public static DateTimeFragment newInstance() {
75         return new DateTimeFragment();
76     }
77 
78     @Override
onCreate(Bundle savedInstanceState)79     public void onCreate(Bundle savedInstanceState) {
80 //        mTvInputManager =
81 //                (TvInputManager) getActivity().getSystemService(Context.TV_INPUT_SERVICE);
82         super.onCreate(savedInstanceState);
83     }
84 
85     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)86     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
87         setPreferencesFromResource(R.xml.date_time, null);
88 
89         final boolean isRestricted = SecurityFragment.isRestrictedProfileInEffect(getContext());
90 
91         mDatePref = findPreference(KEY_SET_DATE);
92         mDatePref.setVisible(!isRestricted);
93         mTimePref = findPreference(KEY_SET_TIME);
94         mTimePref.setVisible(!isRestricted);
95 
96         final boolean tsTimeCapable = SystemProperties.getBoolean("ro.config.ts.date.time", false);
97         final ListPreference autoDateTimePref =
98                 (ListPreference) findPreference(KEY_AUTO_DATE_TIME);
99         autoDateTimePref.setValue(getAutoDateTimeState());
100         autoDateTimePref.setOnPreferenceChangeListener(this);
101         if (tsTimeCapable) {
102             autoDateTimePref.setEntries(R.array.auto_date_time_ts_entries);
103             autoDateTimePref.setEntryValues(R.array.auto_date_time_ts_entry_values);
104         }
105         autoDateTimePref.setVisible(!isRestricted);
106         mTimeZone = findPreference(KEY_SET_TIME_ZONE);
107         mTimeZone.setVisible(!isRestricted);
108         mTime24Pref = findPreference(KEY_USE_24_HOUR);
109         mTime24Pref.setOnPreferenceChangeListener(this);
110     }
111 
112     @Override
onResume()113     public void onResume() {
114         super.onResume();
115 
116         ((SwitchPreference)mTime24Pref).setChecked(is24Hour());
117 
118         // Register for time ticks and other reasons for time change
119         IntentFilter filter = new IntentFilter();
120         filter.addAction(Intent.ACTION_TIME_TICK);
121         filter.addAction(Intent.ACTION_TIME_CHANGED);
122         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
123         getActivity().registerReceiver(mIntentReceiver, filter, null, null);
124 
125         updateTimeAndDateDisplay(getActivity());
126         updateTimeDateEnable();
127     }
128 
129     @Override
onPause()130     public void onPause() {
131         super.onPause();
132         getActivity().unregisterReceiver(mIntentReceiver);
133     }
134 
updateTimeAndDateDisplay(Context context)135     private void updateTimeAndDateDisplay(Context context) {
136         final Calendar now = Calendar.getInstance();
137         mDummyDate.setTimeZone(now.getTimeZone());
138         // We use December 31st because it's unambiguous when demonstrating the date format.
139         // We use 13:00 so we can demonstrate the 12/24 hour options.
140         mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0);
141         Date dummyDate = mDummyDate.getTime();
142         mDatePref.setSummary(DateFormat.getLongDateFormat(context).format(now.getTime()));
143         mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime()));
144         mTimeZone.setSummary(ZoneGetter.getTimeZoneOffsetAndName(getActivity(),
145                 now.getTimeZone(), now.getTime()));
146         mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate));
147     }
148 
updateTimeDateEnable()149     private void updateTimeDateEnable() {
150         final boolean enable = TextUtils.equals(getAutoDateTimeState(), AUTO_DATE_TIME_OFF);
151         mDatePref.setEnabled(enable);
152         mTimePref.setEnabled(enable);
153     }
154 
155     @Override
onPreferenceChange(Preference preference, Object newValue)156     public boolean onPreferenceChange(Preference preference, Object newValue) {
157         if (TextUtils.equals(preference.getKey(), KEY_AUTO_DATE_TIME)) {
158             String value = (String) newValue;
159             if (TextUtils.equals(value, AUTO_DATE_TIME_NTP)) {
160                 setAutoDateTime(true);
161             } else if (TextUtils.equals(value, AUTO_DATE_TIME_TS)) {
162                 throw new IllegalStateException("TS date is not yet implemented");
163 //                mTvInputManager.syncTimefromBroadcast(true);
164 //                setAutoDateTime(false);
165             } else if (TextUtils.equals(value, AUTO_DATE_TIME_OFF)) {
166                 setAutoDateTime(false);
167             } else {
168                 throw new IllegalArgumentException("Unknown auto time value " + value);
169             }
170             updateTimeDateEnable();
171         } else if (TextUtils.equals(preference.getKey(), KEY_USE_24_HOUR)) {
172             final boolean use24Hour = (Boolean) newValue;
173             set24Hour(use24Hour);
174             timeUpdated(use24Hour);
175         }
176         return true;
177     }
178 
179     /*  Get & Set values from the system settings  */
180 
is24Hour()181     private boolean is24Hour() {
182         return DateFormat.is24HourFormat(getActivity());
183     }
184 
timeUpdated(boolean use24Hour)185     private void timeUpdated(boolean use24Hour) {
186         Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
187         int timeFormatPreference =
188                 use24Hour ? Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR
189                         : Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR;
190         timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, timeFormatPreference);
191         getContext().sendBroadcast(timeChanged);
192     }
193 
set24Hour(boolean use24Hour)194     private void set24Hour(boolean use24Hour) {
195         Settings.System.putString(getContext().getContentResolver(),
196                 Settings.System.TIME_12_24,
197                 use24Hour ? HOURS_24 : HOURS_12);
198     }
199 
setAutoDateTime(boolean on)200     private void setAutoDateTime(boolean on) {
201         Settings.Global.putInt(getContext().getContentResolver(),
202                 Settings.Global.AUTO_TIME, on ? 1 : 0);
203     }
204 
getAutoDateTimeState()205     private String getAutoDateTimeState() {
206 //        if(mTvInputManager.isUseBroadcastDateTime()) {
207 //            return AUTO_DATE_TIME_TS;
208 //        }
209 
210         int value = Settings.Global.getInt(getContext().getContentResolver(),
211                 Settings.Global.AUTO_TIME, 0);
212         if(value > 0) {
213             return AUTO_DATE_TIME_NTP;
214         }
215 
216         return AUTO_DATE_TIME_OFF;
217     }
218 
219 }
220