The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.settings; |
| 18 | |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 21 | import android.app.DatePickerDialog; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 22 | import android.app.Dialog; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 23 | import android.app.TimePickerDialog; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.SharedPreferences; |
| 29 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
| 30 | import android.os.Bundle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 31 | import android.preference.CheckBoxPreference; |
| 32 | import android.preference.ListPreference; |
| 33 | import android.preference.Preference; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | import android.preference.PreferenceScreen; |
| 35 | import android.provider.Settings; |
| 36 | import android.provider.Settings.SettingNotFoundException; |
Elliott Hughes | 0448759 | 2013-09-18 15:04:25 -0700 | [diff] [blame] | 37 | import android.text.BidiFormatter; |
| 38 | import android.text.TextDirectionHeuristics; |
| 39 | import android.text.TextUtils; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 40 | import android.text.format.DateFormat; |
Elliott Hughes | 0448759 | 2013-09-18 15:04:25 -0700 | [diff] [blame] | 41 | import android.view.View; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 42 | import android.widget.DatePicker; |
| 43 | import android.widget.TimePicker; |
Elliott Hughes | 81faf89 | 2013-06-25 10:16:27 -0700 | [diff] [blame] | 44 | import java.text.SimpleDateFormat; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 45 | import java.util.Calendar; |
| 46 | import java.util.Date; |
Elliott Hughes | 0448759 | 2013-09-18 15:04:25 -0700 | [diff] [blame] | 47 | import java.util.Locale; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | import java.util.TimeZone; |
| 49 | |
Daisuke Miyakawa | 0f4f2f3 | 2010-09-03 15:40:17 -0700 | [diff] [blame] | 50 | public class DateTimeSettings extends SettingsPreferenceFragment |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 51 | implements OnSharedPreferenceChangeListener, |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 52 | TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 53 | |
| 54 | private static final String HOURS_12 = "12"; |
| 55 | private static final String HOURS_24 = "24"; |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 56 | |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 57 | // Used for showing the current date format, which looks like "12/31/2010", "2010/12/13", etc. |
| 58 | // The date value is dummy (independent of actual date). |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 59 | private Calendar mDummyDate; |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 60 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 61 | private static final String KEY_DATE_FORMAT = "date_format"; |
| 62 | private static final String KEY_AUTO_TIME = "auto_time"; |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 63 | private static final String KEY_AUTO_TIME_ZONE = "auto_zone"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 64 | |
| 65 | private static final int DIALOG_DATEPICKER = 0; |
| 66 | private static final int DIALOG_TIMEPICKER = 1; |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 67 | |
Freeman Ng | 7f6f6e1 | 2011-06-02 15:44:51 -0700 | [diff] [blame] | 68 | // have we been launched from the setup wizard? |
| 69 | protected static final String EXTRA_IS_FIRST_RUN = "firstRun"; |
| 70 | |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 71 | private CheckBoxPreference mAutoTimePref; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 72 | private Preference mTimePref; |
| 73 | private Preference mTime24Pref; |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 74 | private CheckBoxPreference mAutoTimeZonePref; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 75 | private Preference mTimeZone; |
| 76 | private Preference mDatePref; |
| 77 | private ListPreference mDateFormat; |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 78 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 79 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 80 | public void onCreate(Bundle icicle) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | super.onCreate(icicle); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 82 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 83 | addPreferencesFromResource(R.xml.date_time_prefs); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 84 | |
| 85 | initUI(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 86 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 88 | private void initUI() { |
Christopher Tate | f1c0858 | 2012-09-12 14:25:46 -0700 | [diff] [blame] | 89 | boolean autoTimeEnabled = getAutoState(Settings.Global.AUTO_TIME); |
| 90 | boolean autoTimeZoneEnabled = getAutoState(Settings.Global.AUTO_TIME_ZONE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 91 | |
Freeman Ng | 7f6f6e1 | 2011-06-02 15:44:51 -0700 | [diff] [blame] | 92 | Intent intent = getActivity().getIntent(); |
| 93 | boolean isFirstRun = intent.getBooleanExtra(EXTRA_IS_FIRST_RUN, false); |
| 94 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 95 | mDummyDate = Calendar.getInstance(); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 96 | |
| 97 | mAutoTimePref = (CheckBoxPreference) findPreference(KEY_AUTO_TIME); |
| 98 | mAutoTimePref.setChecked(autoTimeEnabled); |
Amith Yamasani | ea07165 | 2010-11-08 14:28:05 -0800 | [diff] [blame] | 99 | mAutoTimeZonePref = (CheckBoxPreference) findPreference(KEY_AUTO_TIME_ZONE); |
Freeman Ng | 7f6f6e1 | 2011-06-02 15:44:51 -0700 | [diff] [blame] | 100 | // Override auto-timezone if it's a wifi-only device or if we're still in setup wizard. |
| 101 | // TODO: Remove the wifiOnly test when auto-timezone is implemented based on wifi-location. |
Robert Greenwalt | 8af88fb | 2011-08-31 11:17:47 -0700 | [diff] [blame] | 102 | if (Utils.isWifiOnly(getActivity()) || isFirstRun) { |
Amith Yamasani | c06d4c4 | 2011-02-25 14:35:20 -0800 | [diff] [blame] | 103 | getPreferenceScreen().removePreference(mAutoTimeZonePref); |
| 104 | autoTimeZoneEnabled = false; |
| 105 | } |
Amith Yamasani | ea07165 | 2010-11-08 14:28:05 -0800 | [diff] [blame] | 106 | mAutoTimeZonePref.setChecked(autoTimeZoneEnabled); |
| 107 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 108 | mTimePref = findPreference("time"); |
| 109 | mTime24Pref = findPreference("24 hour"); |
| 110 | mTimeZone = findPreference("timezone"); |
| 111 | mDatePref = findPreference("date"); |
| 112 | mDateFormat = (ListPreference) findPreference(KEY_DATE_FORMAT); |
Freeman Ng | 7f6f6e1 | 2011-06-02 15:44:51 -0700 | [diff] [blame] | 113 | if (isFirstRun) { |
| 114 | getPreferenceScreen().removePreference(mTime24Pref); |
| 115 | getPreferenceScreen().removePreference(mDateFormat); |
| 116 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 118 | String [] dateFormats = getResources().getStringArray(R.array.date_format_values); |
| 119 | String [] formattedDates = new String[dateFormats.length]; |
| 120 | String currentFormat = getDateFormat(); |
| 121 | // Initialize if DATE_FORMAT is not set in the system settings |
| 122 | // This can happen after a factory reset (or data wipe) |
| 123 | if (currentFormat == null) { |
Eric Fischer | 188ca77 | 2009-06-11 18:16:15 -0700 | [diff] [blame] | 124 | currentFormat = ""; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 125 | } |
Fábio Silva | b6676bb | 2012-10-15 12:21:19 -0300 | [diff] [blame] | 126 | |
| 127 | // Prevents duplicated values on date format selector. |
| 128 | mDummyDate.set(mDummyDate.get(Calendar.YEAR), mDummyDate.DECEMBER, 31, 13, 0, 0); |
| 129 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 130 | for (int i = 0; i < formattedDates.length; i++) { |
Eric Fischer | 188ca77 | 2009-06-11 18:16:15 -0700 | [diff] [blame] | 131 | String formatted = |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 132 | DateFormat.getDateFormatForSetting(getActivity(), dateFormats[i]) |
| 133 | .format(mDummyDate.getTime()); |
Eric Fischer | 188ca77 | 2009-06-11 18:16:15 -0700 | [diff] [blame] | 134 | |
| 135 | if (dateFormats[i].length() == 0) { |
| 136 | formattedDates[i] = getResources(). |
| 137 | getString(R.string.normal_date_format, formatted); |
| 138 | } else { |
| 139 | formattedDates[i] = formatted; |
| 140 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 141 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 142 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 143 | mDateFormat.setEntries(formattedDates); |
| 144 | mDateFormat.setEntryValues(R.array.date_format_values); |
| 145 | mDateFormat.setValue(currentFormat); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 146 | |
| 147 | mTimePref.setEnabled(!autoTimeEnabled); |
| 148 | mDatePref.setEnabled(!autoTimeEnabled); |
| 149 | mTimeZone.setEnabled(!autoTimeZoneEnabled); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 150 | } |
| 151 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 152 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 153 | public void onResume() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 154 | super.onResume(); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 155 | |
| 156 | getPreferenceScreen().getSharedPreferences() |
| 157 | .registerOnSharedPreferenceChangeListener(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 158 | |
| 159 | ((CheckBoxPreference)mTime24Pref).setChecked(is24Hour()); |
| 160 | |
| 161 | // Register for time ticks and other reasons for time change |
| 162 | IntentFilter filter = new IntentFilter(); |
| 163 | filter.addAction(Intent.ACTION_TIME_TICK); |
| 164 | filter.addAction(Intent.ACTION_TIME_CHANGED); |
| 165 | filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 166 | getActivity().registerReceiver(mIntentReceiver, filter, null, null); |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 167 | |
| 168 | updateTimeAndDateDisplay(getActivity()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 171 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 172 | public void onPause() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 173 | super.onPause(); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 174 | getActivity().unregisterReceiver(mIntentReceiver); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 175 | getPreferenceScreen().getSharedPreferences() |
| 176 | .unregisterOnSharedPreferenceChangeListener(this); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 177 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 178 | |
Freeman Ng | 7f6f6e1 | 2011-06-02 15:44:51 -0700 | [diff] [blame] | 179 | public void updateTimeAndDateDisplay(Context context) { |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 180 | java.text.DateFormat shortDateFormat = DateFormat.getDateFormat(context); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 181 | final Calendar now = Calendar.getInstance(); |
Kenny Root | 151b0e1 | 2011-06-12 19:44:14 -0700 | [diff] [blame] | 182 | mDummyDate.setTimeZone(now.getTimeZone()); |
Elliott Hughes | 82723df | 2012-09-06 14:49:35 -0700 | [diff] [blame] | 183 | // We use December 31st because it's unambiguous when demonstrating the date format. |
| 184 | // We use 13:00 so we can demonstrate the 12/24 hour options. |
Kenny Root | 151b0e1 | 2011-06-12 19:44:14 -0700 | [diff] [blame] | 185 | mDummyDate.set(now.get(Calendar.YEAR), 11, 31, 13, 0, 0); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 186 | Date dummyDate = mDummyDate.getTime(); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 187 | mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime())); |
Elliott Hughes | 0448759 | 2013-09-18 15:04:25 -0700 | [diff] [blame] | 188 | mTimeZone.setSummary(getTimeZoneText(now.getTimeZone(), true)); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 189 | mDatePref.setSummary(shortDateFormat.format(now.getTime())); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 190 | mDateFormat.setSummary(shortDateFormat.format(dummyDate)); |
Elliott Hughes | 82723df | 2012-09-06 14:49:35 -0700 | [diff] [blame] | 191 | mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Daisuke Miyakawa | 0f4f2f3 | 2010-09-03 15:40:17 -0700 | [diff] [blame] | 194 | @Override |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 195 | public void onDateSet(DatePicker view, int year, int month, int day) { |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 196 | final Activity activity = getActivity(); |
| 197 | if (activity != null) { |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 198 | setDate(activity, year, month, day); |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 199 | updateTimeAndDateDisplay(activity); |
| 200 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Daisuke Miyakawa | 0f4f2f3 | 2010-09-03 15:40:17 -0700 | [diff] [blame] | 203 | @Override |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 204 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) { |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 205 | final Activity activity = getActivity(); |
| 206 | if (activity != null) { |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 207 | setTime(activity, hourOfDay, minute); |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 208 | updateTimeAndDateDisplay(activity); |
| 209 | } |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 210 | |
The Android Open Source Project | 72ed6fe | 2009-03-13 13:04:25 -0700 | [diff] [blame] | 211 | // We don't need to call timeUpdated() here because the TIME_CHANGED |
| 212 | // broadcast is sent by the AlarmManager as a side effect of setting the |
| 213 | // SystemClock time. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Daisuke Miyakawa | 0f4f2f3 | 2010-09-03 15:40:17 -0700 | [diff] [blame] | 216 | @Override |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 217 | public void onSharedPreferenceChanged(SharedPreferences preferences, String key) { |
| 218 | if (key.equals(KEY_DATE_FORMAT)) { |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 219 | String format = preferences.getString(key, |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 220 | getResources().getString(R.string.default_date_format)); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 221 | Settings.System.putString(getContentResolver(), |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 222 | Settings.System.DATE_FORMAT, format); |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 223 | updateTimeAndDateDisplay(getActivity()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 224 | } else if (key.equals(KEY_AUTO_TIME)) { |
| 225 | boolean autoEnabled = preferences.getBoolean(key, true); |
Christopher Tate | f1c0858 | 2012-09-12 14:25:46 -0700 | [diff] [blame] | 226 | Settings.Global.putInt(getContentResolver(), Settings.Global.AUTO_TIME, |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 227 | autoEnabled ? 1 : 0); |
| 228 | mTimePref.setEnabled(!autoEnabled); |
| 229 | mDatePref.setEnabled(!autoEnabled); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 230 | } else if (key.equals(KEY_AUTO_TIME_ZONE)) { |
| 231 | boolean autoZoneEnabled = preferences.getBoolean(key, true); |
Christopher Tate | f1c0858 | 2012-09-12 14:25:46 -0700 | [diff] [blame] | 232 | Settings.Global.putInt( |
| 233 | getContentResolver(), Settings.Global.AUTO_TIME_ZONE, autoZoneEnabled ? 1 : 0); |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 234 | mTimeZone.setEnabled(!autoZoneEnabled); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public Dialog onCreateDialog(int id) { |
Elliott Hughes | b85d48f | 2013-06-24 16:11:19 -0700 | [diff] [blame] | 240 | final Calendar calendar = Calendar.getInstance(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 241 | switch (id) { |
Elliott Hughes | b85d48f | 2013-06-24 16:11:19 -0700 | [diff] [blame] | 242 | case DIALOG_DATEPICKER: |
| 243 | DatePickerDialog d = new DatePickerDialog( |
| 244 | getActivity(), |
| 245 | this, |
| 246 | calendar.get(Calendar.YEAR), |
| 247 | calendar.get(Calendar.MONTH), |
| 248 | calendar.get(Calendar.DAY_OF_MONTH)); |
| 249 | configureDatePicker(d.getDatePicker()); |
| 250 | return d; |
| 251 | case DIALOG_TIMEPICKER: |
| 252 | return new TimePickerDialog( |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 253 | getActivity(), |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 254 | this, |
| 255 | calendar.get(Calendar.HOUR_OF_DAY), |
| 256 | calendar.get(Calendar.MINUTE), |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 257 | DateFormat.is24HourFormat(getActivity())); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 258 | default: |
Elliott Hughes | b85d48f | 2013-06-24 16:11:19 -0700 | [diff] [blame] | 259 | throw new IllegalArgumentException(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 260 | } |
Elliott Hughes | b85d48f | 2013-06-24 16:11:19 -0700 | [diff] [blame] | 261 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 262 | |
Elliott Hughes | b85d48f | 2013-06-24 16:11:19 -0700 | [diff] [blame] | 263 | static void configureDatePicker(DatePicker datePicker) { |
| 264 | // The system clock can't represent dates outside this range. |
| 265 | Calendar t = Calendar.getInstance(); |
| 266 | t.clear(); |
| 267 | t.set(1970, Calendar.JANUARY, 1); |
| 268 | datePicker.setMinDate(t.getTimeInMillis()); |
| 269 | t.clear(); |
| 270 | t.set(2037, Calendar.DECEMBER, 31); |
| 271 | datePicker.setMaxDate(t.getTimeInMillis()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 274 | /* |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 275 | @Override |
| 276 | public void onPrepareDialog(int id, Dialog d) { |
| 277 | switch (id) { |
| 278 | case DIALOG_DATEPICKER: { |
| 279 | DatePickerDialog datePicker = (DatePickerDialog)d; |
| 280 | final Calendar calendar = Calendar.getInstance(); |
| 281 | datePicker.updateDate( |
| 282 | calendar.get(Calendar.YEAR), |
| 283 | calendar.get(Calendar.MONTH), |
| 284 | calendar.get(Calendar.DAY_OF_MONTH)); |
| 285 | break; |
| 286 | } |
| 287 | case DIALOG_TIMEPICKER: { |
| 288 | TimePickerDialog timePicker = (TimePickerDialog)d; |
| 289 | final Calendar calendar = Calendar.getInstance(); |
| 290 | timePicker.updateTime( |
| 291 | calendar.get(Calendar.HOUR_OF_DAY), |
| 292 | calendar.get(Calendar.MINUTE)); |
| 293 | break; |
| 294 | } |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | } |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 299 | */ |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 300 | @Override |
| 301 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
| 302 | if (preference == mDatePref) { |
| 303 | showDialog(DIALOG_DATEPICKER); |
| 304 | } else if (preference == mTimePref) { |
| 305 | // The 24-hour mode may have changed, so recreate the dialog |
| 306 | removeDialog(DIALOG_TIMEPICKER); |
| 307 | showDialog(DIALOG_TIMEPICKER); |
| 308 | } else if (preference == mTime24Pref) { |
Narayan Kamath | 0a2bc1c | 2014-03-11 13:05:37 +0000 | [diff] [blame] | 309 | final boolean is24Hour = ((CheckBoxPreference)mTime24Pref).isChecked(); |
| 310 | set24Hour(is24Hour); |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 311 | updateTimeAndDateDisplay(getActivity()); |
Narayan Kamath | 0a2bc1c | 2014-03-11 13:05:37 +0000 | [diff] [blame] | 312 | timeUpdated(is24Hour); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 313 | } |
Daisuke Miyakawa | 0f4f2f3 | 2010-09-03 15:40:17 -0700 | [diff] [blame] | 314 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 315 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 316 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 317 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 318 | public void onActivityResult(int requestCode, int resultCode, |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 319 | Intent data) { |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 320 | updateTimeAndDateDisplay(getActivity()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 321 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 322 | |
Narayan Kamath | 0a2bc1c | 2014-03-11 13:05:37 +0000 | [diff] [blame] | 323 | private void timeUpdated(boolean is24Hour) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 324 | Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED); |
Narayan Kamath | 0a2bc1c | 2014-03-11 13:05:37 +0000 | [diff] [blame] | 325 | timeChanged.putExtra(Intent.EXTRA_TIME_PREF_24_HOUR_FORMAT, is24Hour); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 326 | getActivity().sendBroadcast(timeChanged); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 327 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 328 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 329 | /* Get & Set values from the system settings */ |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 330 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 331 | private boolean is24Hour() { |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 332 | return DateFormat.is24HourFormat(getActivity()); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 333 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 334 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 335 | private void set24Hour(boolean is24Hour) { |
| 336 | Settings.System.putString(getContentResolver(), |
| 337 | Settings.System.TIME_12_24, |
| 338 | is24Hour? HOURS_24 : HOURS_12); |
| 339 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 340 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 341 | private String getDateFormat() { |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 342 | return Settings.System.getString(getContentResolver(), |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 343 | Settings.System.DATE_FORMAT); |
| 344 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 345 | |
| 346 | private boolean getAutoState(String name) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 347 | try { |
Christopher Tate | f1c0858 | 2012-09-12 14:25:46 -0700 | [diff] [blame] | 348 | return Settings.Global.getInt(getContentResolver(), name) > 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 349 | } catch (SettingNotFoundException snfe) { |
Amith Yamasani | ea07165 | 2010-11-08 14:28:05 -0800 | [diff] [blame] | 350 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 354 | /* package */ static void setDate(Context context, int year, int month, int day) { |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 355 | Calendar c = Calendar.getInstance(); |
| 356 | |
| 357 | c.set(Calendar.YEAR, year); |
| 358 | c.set(Calendar.MONTH, month); |
| 359 | c.set(Calendar.DAY_OF_MONTH, day); |
| 360 | long when = c.getTimeInMillis(); |
| 361 | |
| 362 | if (when / 1000 < Integer.MAX_VALUE) { |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 363 | ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 367 | /* package */ static void setTime(Context context, int hourOfDay, int minute) { |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 368 | Calendar c = Calendar.getInstance(); |
| 369 | |
| 370 | c.set(Calendar.HOUR_OF_DAY, hourOfDay); |
| 371 | c.set(Calendar.MINUTE, minute); |
Gilles Debunne | 33ff115 | 2011-07-28 14:16:51 -0700 | [diff] [blame] | 372 | c.set(Calendar.SECOND, 0); |
| 373 | c.set(Calendar.MILLISECOND, 0); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 374 | long when = c.getTimeInMillis(); |
| 375 | |
| 376 | if (when / 1000 < Integer.MAX_VALUE) { |
Amith Yamasani | 5f05010 | 2012-11-01 15:36:29 -0700 | [diff] [blame] | 377 | ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when); |
Daisuke Miyakawa | 71cc548 | 2010-09-09 11:51:16 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Elliott Hughes | 0448759 | 2013-09-18 15:04:25 -0700 | [diff] [blame] | 381 | public static String getTimeZoneText(TimeZone tz, boolean includeName) { |
| 382 | Date now = new Date(); |
| 383 | |
| 384 | // Use SimpleDateFormat to format the GMT+00:00 string. |
| 385 | SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ"); |
| 386 | gmtFormatter.setTimeZone(tz); |
| 387 | String gmtString = gmtFormatter.format(now); |
| 388 | |
| 389 | // Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL. |
| 390 | BidiFormatter bidiFormatter = BidiFormatter.getInstance(); |
| 391 | Locale l = Locale.getDefault(); |
| 392 | boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL; |
| 393 | gmtString = bidiFormatter.unicodeWrap(gmtString, |
| 394 | isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR); |
| 395 | |
| 396 | if (!includeName) { |
| 397 | return gmtString; |
| 398 | } |
| 399 | |
| 400 | // Optionally append the time zone name. |
| 401 | SimpleDateFormat zoneNameFormatter = new SimpleDateFormat("zzzz"); |
| 402 | zoneNameFormatter.setTimeZone(tz); |
| 403 | String zoneNameString = zoneNameFormatter.format(now); |
| 404 | |
| 405 | // We don't use punctuation here to avoid having to worry about localizing that too! |
| 406 | return gmtString + " " + zoneNameString; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 407 | } |
Amith Yamasani | 1bb6db5 | 2010-09-17 13:34:47 -0700 | [diff] [blame] | 408 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 409 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 410 | @Override |
| 411 | public void onReceive(Context context, Intent intent) { |
Daisuke Miyakawa | 29e812f | 2010-09-13 16:50:57 -0700 | [diff] [blame] | 412 | final Activity activity = getActivity(); |
| 413 | if (activity != null) { |
| 414 | updateTimeAndDateDisplay(activity); |
| 415 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 416 | } |
| 417 | }; |
| 418 | } |