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.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.pm.ActivityInfo;
26 import android.content.res.Configuration;
27 import android.os.Bundle;
28 import android.provider.Settings;
29 import android.provider.Settings.SettingNotFoundException;
30 import android.text.format.DateFormat;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.view.Window;
35 import android.view.inputmethod.InputMethodManager;
36 import android.widget.AdapterView;
37 import android.widget.AdapterView.OnItemClickListener;
38 import android.widget.Button;
39 import android.widget.CompoundButton;
40 import android.widget.CompoundButton.OnCheckedChangeListener;
41 import android.widget.DatePicker;
42 import android.widget.ListPopupWindow;
43 import android.widget.SimpleAdapter;
44 import android.widget.TimePicker;
46 import java.util.Calendar;
47 import java.util.TimeZone;
49 public class DateTimeSettingsSetupWizard extends Activity
50 implements OnClickListener, OnItemClickListener, OnCheckedChangeListener{
51 private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
53 // force the first status of auto datetime flag.
54 private static final String EXTRA_INITIAL_AUTO_DATETIME_VALUE =
55 "extra_initial_auto_datetime_value";
57 private boolean mXLargeScreenSize;
59 /* Available only in XL */
60 private CompoundButton mAutoDateTimeButton;
61 // private CompoundButton mAutoTimeZoneButton;
63 private Button mTimeZoneButton;
64 private ListPopupWindow mTimeZonePopup;
65 private SimpleAdapter mTimeZoneAdapter;
66 private TimeZone mSelectedTimeZone;
68 private TimePicker mTimePicker;
69 private DatePicker mDatePicker;
70 private InputMethodManager mInputMethodManager;
73 protected void onCreate(Bundle savedInstanceState) {
74 requestWindowFeature(Window.FEATURE_NO_TITLE);
75 super.onCreate(savedInstanceState);
76 setContentView(R.layout.date_time_settings_setupwizard);
77 mXLargeScreenSize = (getResources().getConfiguration().screenLayout
78 & Configuration.SCREENLAYOUT_SIZE_MASK)
79 >= Configuration.SCREENLAYOUT_SIZE_LARGE;
80 if (mXLargeScreenSize) {
83 findViewById(R.id.next_button).setOnClickListener(this);
87 public void initUiForXl() {
88 final View layoutRoot = findViewById(R.id.layout_root);
89 layoutRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
91 // Currently just comment out codes related to auto timezone.
92 // TODO: Remove them when we are sure they are unnecessary.
94 final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
95 mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
96 mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
97 mAutoTimeZoneButton.setOnCheckedChangeListener(this);
98 mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
99 R.string.zone_auto_summaryOff);*/
101 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
103 final TimeZone tz = TimeZone.getDefault();
104 mSelectedTimeZone = tz;
105 mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
106 mTimeZoneButton.setText(tz.getDisplayName());
107 // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
108 mTimeZoneButton.setOnClickListener(this);
109 mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false,
110 R.layout.date_time_setup_custom_list_item_2);
112 final boolean autoDateTimeEnabled;
113 final Intent intent = getIntent();
114 if (intent.hasExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE)) {
115 autoDateTimeEnabled = intent.getBooleanExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE, false);
117 autoDateTimeEnabled = isAutoDateTimeEnabled();
120 mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto_button);
121 mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
122 mAutoDateTimeButton.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
123 R.string.date_time_auto_summaryOff);
124 mAutoDateTimeButton.setOnCheckedChangeListener(this);
126 mTimePicker = (TimePicker)findViewById(R.id.time_picker);
127 mTimePicker.setEnabled(!autoDateTimeEnabled);
128 mDatePicker = (DatePicker)findViewById(R.id.date_picker);
129 mDatePicker.setEnabled(!autoDateTimeEnabled);
130 mDatePicker.setCalendarViewShown(false);
132 mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
134 ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
135 final Button skipButton = (Button)findViewById(R.id.skip_button);
136 if (skipButton != null) {
137 skipButton.setOnClickListener(this);
142 public void onResume() {
144 IntentFilter filter = new IntentFilter();
145 filter.addAction(Intent.ACTION_TIME_TICK);
146 filter.addAction(Intent.ACTION_TIME_CHANGED);
147 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
148 registerReceiver(mIntentReceiver, filter, null, null);
152 public void onPause() {
154 unregisterReceiver(mIntentReceiver);
158 public void onClick(View view) {
159 switch (view.getId()) {
160 case R.id.time_zone_button: {
161 mTimeZonePopup = new ListPopupWindow(this, null);
162 mTimeZonePopup.setWidth(mTimeZoneButton.getWidth());
163 mTimeZonePopup.setAnchorView(mTimeZoneButton);
164 mTimeZonePopup.setAdapter(mTimeZoneAdapter);
165 mTimeZonePopup.setOnItemClickListener(this);
166 mTimeZonePopup.setModal(true);
167 mTimeZonePopup.show();
170 case R.id.next_button: {
171 if (mXLargeScreenSize) {
172 /* Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME_ZONE,
173 mAutoTimeZoneButton.isChecked() ? 1 : 0); */
174 Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
175 mAutoDateTimeButton.isChecked() ? 1 : 0);
177 final TimeZone systemTimeZone = TimeZone.getDefault();
178 if (!systemTimeZone.equals(mSelectedTimeZone)) {
179 Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
180 final AlarmManager alarm = (AlarmManager)
181 getSystemService(Context.ALARM_SERVICE);
182 alarm.setTimeZone(mSelectedTimeZone.getID());
185 if (!mAutoDateTimeButton.isChecked()) {
186 DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
187 mDatePicker.getDayOfMonth());
188 DateTimeSettings.setTime(
189 mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
193 case R.id.skip_button: {
194 setResult(RESULT_OK);
202 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
203 final boolean autoEnabled = isChecked; // just for readibility.
204 /*if (buttonView == mAutoTimeZoneButton) {
205 // In XL screen, we save all the state only when the next button is pressed.
206 if (!mXLargeScreenSize) {
207 Settings.System.putInt(getContentResolver(),
208 Settings.System.AUTO_TIME_ZONE,
211 mTimeZone.setEnabled(!autoEnabled);
213 findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
214 findViewById(R.id.zone_picker).setVisibility(View.GONE);
217 if (buttonView == mAutoDateTimeButton) {
218 Settings.System.putInt(getContentResolver(),
219 Settings.System.AUTO_TIME,
221 mTimePicker.setEnabled(!autoEnabled);
222 mDatePicker.setEnabled(!autoEnabled);
225 final View focusedView = getCurrentFocus();
226 if (focusedView != null) {
227 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
228 focusedView.clearFocus();
234 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
235 final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
236 mSelectedTimeZone = tz;
238 final Calendar now = Calendar.getInstance(tz);
239 mTimeZoneButton.setText(tz.getDisplayName());
240 // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
241 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
242 now.get(Calendar.DAY_OF_MONTH));
243 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
244 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
245 mTimeZonePopup.dismiss();
248 private boolean isAutoDateTimeEnabled() {
250 return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
251 } catch (SettingNotFoundException e) {
257 private boolean isAutoTimeZoneEnabled() {
259 return Settings.System.getInt(getContentResolver(),
260 Settings.System.AUTO_TIME_ZONE) > 0;
261 } catch (SettingNotFoundException e) {
266 private void updateTimeAndDateDisplay() {
267 final Calendar now = Calendar.getInstance();
268 mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
269 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
270 now.get(Calendar.DAY_OF_MONTH));
271 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
272 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
275 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
277 public void onReceive(Context context, Intent intent) {
278 updateTimeAndDateDisplay();