DateTimeSettingsSetupWizard.java revision a85bfd2552ea4ee73e4a98e85194a3eb42284940
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.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.ActivityInfo;
26import android.content.res.Configuration;
27import android.os.Bundle;
28import android.provider.Settings;
29import android.provider.Settings.SettingNotFoundException;
30import android.text.format.DateFormat;
31import android.util.Log;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.view.Window;
35import android.view.inputmethod.InputMethodManager;
36import android.widget.AdapterView;
37import android.widget.AdapterView.OnItemClickListener;
38import android.widget.Button;
39import android.widget.CompoundButton;
40import android.widget.CompoundButton.OnCheckedChangeListener;
41import android.widget.DatePicker;
42import android.widget.ListPopupWindow;
43import android.widget.SimpleAdapter;
44import android.widget.TimePicker;
45
46import java.util.Calendar;
47import java.util.TimeZone;
48
49public class DateTimeSettingsSetupWizard extends Activity
50        implements OnClickListener, OnItemClickListener, OnCheckedChangeListener{
51    private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
52
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";
56
57    private boolean mXLargeScreenSize;
58
59    /* Available only in XL */
60    private CompoundButton mAutoDateTimeButton;
61    // private CompoundButton mAutoTimeZoneButton;
62
63    private Button mTimeZoneButton;
64    private ListPopupWindow mTimeZonePopup;
65    private SimpleAdapter mTimeZoneAdapter;
66    private TimeZone mSelectedTimeZone;
67
68    private TimePicker mTimePicker;
69    private DatePicker mDatePicker;
70    private InputMethodManager mInputMethodManager;
71
72    @Override
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_XLARGE;
80        if (mXLargeScreenSize) {
81            initUiForXl();
82        } else {
83            findViewById(R.id.next_button).setOnClickListener(this);
84        }
85    }
86
87    public void initUiForXl() {
88        final View layoutRoot = findViewById(R.id.layout_root);
89        layoutRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
90
91        // Currently just comment out codes related to auto timezone.
92        // TODO: Remove them when we are sure they are unnecessary.
93        /*
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);*/
100
101        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
102
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);
111
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);
116        } else {
117            autoDateTimeEnabled = isAutoDateTimeEnabled();
118        }
119
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);
125
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);
131
132        mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
133
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);
138        }
139    }
140
141    @Override
142    public void onResume() {
143        super.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);
149    }
150
151    @Override
152    public void onPause() {
153        super.onPause();
154        unregisterReceiver(mIntentReceiver);
155    }
156
157    @Override
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();
168            break;
169        }
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);
176
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());
183                }
184
185                if (!mAutoDateTimeButton.isChecked()) {
186                    DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
187                            mDatePicker.getDayOfMonth());
188                    DateTimeSettings.setTime(
189                            mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
190                }
191            }
192        }  // $FALL-THROUGH$
193        case R.id.skip_button: {
194            setResult(RESULT_OK);
195            finish();
196            break;
197        }
198        }
199    }
200
201    @Override
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,
209                        isChecked ? 1 : 0);
210            }
211            mTimeZone.setEnabled(!autoEnabled);
212            if (isChecked) {
213                findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
214                findViewById(R.id.zone_picker).setVisibility(View.GONE);
215            }
216        } else */
217        if (buttonView == mAutoDateTimeButton) {
218            Settings.System.putInt(getContentResolver(),
219                    Settings.System.AUTO_TIME,
220                    isChecked ? 1 : 0);
221            mTimePicker.setEnabled(!autoEnabled);
222            mDatePicker.setEnabled(!autoEnabled);
223        }
224        if (autoEnabled) {
225            final View focusedView = getCurrentFocus();
226            if (focusedView != null) {
227                mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
228                focusedView.clearFocus();
229            }
230        }
231    }
232
233    @Override
234    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
235        final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
236        mSelectedTimeZone = tz;
237
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();
246    }
247
248    private boolean isAutoDateTimeEnabled() {
249        try {
250            return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
251        } catch (SettingNotFoundException e) {
252            return true;
253        }
254    }
255
256    /*
257    private boolean isAutoTimeZoneEnabled() {
258        try {
259            return Settings.System.getInt(getContentResolver(),
260                    Settings.System.AUTO_TIME_ZONE) > 0;
261        } catch (SettingNotFoundException e) {
262            return true;
263        }
264    }*/
265
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));
273    }
274
275    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
276        @Override
277        public void onReceive(Context context, Intent intent) {
278            updateTimeAndDateDisplay();
279        }
280    };
281}
282