DateTimeSettingsSetupWizard.java revision d4fa73f87799fc96b89bec81ab7b322e58de3159
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.util.Log;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.view.Window;
34import android.view.inputmethod.InputMethodManager;
35import android.widget.AdapterView;
36import android.widget.AdapterView.OnItemClickListener;
37import android.widget.Button;
38import android.widget.CompoundButton;
39import android.widget.CompoundButton.OnCheckedChangeListener;
40import android.widget.DatePicker;
41import android.widget.ListPopupWindow;
42import android.widget.SimpleAdapter;
43import android.widget.TextView;
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        ((TextView)findViewById(R.id.date_time_auto_text))
123                .setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
124                        R.string.date_time_auto_summaryOff);
125        mAutoDateTimeButton.setOnCheckedChangeListener(this);
126
127        mTimePicker = (TimePicker)findViewById(R.id.time_picker);
128        mTimePicker.setEnabled(!autoDateTimeEnabled);
129        mDatePicker = (DatePicker)findViewById(R.id.date_picker);
130        mDatePicker.setEnabled(!autoDateTimeEnabled);
131        mDatePicker.setCalendarViewShown(false);
132
133        mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
134
135        ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
136        final Button skipButton = (Button)findViewById(R.id.skip_button);
137        if (skipButton != null) {
138            skipButton.setOnClickListener(this);
139        }
140    }
141
142    @Override
143    public void onResume() {
144        super.onResume();
145        IntentFilter filter = new IntentFilter();
146        filter.addAction(Intent.ACTION_TIME_TICK);
147        filter.addAction(Intent.ACTION_TIME_CHANGED);
148        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
149        registerReceiver(mIntentReceiver, filter, null, null);
150    }
151
152    @Override
153    public void onPause() {
154        super.onPause();
155        unregisterReceiver(mIntentReceiver);
156    }
157
158    @Override
159    public void onClick(View view) {
160        switch (view.getId()) {
161        case R.id.time_zone_button: {
162            mTimeZonePopup = new ListPopupWindow(this, null);
163            mTimeZonePopup.setWidth(mTimeZoneButton.getWidth());
164            mTimeZonePopup.setAnchorView(mTimeZoneButton);
165            mTimeZonePopup.setAdapter(mTimeZoneAdapter);
166            mTimeZonePopup.setOnItemClickListener(this);
167            mTimeZonePopup.setModal(true);
168            mTimeZonePopup.show();
169            break;
170        }
171        case R.id.next_button: {
172            if (mXLargeScreenSize) {
173                /* Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME_ZONE,
174                        mAutoTimeZoneButton.isChecked() ? 1 : 0); */
175                Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
176                        mAutoDateTimeButton.isChecked() ? 1 : 0);
177
178                final TimeZone systemTimeZone = TimeZone.getDefault();
179                if (!systemTimeZone.equals(mSelectedTimeZone)) {
180                    Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
181                    final AlarmManager alarm = (AlarmManager)
182                            getSystemService(Context.ALARM_SERVICE);
183                    alarm.setTimeZone(mSelectedTimeZone.getID());
184                }
185
186                if (!mAutoDateTimeButton.isChecked()) {
187                    DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
188                            mDatePicker.getDayOfMonth());
189                    DateTimeSettings.setTime(
190                            mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
191                }
192            }
193        }  // $FALL-THROUGH$
194        case R.id.skip_button: {
195            setResult(RESULT_OK);
196            finish();
197            break;
198        }
199        }
200    }
201
202    @Override
203    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
204        final boolean autoEnabled = isChecked;  // just for readibility.
205        /*if (buttonView == mAutoTimeZoneButton) {
206            // In XL screen, we save all the state only when the next button is pressed.
207            if (!mXLargeScreenSize) {
208                Settings.System.putInt(getContentResolver(),
209                        Settings.System.AUTO_TIME_ZONE,
210                        isChecked ? 1 : 0);
211            }
212            mTimeZone.setEnabled(!autoEnabled);
213            if (isChecked) {
214                findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
215                findViewById(R.id.zone_picker).setVisibility(View.GONE);
216            }
217        } else */
218        if (buttonView == mAutoDateTimeButton) {
219            Settings.System.putInt(getContentResolver(),
220                    Settings.System.AUTO_TIME,
221                    isChecked ? 1 : 0);
222            mTimePicker.setEnabled(!autoEnabled);
223            mDatePicker.setEnabled(!autoEnabled);
224        }
225        if (autoEnabled) {
226            final View focusedView = getCurrentFocus();
227            if (focusedView != null) {
228                mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
229                focusedView.clearFocus();
230            }
231        }
232    }
233
234    @Override
235    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
236        final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
237        mSelectedTimeZone = tz;
238
239        final Calendar now = Calendar.getInstance(tz);
240        mTimeZoneButton.setText(tz.getDisplayName());
241        // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
242        mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
243                now.get(Calendar.DAY_OF_MONTH));
244        mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
245        mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
246        mTimeZonePopup.dismiss();
247    }
248
249    private boolean isAutoDateTimeEnabled() {
250        try {
251            return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
252        } catch (SettingNotFoundException e) {
253            return true;
254        }
255    }
256
257    /*
258    private boolean isAutoTimeZoneEnabled() {
259        try {
260            return Settings.System.getInt(getContentResolver(),
261                    Settings.System.AUTO_TIME_ZONE) > 0;
262        } catch (SettingNotFoundException e) {
263            return true;
264        }
265    }*/
266
267    private void updateTimeAndDateDisplay() {
268        final Calendar now = Calendar.getInstance();
269        mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
270        mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
271                now.get(Calendar.DAY_OF_MONTH));
272        mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
273        mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
274    }
275
276    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
277        @Override
278        public void onReceive(Context context, Intent intent) {
279            updateTimeAndDateDisplay();
280        }
281    };
282}
283