DateTimeSettingsSetupWizard.java revision da40cc28d9156d89392dedd83d827967ff6ffb7f
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
17package com.android.settings;
18
19import android.app.Activity;
20import android.app.AlarmManager;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.content.res.Configuration;
25import android.os.Bundle;
26import android.provider.Settings;
27import android.provider.Settings.SettingNotFoundException;
28import android.util.Log;
29import android.view.View;
30import android.view.View.OnClickListener;
31import android.view.Window;
32import android.view.inputmethod.InputMethodManager;
33import android.widget.AdapterView;
34import android.widget.AdapterView.OnItemClickListener;
35import android.widget.Button;
36import android.widget.CompoundButton;
37import android.widget.CompoundButton.OnCheckedChangeListener;
38import android.widget.DatePicker;
39import android.widget.ListPopupWindow;
40import android.widget.SimpleAdapter;
41import android.widget.TimePicker;
42
43import java.util.Calendar;
44import java.util.TimeZone;
45
46public class DateTimeSettingsSetupWizard extends Activity
47        implements OnClickListener, OnItemClickListener, OnCheckedChangeListener{
48    private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
49
50    // force the first status of auto datetime flag.
51    private static final String EXTRA_INITIAL_AUTO_DATETIME_VALUE =
52        "extra_initial_auto_datetime_value";
53
54    private boolean mXLargeScreenSize;
55
56    /* Available only in XL */
57    private CompoundButton mAutoDateTimeButton;
58    // private CompoundButton mAutoTimeZoneButton;
59
60    private Button mTimeZoneButton;
61    private ListPopupWindow mTimeZonePopup;
62    private SimpleAdapter mTimeZoneAdapter;
63    private TimeZone mSelectedTimeZone;
64
65    private TimePicker mTimePicker;
66    private DatePicker mDatePicker;
67    private InputMethodManager mInputMethodManager;
68
69    @Override
70    protected void onCreate(Bundle savedInstanceState) {
71        requestWindowFeature(Window.FEATURE_NO_TITLE);
72        super.onCreate(savedInstanceState);
73        setContentView(R.layout.date_time_settings_setupwizard);
74        mXLargeScreenSize = (getResources().getConfiguration().screenLayout
75                & Configuration.SCREENLAYOUT_SIZE_MASK)
76                == Configuration.SCREENLAYOUT_SIZE_XLARGE;
77        if (mXLargeScreenSize) {
78            initUiForXl();
79        } else {
80            findViewById(R.id.next_button).setOnClickListener(this);
81        }
82    }
83
84    public void initUiForXl() {
85        // Currently just comment out codes related to auto timezone.
86        // TODO: Remove them when we are sure they are unnecessary.
87        /*
88        final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
89        mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
90        mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
91        mAutoTimeZoneButton.setOnCheckedChangeListener(this);
92        mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
93                R.string.zone_auto_summaryOff);*/
94
95        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
96
97        final TimeZone tz = TimeZone.getDefault();
98        mSelectedTimeZone = tz;
99        mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
100        mTimeZoneButton.setText(tz.getDisplayName());
101        // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
102        mTimeZoneButton.setOnClickListener(this);
103        mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false);
104
105        final boolean autoDateTimeEnabled;
106        final Intent intent = getIntent();
107        if (intent.hasExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE)) {
108            autoDateTimeEnabled = intent.getBooleanExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE, false);
109        } else {
110            autoDateTimeEnabled = isAutoDateTimeEnabled();
111        }
112
113        mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto);
114        mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
115        mAutoDateTimeButton.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
116                R.string.date_time_auto_summaryOff);
117        mAutoDateTimeButton.setOnCheckedChangeListener(this);
118
119        mTimePicker = (TimePicker)findViewById(R.id.time_picker);
120        mTimePicker.setEnabled(!autoDateTimeEnabled);
121        mDatePicker = (DatePicker)findViewById(R.id.date_picker);
122        mDatePicker.setEnabled(!autoDateTimeEnabled);
123
124        mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
125
126        ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
127        ((Button)findViewById(R.id.skip_button)).setOnClickListener(this);
128    }
129
130    @Override
131    public void onClick(View view) {
132        switch (view.getId()) {
133        case R.id.time_zone_button: {
134            mTimeZonePopup = new ListPopupWindow(this, null);
135            mTimeZonePopup.setWidth(mTimeZoneButton.getWidth());
136            mTimeZonePopup.setAnchorView(mTimeZoneButton);
137            mTimeZonePopup.setAdapter(mTimeZoneAdapter);
138            mTimeZonePopup.setOnItemClickListener(this);
139            mTimeZonePopup.setModal(true);
140            mTimeZonePopup.show();
141            break;
142        }
143        case R.id.next_button: {
144            if (mXLargeScreenSize) {
145                /* Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME_ZONE,
146                        mAutoTimeZoneButton.isChecked() ? 1 : 0); */
147                Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
148                        mAutoDateTimeButton.isChecked() ? 1 : 0);
149
150                final TimeZone systemTimeZone = TimeZone.getDefault();
151                if (!systemTimeZone.equals(mSelectedTimeZone)) {
152                    Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
153                    final AlarmManager alarm = (AlarmManager)
154                            getSystemService(Context.ALARM_SERVICE);
155                    alarm.setTimeZone(mSelectedTimeZone.getID());
156                }
157
158                // Note: in non-XL, Date & Time is stored by DatePickerDialog/TimePickerDialog,
159                // so we don't need to save those values there, while in XL, we need to as
160                // we don't use those Dialogs.
161                DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
162                        mDatePicker.getDayOfMonth());
163                DateTimeSettings.setTime(
164                        mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
165            }
166        }  // $FALL-THROUGH$
167        case R.id.skip_button: {
168            setResult(RESULT_OK);
169            finish();
170            break;
171        }
172        }
173    }
174
175    @Override
176    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
177        final boolean autoEnabled = isChecked;  // just for readibility.
178        /*if (buttonView == mAutoTimeZoneButton) {
179            // In XL screen, we save all the state only when the next button is pressed.
180            if (!mXLargeScreenSize) {
181                Settings.System.putInt(getContentResolver(),
182                        Settings.System.AUTO_TIME_ZONE,
183                        isChecked ? 1 : 0);
184            }
185            mTimeZone.setEnabled(!autoEnabled);
186            if (isChecked) {
187                findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
188                findViewById(R.id.zone_picker).setVisibility(View.GONE);
189            }
190        } else */
191        if (buttonView == mAutoDateTimeButton) {
192            if (!mXLargeScreenSize) {
193                Settings.System.putInt(getContentResolver(),
194                        Settings.System.AUTO_TIME,
195                        isChecked ? 1 : 0);
196            }
197            mTimePicker.setEnabled(!autoEnabled);
198            mDatePicker.setEnabled(!autoEnabled);
199        }
200        if (autoEnabled) {
201            final View focusedView = getCurrentFocus();
202            if (focusedView != null) {
203                mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
204                focusedView.clearFocus();
205            }
206        }
207    }
208
209    @Override
210    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
211        final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
212        mSelectedTimeZone = tz;
213
214        final Calendar now = Calendar.getInstance(tz);
215        mTimeZoneButton.setText(tz.getDisplayName());
216        // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
217        mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
218                now.get(Calendar.DAY_OF_MONTH));
219        mTimePicker.setCurrentHour(now.get(Calendar.HOUR));
220        mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
221        mTimeZonePopup.dismiss();
222    }
223
224    private boolean isAutoDateTimeEnabled() {
225        try {
226            return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
227        } catch (SettingNotFoundException e) {
228            return true;
229        }
230    }
231
232    /*
233    private boolean isAutoTimeZoneEnabled() {
234        try {
235            return Settings.System.getInt(getContentResolver(),
236                    Settings.System.AUTO_TIME_ZONE) > 0;
237        } catch (SettingNotFoundException e) {
238            return true;
239        }
240    }*/
241}
242