DateTimeSettingsSetupWizard.java revision cc0dc00916c5194cdef4151767ec58c6ccad6578
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        final View layoutRoot = findViewById(R.id.layout_root);
86        layoutRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
87
88        // Currently just comment out codes related to auto timezone.
89        // TODO: Remove them when we are sure they are unnecessary.
90        /*
91        final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
92        mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
93        mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
94        mAutoTimeZoneButton.setOnCheckedChangeListener(this);
95        mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
96                R.string.zone_auto_summaryOff);*/
97
98        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
99
100        final TimeZone tz = TimeZone.getDefault();
101        mSelectedTimeZone = tz;
102        mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
103        mTimeZoneButton.setText(tz.getDisplayName());
104        // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
105        mTimeZoneButton.setOnClickListener(this);
106        mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false,
107                R.layout.date_time_setup_custom_list_item_2);
108
109        final boolean autoDateTimeEnabled;
110        final Intent intent = getIntent();
111        if (intent.hasExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE)) {
112            autoDateTimeEnabled = intent.getBooleanExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE, false);
113        } else {
114            autoDateTimeEnabled = isAutoDateTimeEnabled();
115        }
116
117        mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto_button);
118        mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
119        mAutoDateTimeButton.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
120                R.string.date_time_auto_summaryOff);
121        mAutoDateTimeButton.setOnCheckedChangeListener(this);
122
123        mTimePicker = (TimePicker)findViewById(R.id.time_picker);
124        mTimePicker.setEnabled(!autoDateTimeEnabled);
125        mDatePicker = (DatePicker)findViewById(R.id.date_picker);
126        mDatePicker.setEnabled(!autoDateTimeEnabled);
127        mDatePicker.setCalendarViewShown(false);
128
129        mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
130
131        ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
132        final Button skipButton = (Button)findViewById(R.id.skip_button);
133        if (skipButton != null) {
134            skipButton.setOnClickListener(this);
135        }
136    }
137
138    @Override
139    public void onClick(View view) {
140        switch (view.getId()) {
141        case R.id.time_zone_button: {
142            mTimeZonePopup = new ListPopupWindow(this, null);
143            mTimeZonePopup.setWidth(mTimeZoneButton.getWidth());
144            mTimeZonePopup.setAnchorView(mTimeZoneButton);
145            mTimeZonePopup.setAdapter(mTimeZoneAdapter);
146            mTimeZonePopup.setOnItemClickListener(this);
147            mTimeZonePopup.setModal(true);
148            mTimeZonePopup.show();
149            break;
150        }
151        case R.id.next_button: {
152            if (mXLargeScreenSize) {
153                /* Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME_ZONE,
154                        mAutoTimeZoneButton.isChecked() ? 1 : 0); */
155                Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
156                        mAutoDateTimeButton.isChecked() ? 1 : 0);
157
158                final TimeZone systemTimeZone = TimeZone.getDefault();
159                if (!systemTimeZone.equals(mSelectedTimeZone)) {
160                    Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
161                    final AlarmManager alarm = (AlarmManager)
162                            getSystemService(Context.ALARM_SERVICE);
163                    alarm.setTimeZone(mSelectedTimeZone.getID());
164                }
165
166                // Note: in non-XL, Date & Time is stored by DatePickerDialog/TimePickerDialog,
167                // so we don't need to save those values there, while in XL, we need to as
168                // we don't use those Dialogs.
169                DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
170                        mDatePicker.getDayOfMonth());
171                DateTimeSettings.setTime(
172                        mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
173            }
174        }  // $FALL-THROUGH$
175        case R.id.skip_button: {
176            setResult(RESULT_OK);
177            finish();
178            break;
179        }
180        }
181    }
182
183    @Override
184    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
185        final boolean autoEnabled = isChecked;  // just for readibility.
186        /*if (buttonView == mAutoTimeZoneButton) {
187            // In XL screen, we save all the state only when the next button is pressed.
188            if (!mXLargeScreenSize) {
189                Settings.System.putInt(getContentResolver(),
190                        Settings.System.AUTO_TIME_ZONE,
191                        isChecked ? 1 : 0);
192            }
193            mTimeZone.setEnabled(!autoEnabled);
194            if (isChecked) {
195                findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
196                findViewById(R.id.zone_picker).setVisibility(View.GONE);
197            }
198        } else */
199        if (buttonView == mAutoDateTimeButton) {
200            if (!mXLargeScreenSize) {
201                Settings.System.putInt(getContentResolver(),
202                        Settings.System.AUTO_TIME,
203                        isChecked ? 1 : 0);
204            }
205            mTimePicker.setEnabled(!autoEnabled);
206            mDatePicker.setEnabled(!autoEnabled);
207        }
208        if (autoEnabled) {
209            final View focusedView = getCurrentFocus();
210            if (focusedView != null) {
211                mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
212                focusedView.clearFocus();
213            }
214        }
215    }
216
217    @Override
218    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
219        final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
220        mSelectedTimeZone = tz;
221
222        final Calendar now = Calendar.getInstance(tz);
223        mTimeZoneButton.setText(tz.getDisplayName());
224        // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
225        mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
226                now.get(Calendar.DAY_OF_MONTH));
227        mTimePicker.setCurrentHour(now.get(Calendar.HOUR));
228        mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
229        mTimeZonePopup.dismiss();
230    }
231
232    private boolean isAutoDateTimeEnabled() {
233        try {
234            return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
235        } catch (SettingNotFoundException e) {
236            return true;
237        }
238    }
239
240    /*
241    private boolean isAutoTimeZoneEnabled() {
242        try {
243            return Settings.System.getInt(getContentResolver(),
244                    Settings.System.AUTO_TIME_ZONE) > 0;
245        } catch (SettingNotFoundException e) {
246            return true;
247        }
248    }*/
249}
250