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 mDatePref = findPreference(KEY_SET_DATE); 90 mDatePref.setIntent(SetDateTimeActivity.getSetDateIntent(getActivity())); 91 mTimePref = findPreference(KEY_SET_TIME); 92 mTimePref.setIntent(SetDateTimeActivity.getSetTimeIntent(getActivity())); 93 94 final boolean tsTimeCapable = SystemProperties.getBoolean("ro.config.ts.date.time", false); 95 final ListPreference autoDateTimePref = 96 (ListPreference) findPreference(KEY_AUTO_DATE_TIME); 97 autoDateTimePref.setValue(getAutoDateTimeState()); 98 autoDateTimePref.setOnPreferenceChangeListener(this); 99 if (tsTimeCapable) { 100 autoDateTimePref.setEntries(R.array.auto_date_time_ts_entries); 101 autoDateTimePref.setEntryValues(R.array.auto_date_time_ts_entry_values); 102 } 103 mTimeZone = findPreference(KEY_SET_TIME_ZONE); 104 mTime24Pref = findPreference(KEY_USE_24_HOUR); 105 mTime24Pref.setOnPreferenceChangeListener(this); 106 } 107 108 @Override onResume()109 public void onResume() { 110 super.onResume(); 111 112 ((SwitchPreference)mTime24Pref).setChecked(is24Hour()); 113 114 // Register for time ticks and other reasons for time change 115 IntentFilter filter = new IntentFilter(); 116 filter.addAction(Intent.ACTION_TIME_TICK); 117 filter.addAction(Intent.ACTION_TIME_CHANGED); 118 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 119 getActivity().registerReceiver(mIntentReceiver, filter, null, null); 120 121 updateTimeAndDateDisplay(getActivity()); 122 updateTimeDateEnable(); 123 } 124 125 @Override onPause()126 public void onPause() { 127 super.onPause(); 128 getActivity().unregisterReceiver(mIntentReceiver); 129 } 130 updateTimeAndDateDisplay(Context context)131 private void updateTimeAndDateDisplay(Context context) { 132 final Calendar now = Calendar.getInstance(); 133 mDummyDate.setTimeZone(now.getTimeZone()); 134 // We use December 31st because it's unambiguous when demonstrating the date format. 135 // We use 13:00 so we can demonstrate the 12/24 hour options. 136 mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0); 137 Date dummyDate = mDummyDate.getTime(); 138 mDatePref.setSummary(DateFormat.getLongDateFormat(context).format(now.getTime())); 139 mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime())); 140 mTimeZone.setSummary(ZoneGetter.getTimeZoneOffsetAndName(now.getTimeZone(), now.getTime())); 141 mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate)); 142 } 143 updateTimeDateEnable()144 private void updateTimeDateEnable() { 145 final boolean enable = TextUtils.equals(getAutoDateTimeState(), AUTO_DATE_TIME_OFF); 146 mDatePref.setEnabled(enable); 147 mTimePref.setEnabled(enable); 148 } 149 150 @Override onPreferenceChange(Preference preference, Object newValue)151 public boolean onPreferenceChange(Preference preference, Object newValue) { 152 if (TextUtils.equals(preference.getKey(), KEY_AUTO_DATE_TIME)) { 153 String value = (String) newValue; 154 if (TextUtils.equals(value, AUTO_DATE_TIME_NTP)) { 155 setAutoDateTime(true); 156 } else if (TextUtils.equals(value, AUTO_DATE_TIME_TS)) { 157 throw new IllegalStateException("TS date is not yet implemented"); 158 // mTvInputManager.syncTimefromBroadcast(true); 159 // setAutoDateTime(false); 160 } else if (TextUtils.equals(value, AUTO_DATE_TIME_OFF)) { 161 setAutoDateTime(false); 162 } else { 163 throw new IllegalArgumentException("Unknown auto time value " + value); 164 } 165 updateTimeDateEnable(); 166 } else if (TextUtils.equals(preference.getKey(), KEY_USE_24_HOUR)) { 167 set24Hour((Boolean) newValue); 168 updateTimeAndDateDisplay(getActivity()); 169 } 170 return true; 171 } 172 173 /* Get & Set values from the system settings */ 174 is24Hour()175 private boolean is24Hour() { 176 return DateFormat.is24HourFormat(getActivity()); 177 } 178 set24Hour(boolean is24Hour)179 private void set24Hour(boolean is24Hour) { 180 Settings.System.putString(getActivity().getContentResolver(), 181 Settings.System.TIME_12_24, 182 is24Hour? HOURS_24 : HOURS_12); 183 } 184 setAutoDateTime(boolean on)185 private void setAutoDateTime(boolean on) { 186 Settings.Global.putInt(getActivity().getContentResolver(), 187 Settings.Global.AUTO_TIME, on ? 1 : 0); 188 } 189 getAutoDateTimeState()190 private String getAutoDateTimeState() { 191 // if(mTvInputManager.isUseBroadcastDateTime()) { 192 // return AUTO_DATE_TIME_TS; 193 // } 194 195 int value = Settings.Global.getInt(getActivity().getContentResolver(), 196 Settings.Global.AUTO_TIME, 0); 197 if(value > 0) { 198 return AUTO_DATE_TIME_NTP; 199 } 200 201 return AUTO_DATE_TIME_OFF; 202 } 203 204 } 205