DateTimeSettingsSetupWizard.java revision fe3b6bea01de328cdf7d6cf414607304fcdeaed6
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 com.android.settings.ZonePicker.ZoneSelectionListener;
20
21import android.app.Activity;
22import android.app.StatusBarManager;
23import android.content.Context;
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.Button;
34import android.widget.CompoundButton;
35import android.widget.CompoundButton.OnCheckedChangeListener;
36import android.widget.DatePicker;
37import android.widget.TimePicker;
38
39import java.util.Calendar;
40import java.util.TimeZone;
41
42public class DateTimeSettingsSetupWizard extends Activity
43        implements OnClickListener, ZoneSelectionListener, OnCheckedChangeListener{
44    private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
45
46    private boolean mXLargeScreenSize;
47
48    /* Available only in XL */
49    private CompoundButton mAutoDateTimeButton;
50    private CompoundButton mAutoTimeZoneButton;
51    private Button mTimeZone;
52    private TimePicker mTimePicker;
53    private DatePicker mDatePicker;
54    private InputMethodManager mInputMethodManager;
55
56    private StatusBarManager mStatusBarManager;
57
58    @Override
59    protected void onCreate(Bundle savedInstanceState) {
60        requestWindowFeature(Window.FEATURE_NO_TITLE);
61        super.onCreate(savedInstanceState);
62        setContentView(R.layout.date_time_settings_setupwizard);
63        mStatusBarManager = (StatusBarManager)getSystemService(Context.STATUS_BAR_SERVICE);
64
65        mXLargeScreenSize = (getResources().getConfiguration().screenLayout
66                & Configuration.SCREENLAYOUT_SIZE_MASK)
67                == Configuration.SCREENLAYOUT_SIZE_XLARGE;
68        if (mXLargeScreenSize) {
69            initUiForXl();
70        } else {
71            findViewById(R.id.next_button).setOnClickListener(this);
72        }
73    }
74
75    public void initUiForXl() {
76        final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
77        mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
78        mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
79        mAutoTimeZoneButton.setOnCheckedChangeListener(this);
80        mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
81                R.string.zone_auto_summaryOff);
82
83        final TimeZone tz = TimeZone.getDefault();
84        mTimeZone = (Button)findViewById(R.id.current_time_zone);
85        mTimeZone.setText(DateTimeSettings.getTimeZoneText(tz));
86        mTimeZone.setOnClickListener(this);
87        mTimeZone.setEnabled(!autoTimeZoneEnabled);
88
89        final boolean autoDateTimeEnabled = isAutoDateTimeEnabled();
90        mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto);
91        mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
92        mAutoDateTimeButton.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
93                R.string.date_time_auto_summaryOff);
94        mAutoDateTimeButton.setOnCheckedChangeListener(this);
95
96        mTimePicker = (TimePicker)findViewById(R.id.time_picker);
97        mTimePicker.setEnabled(!autoDateTimeEnabled);
98        mDatePicker = (DatePicker)findViewById(R.id.date_picker);
99        mDatePicker.setEnabled(!autoDateTimeEnabled);
100
101        mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
102
103        ((ZonePicker)getFragmentManager().findFragmentById(R.id.zone_picker_fragment))
104                .setZoneSelectionListener(this);
105
106        ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
107        ((Button)findViewById(R.id.skip_button)).setOnClickListener(this);
108
109        if (mStatusBarManager != null) {
110            mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND
111                    | StatusBarManager.DISABLE_NOTIFICATION_ICONS
112                    | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
113                    | StatusBarManager.DISABLE_SYSTEM_INFO
114                    | StatusBarManager.DISABLE_NAVIGATION);
115        } else {
116            Log.e(TAG, "StatusBarManager isn't available.");
117        }
118    }
119
120    @Override
121    public void onDestroy() {
122        if (mStatusBarManager != null) {
123            mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
124        }
125        super.onDestroy();
126    }
127
128    @Override
129    public void onClick(View view) {
130        switch (view.getId()) {
131        case R.id.current_time_zone: {
132            findViewById(R.id.current_time_zone).setVisibility(View.GONE);
133            findViewById(R.id.zone_picker).setVisibility(View.VISIBLE);
134            break;
135        }
136        case R.id.next_button: {
137            if (mXLargeScreenSize) {
138                Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME_ZONE,
139                        mAutoTimeZoneButton.isChecked() ? 1 : 0);
140                Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
141                        mAutoDateTimeButton.isChecked() ? 1 : 0);
142                // Note: in non-XL, Date & Time is stored by DatePickerDialog/TimePickerDialog,
143                // so we don't need to save those values there, while in XL, we need to as
144                // we don't use those Dialogs.
145                DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
146                        mDatePicker.getDayOfMonth());
147                DateTimeSettings.setTime(
148                        mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
149            }
150        }  // $FALL-THROUGH$
151        case R.id.skip_button: {
152            setResult(RESULT_OK);
153            finish();
154            break;
155        }
156        }
157    }
158
159    @Override
160    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
161        final boolean autoEnabled = isChecked;  // just for readibility.
162        if (buttonView == mAutoTimeZoneButton) {
163            // In XL screen, we save all the state only when the next button is pressed.
164            if (!mXLargeScreenSize) {
165                Settings.System.putInt(getContentResolver(),
166                        Settings.System.AUTO_TIME_ZONE,
167                        isChecked ? 1 : 0);
168            }
169            mTimeZone.setEnabled(!autoEnabled);
170            if (isChecked) {
171                findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
172                findViewById(R.id.zone_picker).setVisibility(View.GONE);
173            }
174        } else if (buttonView == mAutoDateTimeButton) {
175            if (!mXLargeScreenSize) {
176                Settings.System.putInt(getContentResolver(),
177                        Settings.System.AUTO_TIME,
178                        isChecked ? 1 : 0);
179            }
180            mTimePicker.setEnabled(!autoEnabled);
181            mDatePicker.setEnabled(!autoEnabled);
182        }
183        if (autoEnabled) {
184            final View focusedView = getCurrentFocus();
185            if (focusedView != null) {
186                mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
187                focusedView.clearFocus();
188            }
189        }
190    }
191
192    @Override
193    public void onZoneSelected(TimeZone tz) {
194        findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
195        findViewById(R.id.zone_picker).setVisibility(View.GONE);
196        final Calendar now = Calendar.getInstance(tz);
197        mTimeZone.setText(DateTimeSettings.getTimeZoneText(tz));
198        mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
199                now.get(Calendar.DAY_OF_MONTH));
200        mTimePicker.setCurrentHour(now.get(Calendar.HOUR));
201        mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
202    }
203
204    private boolean isAutoDateTimeEnabled() {
205        try {
206            return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
207        } catch (SettingNotFoundException e) {
208            return true;
209        }
210    }
211
212    private boolean isAutoTimeZoneEnabled() {
213        try {
214            return Settings.System.getInt(getContentResolver(),
215                    Settings.System.AUTO_TIME_ZONE) > 0;
216        } catch (SettingNotFoundException e) {
217            return true;
218        }
219    }
220}
221