2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.app.Activity;
20 import android.app.AlarmManager;
21 import android.app.Fragment;
22 import android.app.FragmentTransaction;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.pm.ActivityInfo;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.preference.Preference;
31 import android.preference.PreferenceFragment;
32 import android.provider.Settings;
33 import android.provider.Settings.SettingNotFoundException;
34 import android.text.format.DateFormat;
35 import android.util.Log;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.view.Window;
39 import android.view.inputmethod.InputMethodManager;
40 import android.widget.AdapterView;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.Button;
43 import android.widget.CompoundButton;
44 import android.widget.CompoundButton.OnCheckedChangeListener;
45 import android.widget.DatePicker;
46 import android.widget.LinearLayout;
47 import android.widget.ListPopupWindow;
48 import android.widget.SimpleAdapter;
49 import android.widget.TimePicker;
51 import java.util.Calendar;
52 import java.util.TimeZone;
54 public class DateTimeSettingsSetupWizard extends Activity
55 implements OnClickListener, OnItemClickListener, OnCheckedChangeListener,
56 PreferenceFragment.OnPreferenceStartFragmentCallback {
57 private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
59 // force the first status of auto datetime flag.
60 private static final String EXTRA_INITIAL_AUTO_DATETIME_VALUE =
61 "extra_initial_auto_datetime_value";
63 // If we have enough screen real estate, we use a radically different layout with
64 // big date and time pickers right on the screen, which requires very different handling.
65 // Otherwise, we use the standard date time settings fragment.
66 private boolean mUsingXLargeLayout;
68 /* Available only in XL */
69 private CompoundButton mAutoDateTimeButton;
70 // private CompoundButton mAutoTimeZoneButton;
72 private Button mTimeZoneButton;
73 private ListPopupWindow mTimeZonePopup;
74 private SimpleAdapter mTimeZoneAdapter;
75 private TimeZone mSelectedTimeZone;
77 private TimePicker mTimePicker;
78 private DatePicker mDatePicker;
79 private InputMethodManager mInputMethodManager;
82 protected void onCreate(Bundle savedInstanceState) {
83 requestWindowFeature(Window.FEATURE_NO_TITLE);
84 super.onCreate(savedInstanceState);
85 setContentView(R.layout.date_time_settings_setupwizard);
87 // we know we've loaded the special xlarge layout because it has controls
88 // not present in the standard layout
89 mUsingXLargeLayout = findViewById(R.id.time_zone_button) != null;
90 if (mUsingXLargeLayout) {
91 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
94 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
95 findViewById(R.id.next_button).setOnClickListener(this);
97 mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false,
98 R.layout.date_time_setup_custom_list_item_2);
100 // For the normal view, disable Back since changes stick immediately
101 // and can't be canceled, and we already have a Next button. For xLarge,
102 // though, we save up our changes and set them upon Next, so Back can
103 // cancel. And also, in xlarge, we need the keyboard dismiss button
105 if (!mUsingXLargeLayout) {
106 final View layoutRoot = findViewById(R.id.layout_root);
107 layoutRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
111 public void initUiForXl() {
112 // Currently just comment out codes related to auto timezone.
113 // TODO: Remove them when we are sure they are unnecessary.
115 final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
116 mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
117 mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
118 mAutoTimeZoneButton.setOnCheckedChangeListener(this);
119 mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
120 R.string.zone_auto_summaryOff);*/
122 final TimeZone tz = TimeZone.getDefault();
123 mSelectedTimeZone = tz;
124 mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
125 mTimeZoneButton.setText(tz.getDisplayName());
126 // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
127 mTimeZoneButton.setOnClickListener(this);
129 final boolean autoDateTimeEnabled;
130 final Intent intent = getIntent();
131 if (intent.hasExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE)) {
132 autoDateTimeEnabled = intent.getBooleanExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE, false);
134 autoDateTimeEnabled = isAutoDateTimeEnabled();
137 mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto_button);
138 mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
139 mAutoDateTimeButton.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
140 R.string.date_time_auto_summaryOff);
141 mAutoDateTimeButton.setOnCheckedChangeListener(this);
143 mTimePicker = (TimePicker)findViewById(R.id.time_picker);
144 mTimePicker.setEnabled(!autoDateTimeEnabled);
145 mDatePicker = (DatePicker)findViewById(R.id.date_picker);
146 mDatePicker.setEnabled(!autoDateTimeEnabled);
147 mDatePicker.setCalendarViewShown(false);
149 mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
151 ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
152 final Button skipButton = (Button)findViewById(R.id.skip_button);
153 if (skipButton != null) {
154 skipButton.setOnClickListener(this);
159 public void onResume() {
161 IntentFilter filter = new IntentFilter();
162 filter.addAction(Intent.ACTION_TIME_TICK);
163 filter.addAction(Intent.ACTION_TIME_CHANGED);
164 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
165 registerReceiver(mIntentReceiver, filter, null, null);
169 public void onPause() {
171 unregisterReceiver(mIntentReceiver);
175 public void onClick(View view) {
176 switch (view.getId()) {
177 case R.id.time_zone_button: {
178 showTimezonePicker(R.id.time_zone_button);
181 case R.id.next_button: {
182 if (mSelectedTimeZone != null) {
183 final TimeZone systemTimeZone = TimeZone.getDefault();
184 if (!systemTimeZone.equals(mSelectedTimeZone)) {
185 Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
186 final AlarmManager alarm = (AlarmManager)
187 getSystemService(Context.ALARM_SERVICE);
188 alarm.setTimeZone(mSelectedTimeZone.getID());
191 if (mAutoDateTimeButton != null) {
192 Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
193 mAutoDateTimeButton.isChecked() ? 1 : 0);
194 if (!mAutoDateTimeButton.isChecked()) {
195 DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
196 mDatePicker.getDayOfMonth());
197 DateTimeSettings.setTime(
198 mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
202 case R.id.skip_button: {
203 setResult(RESULT_OK);
211 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
212 final boolean autoEnabled = isChecked; // just for readibility.
213 /*if (buttonView == mAutoTimeZoneButton) {
214 // In XL screen, we save all the state only when the next button is pressed.
215 if (!mUsingXLargeLayout) {
216 Settings.System.putInt(getContentResolver(),
217 Settings.System.AUTO_TIME_ZONE,
220 mTimeZone.setEnabled(!autoEnabled);
222 findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
223 findViewById(R.id.zone_picker).setVisibility(View.GONE);
226 if (buttonView == mAutoDateTimeButton) {
227 Settings.System.putInt(getContentResolver(),
228 Settings.System.AUTO_TIME,
230 mTimePicker.setEnabled(!autoEnabled);
231 mDatePicker.setEnabled(!autoEnabled);
234 final View focusedView = getCurrentFocus();
235 if (focusedView != null) {
236 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
237 focusedView.clearFocus();
243 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
244 final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
245 if (mUsingXLargeLayout) {
246 mSelectedTimeZone = tz;
247 final Calendar now = Calendar.getInstance(tz);
248 if (mTimeZoneButton != null) {
249 mTimeZoneButton.setText(tz.getDisplayName());
251 // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
252 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
253 now.get(Calendar.DAY_OF_MONTH));
254 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
255 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
257 // in prefs mode, we actually change the setting right now, as opposed to waiting
258 // until Next is pressed in xLarge mode
259 final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
260 alarm.setTimeZone(tz.getID());
261 DateTimeSettings settingsFragment = (DateTimeSettings) getFragmentManager().
262 findFragmentById(R.id.date_time_settings_fragment);
263 settingsFragment.updateTimeAndDateDisplay(this);
265 mTimeZonePopup.dismiss();
269 * If this is called, that means we're in prefs style portrait mode for a large display
270 * and the user has tapped on the time zone preference. If we were a PreferenceActivity,
271 * we'd then launch the timezone fragment in a new activity, but we aren't, and here
272 * on a tablet display, we really want more of a popup picker look' like the one we use
273 * for the xlarge version of this activity. So we just take this opportunity to launch that.
275 * TODO: For phones, we might want to change this to do the "normal" opening
276 * of the zonepicker fragment in its own activity. Or we might end up just
277 * creating a separate DateTimeSettingsSetupWizardPhone activity that subclasses
278 * PreferenceActivity in the first place to handle all that automatically.
281 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
282 showTimezonePicker(R.id.timezone_dropdown_anchor);
286 private void showTimezonePicker(int anchorViewId) {
287 View anchorView = findViewById(anchorViewId);
288 if (anchorView == null) {
289 Log.e(TAG, "Unable to find zone picker anchor view " + anchorViewId);
292 mTimeZonePopup = new ListPopupWindow(this, null);
293 mTimeZonePopup.setWidth(anchorView.getWidth());
294 mTimeZonePopup.setAnchorView(anchorView);
295 mTimeZonePopup.setAdapter(mTimeZoneAdapter);
296 mTimeZonePopup.setOnItemClickListener(this);
297 mTimeZonePopup.setModal(true);
298 mTimeZonePopup.show();
301 private boolean isAutoDateTimeEnabled() {
303 return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
304 } catch (SettingNotFoundException e) {
310 private boolean isAutoTimeZoneEnabled() {
312 return Settings.System.getInt(getContentResolver(),
313 Settings.System.AUTO_TIME_ZONE) > 0;
314 } catch (SettingNotFoundException e) {
319 private void updateTimeAndDateDisplay() {
320 if (!mUsingXLargeLayout) {
323 final Calendar now = Calendar.getInstance();
324 mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
325 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
326 now.get(Calendar.DAY_OF_MONTH));
327 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
328 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
331 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
333 public void onReceive(Context context, Intent intent) {
334 updateTimeAndDateDisplay();