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.util.Log;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.view.Window;
38 import android.view.inputmethod.InputMethodManager;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.OnItemClickListener;
41 import android.widget.Button;
42 import android.widget.CompoundButton;
43 import android.widget.CompoundButton.OnCheckedChangeListener;
44 import android.widget.DatePicker;
45 import android.widget.LinearLayout;
46 import android.widget.ListPopupWindow;
47 import android.widget.SimpleAdapter;
48 import android.widget.TextView;
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.setOnCheckedChangeListener(this);
141 mTimePicker = (TimePicker)findViewById(R.id.time_picker);
142 mTimePicker.setEnabled(!autoDateTimeEnabled);
143 mDatePicker = (DatePicker)findViewById(R.id.date_picker);
144 mDatePicker.setEnabled(!autoDateTimeEnabled);
145 mDatePicker.setCalendarViewShown(false);
147 mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
149 ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
150 final Button skipButton = (Button)findViewById(R.id.skip_button);
151 if (skipButton != null) {
152 skipButton.setOnClickListener(this);
157 public void onResume() {
159 IntentFilter filter = new IntentFilter();
160 filter.addAction(Intent.ACTION_TIME_TICK);
161 filter.addAction(Intent.ACTION_TIME_CHANGED);
162 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
163 registerReceiver(mIntentReceiver, filter, null, null);
167 public void onPause() {
169 unregisterReceiver(mIntentReceiver);
173 public void onClick(View view) {
174 switch (view.getId()) {
175 case R.id.time_zone_button: {
176 showTimezonePicker(R.id.time_zone_button);
179 case R.id.next_button: {
180 if (mSelectedTimeZone != null) {
181 final TimeZone systemTimeZone = TimeZone.getDefault();
182 if (!systemTimeZone.equals(mSelectedTimeZone)) {
183 Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
184 final AlarmManager alarm = (AlarmManager)
185 getSystemService(Context.ALARM_SERVICE);
186 alarm.setTimeZone(mSelectedTimeZone.getID());
189 if (mAutoDateTimeButton != null) {
190 Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
191 mAutoDateTimeButton.isChecked() ? 1 : 0);
192 if (!mAutoDateTimeButton.isChecked()) {
193 DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
194 mDatePicker.getDayOfMonth());
195 DateTimeSettings.setTime(
196 mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
200 case R.id.skip_button: {
201 setResult(RESULT_OK);
209 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
210 final boolean autoEnabled = isChecked; // just for readibility.
211 /*if (buttonView == mAutoTimeZoneButton) {
212 // In XL screen, we save all the state only when the next button is pressed.
213 if (!mUsingXLargeLayout) {
214 Settings.System.putInt(getContentResolver(),
215 Settings.System.AUTO_TIME_ZONE,
218 mTimeZone.setEnabled(!autoEnabled);
220 findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
221 findViewById(R.id.zone_picker).setVisibility(View.GONE);
224 if (buttonView == mAutoDateTimeButton) {
225 Settings.System.putInt(getContentResolver(),
226 Settings.System.AUTO_TIME,
228 mTimePicker.setEnabled(!autoEnabled);
229 mDatePicker.setEnabled(!autoEnabled);
232 final View focusedView = getCurrentFocus();
233 if (focusedView != null) {
234 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
235 focusedView.clearFocus();
241 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
242 final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
243 if (mUsingXLargeLayout) {
244 mSelectedTimeZone = tz;
245 final Calendar now = Calendar.getInstance(tz);
246 if (mTimeZoneButton != null) {
247 mTimeZoneButton.setText(tz.getDisplayName());
249 // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
250 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
251 now.get(Calendar.DAY_OF_MONTH));
252 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
253 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
255 // in prefs mode, we actually change the setting right now, as opposed to waiting
256 // until Next is pressed in xLarge mode
257 final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
258 alarm.setTimeZone(tz.getID());
259 DateTimeSettings settingsFragment = (DateTimeSettings) getFragmentManager().
260 findFragmentById(R.id.date_time_settings_fragment);
261 settingsFragment.updateTimeAndDateDisplay(this);
263 mTimeZonePopup.dismiss();
267 * If this is called, that means we're in prefs style portrait mode for a large display
268 * and the user has tapped on the time zone preference. If we were a PreferenceActivity,
269 * we'd then launch the timezone fragment in a new activity, but we aren't, and here
270 * on a tablet display, we really want more of a popup picker look' like the one we use
271 * for the xlarge version of this activity. So we just take this opportunity to launch that.
273 * TODO: For phones, we might want to change this to do the "normal" opening
274 * of the zonepicker fragment in its own activity. Or we might end up just
275 * creating a separate DateTimeSettingsSetupWizardPhone activity that subclasses
276 * PreferenceActivity in the first place to handle all that automatically.
279 public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
280 showTimezonePicker(R.id.timezone_dropdown_anchor);
284 private void showTimezonePicker(int anchorViewId) {
285 View anchorView = findViewById(anchorViewId);
286 if (anchorView == null) {
287 Log.e(TAG, "Unable to find zone picker anchor view " + anchorViewId);
290 mTimeZonePopup = new ListPopupWindow(this, null);
291 mTimeZonePopup.setWidth(anchorView.getWidth());
292 mTimeZonePopup.setAnchorView(anchorView);
293 mTimeZonePopup.setAdapter(mTimeZoneAdapter);
294 mTimeZonePopup.setOnItemClickListener(this);
295 mTimeZonePopup.setModal(true);
296 mTimeZonePopup.show();
299 private boolean isAutoDateTimeEnabled() {
301 return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
302 } catch (SettingNotFoundException e) {
308 private boolean isAutoTimeZoneEnabled() {
310 return Settings.System.getInt(getContentResolver(),
311 Settings.System.AUTO_TIME_ZONE) > 0;
312 } catch (SettingNotFoundException e) {
317 private void updateTimeAndDateDisplay() {
318 if (!mUsingXLargeLayout) {
321 final Calendar now = Calendar.getInstance();
322 mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
323 mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
324 now.get(Calendar.DAY_OF_MONTH));
325 mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
326 mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
329 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
331 public void onReceive(Context context, Intent intent) {
332 updateTimeAndDateDisplay();