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