OSDN Git Service

am 57c88834: (-s ours) am 9fe36302: (-s ours) am 9cb97c44: Merge "DO NOT MERGE Fix...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / DateTimeSettingsSetupWizard.java
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
17 package com.android.settings;
18
19 import android.app.Activity;
20 import android.app.AlarmManager;
21 import android.app.Fragment;
22 import android.app.FragmentTransaction;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.pm.ActivityInfo;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.preference.Preference;
31 import android.preference.PreferenceFragment;
32 import android.provider.Settings;
33 import android.provider.Settings.SettingNotFoundException;
34 import android.util.Log;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.view.Window;
38 import android.view.inputmethod.InputMethodManager;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.OnItemClickListener;
41 import android.widget.Button;
42 import android.widget.CompoundButton;
43 import android.widget.CompoundButton.OnCheckedChangeListener;
44 import android.widget.DatePicker;
45 import android.widget.LinearLayout;
46 import android.widget.ListPopupWindow;
47 import android.widget.SimpleAdapter;
48 import android.widget.TextView;
49 import android.widget.TimePicker;
50
51 import java.util.Calendar;
52 import java.util.TimeZone;
53
54 public class DateTimeSettingsSetupWizard extends Activity
55         implements OnClickListener, OnItemClickListener, OnCheckedChangeListener,
56         PreferenceFragment.OnPreferenceStartFragmentCallback {
57     private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
58
59     // force the first status of auto datetime flag.
60     private static final String EXTRA_INITIAL_AUTO_DATETIME_VALUE =
61             "extra_initial_auto_datetime_value";
62
63     // If we have enough screen real estate, we use a radically different layout with
64     // big date and time pickers right on the screen, which requires very different handling.
65     // Otherwise, we use the standard date time settings fragment.
66     private boolean mUsingXLargeLayout;
67
68     /* Available only in XL */
69     private CompoundButton mAutoDateTimeButton;
70     // private CompoundButton mAutoTimeZoneButton;
71
72     private Button mTimeZoneButton;
73     private ListPopupWindow mTimeZonePopup;
74     private SimpleAdapter mTimeZoneAdapter;
75     private TimeZone mSelectedTimeZone;
76
77     private TimePicker mTimePicker;
78     private DatePicker mDatePicker;
79     private InputMethodManager mInputMethodManager;
80
81     @Override
82     protected void onCreate(Bundle savedInstanceState) {
83         requestWindowFeature(Window.FEATURE_NO_TITLE);
84         super.onCreate(savedInstanceState);
85         setContentView(R.layout.date_time_settings_setupwizard);
86
87         // we know we've loaded the special xlarge layout because it has controls
88         // not present in the standard layout
89         mUsingXLargeLayout = findViewById(R.id.time_zone_button) != null;
90         if (mUsingXLargeLayout) {
91             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
92             initUiForXl();
93         } else {
94             setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
95             findViewById(R.id.next_button).setOnClickListener(this);
96         }
97         mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false,
98             R.layout.date_time_setup_custom_list_item_2);
99
100         // For the normal view, disable Back since changes stick immediately
101         // and can't be canceled, and we already have a Next button. For xLarge,
102         // though, we save up our changes and set them upon Next, so Back can
103         // cancel. And also, in xlarge, we need the keyboard dismiss button
104         // to be available.
105         if (!mUsingXLargeLayout) {
106             final View layoutRoot = findViewById(R.id.layout_root);
107             layoutRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_BACK);
108         }
109     }
110
111     public void initUiForXl() {
112         // Currently just comment out codes related to auto timezone.
113         // TODO: Remove them when we are sure they are unnecessary.
114         /*
115         final boolean autoTimeZoneEnabled = isAutoTimeZoneEnabled();
116         mAutoTimeZoneButton = (CompoundButton)findViewById(R.id.time_zone_auto);
117         mAutoTimeZoneButton.setChecked(autoTimeZoneEnabled);
118         mAutoTimeZoneButton.setOnCheckedChangeListener(this);
119         mAutoTimeZoneButton.setText(autoTimeZoneEnabled ? R.string.zone_auto_summaryOn :
120                 R.string.zone_auto_summaryOff);*/
121
122         final TimeZone tz = TimeZone.getDefault();
123         mSelectedTimeZone = tz;
124         mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
125         mTimeZoneButton.setText(tz.getDisplayName());
126         // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
127         mTimeZoneButton.setOnClickListener(this);
128
129         final boolean autoDateTimeEnabled;
130         final Intent intent = getIntent();
131         if (intent.hasExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE)) {
132             autoDateTimeEnabled = intent.getBooleanExtra(EXTRA_INITIAL_AUTO_DATETIME_VALUE, false);
133         } else {
134             autoDateTimeEnabled = isAutoDateTimeEnabled();
135         }
136
137         mAutoDateTimeButton = (CompoundButton)findViewById(R.id.date_time_auto_button);
138         mAutoDateTimeButton.setChecked(autoDateTimeEnabled);
139         mAutoDateTimeButton.setOnCheckedChangeListener(this);
140
141         mTimePicker = (TimePicker)findViewById(R.id.time_picker);
142         mTimePicker.setEnabled(!autoDateTimeEnabled);
143         mDatePicker = (DatePicker)findViewById(R.id.date_picker);
144         mDatePicker.setEnabled(!autoDateTimeEnabled);
145         mDatePicker.setCalendarViewShown(false);
146
147         mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
148
149         ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
150         final Button skipButton = (Button)findViewById(R.id.skip_button);
151         if (skipButton != null) {
152             skipButton.setOnClickListener(this);
153         }
154     }
155
156     @Override
157     public void onResume() {
158         super.onResume();
159         IntentFilter filter = new IntentFilter();
160         filter.addAction(Intent.ACTION_TIME_TICK);
161         filter.addAction(Intent.ACTION_TIME_CHANGED);
162         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
163         registerReceiver(mIntentReceiver, filter, null, null);
164     }
165
166     @Override
167     public void onPause() {
168         super.onPause();
169         unregisterReceiver(mIntentReceiver);
170     }
171
172     @Override
173     public void onClick(View view) {
174         switch (view.getId()) {
175         case R.id.time_zone_button: {
176             showTimezonePicker(R.id.time_zone_button);
177             break;
178         }
179         case R.id.next_button: {
180             if (mSelectedTimeZone != null) {
181                 final TimeZone systemTimeZone = TimeZone.getDefault();
182                 if (!systemTimeZone.equals(mSelectedTimeZone)) {
183                     Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
184                     final AlarmManager alarm = (AlarmManager)
185                             getSystemService(Context.ALARM_SERVICE);
186                     alarm.setTimeZone(mSelectedTimeZone.getID());
187                 }
188             }
189             if (mAutoDateTimeButton != null) {
190                 Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
191                       mAutoDateTimeButton.isChecked() ? 1 : 0);
192                 if (!mAutoDateTimeButton.isChecked()) {
193                     DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
194                             mDatePicker.getDayOfMonth());
195                     DateTimeSettings.setTime(
196                             mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
197                 }
198             }
199         }  // $FALL-THROUGH$
200         case R.id.skip_button: {
201             setResult(RESULT_OK);
202             finish();
203             break;
204         }
205         }
206     }
207
208     @Override
209     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
210         final boolean autoEnabled = isChecked;  // just for readibility.
211         /*if (buttonView == mAutoTimeZoneButton) {
212             // In XL screen, we save all the state only when the next button is pressed.
213             if (!mUsingXLargeLayout) {
214                 Settings.System.putInt(getContentResolver(),
215                         Settings.System.AUTO_TIME_ZONE,
216                         isChecked ? 1 : 0);
217             }
218             mTimeZone.setEnabled(!autoEnabled);
219             if (isChecked) {
220                 findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
221                 findViewById(R.id.zone_picker).setVisibility(View.GONE);
222             }
223         } else */
224         if (buttonView == mAutoDateTimeButton) {
225             Settings.System.putInt(getContentResolver(),
226                     Settings.System.AUTO_TIME,
227                     isChecked ? 1 : 0);
228             mTimePicker.setEnabled(!autoEnabled);
229             mDatePicker.setEnabled(!autoEnabled);
230         }
231         if (autoEnabled) {
232             final View focusedView = getCurrentFocus();
233             if (focusedView != null) {
234                 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
235                 focusedView.clearFocus();
236             }
237         }
238     }
239
240     @Override
241     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
242         final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
243         if (mUsingXLargeLayout) {
244             mSelectedTimeZone = tz;
245             final Calendar now = Calendar.getInstance(tz);
246             if (mTimeZoneButton != null) {
247                 mTimeZoneButton.setText(tz.getDisplayName());
248             }
249             // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
250             mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
251                     now.get(Calendar.DAY_OF_MONTH));
252             mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
253             mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
254         } else {
255             // in prefs mode, we actually change the setting right now, as opposed to waiting
256             // until Next is pressed in xLarge mode
257             final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
258             alarm.setTimeZone(tz.getID());
259             DateTimeSettings settingsFragment = (DateTimeSettings) getFragmentManager().
260                     findFragmentById(R.id.date_time_settings_fragment);
261             settingsFragment.updateTimeAndDateDisplay(this);
262         }
263         mTimeZonePopup.dismiss();
264     }
265
266     /**
267      * If this is called, that means we're in prefs style portrait mode for a large display
268      * and the user has tapped on the time zone preference. If we were a PreferenceActivity,
269      * we'd then launch the timezone fragment in a new activity, but we aren't, and here
270      * on a tablet display, we really want more of a popup picker look' like the one we use
271      * for the xlarge version of this activity. So we just take this opportunity to launch that.
272      *
273      * TODO: For phones, we might want to change this to do the "normal" opening
274      * of the zonepicker fragment in its own activity. Or we might end up just
275      * creating a separate DateTimeSettingsSetupWizardPhone activity that subclasses
276      * PreferenceActivity in the first place to handle all that automatically.
277      */
278     @Override
279     public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
280         showTimezonePicker(R.id.timezone_dropdown_anchor);
281         return true;
282     }
283
284     private void showTimezonePicker(int anchorViewId) {
285         View anchorView = findViewById(anchorViewId);
286         if (anchorView == null) {
287             Log.e(TAG, "Unable to find zone picker anchor view " + anchorViewId);
288             return;
289         }
290         mTimeZonePopup = new ListPopupWindow(this, null);
291         mTimeZonePopup.setWidth(anchorView.getWidth());
292         mTimeZonePopup.setAnchorView(anchorView);
293         mTimeZonePopup.setAdapter(mTimeZoneAdapter);
294         mTimeZonePopup.setOnItemClickListener(this);
295         mTimeZonePopup.setModal(true);
296         mTimeZonePopup.show();
297     }
298
299     private boolean isAutoDateTimeEnabled() {
300         try {
301             return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
302         } catch (SettingNotFoundException e) {
303             return true;
304         }
305     }
306
307     /*
308     private boolean isAutoTimeZoneEnabled() {
309         try {
310             return Settings.System.getInt(getContentResolver(),
311                     Settings.System.AUTO_TIME_ZONE) > 0;
312         } catch (SettingNotFoundException e) {
313             return true;
314         }
315     }*/
316
317     private void updateTimeAndDateDisplay() {
318         if (!mUsingXLargeLayout) {
319             return;
320         }
321         final Calendar now = Calendar.getInstance();
322         mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
323         mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
324                 now.get(Calendar.DAY_OF_MONTH));
325         mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
326         mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
327     }
328
329     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
330         @Override
331         public void onReceive(Context context, Intent intent) {
332             updateTimeAndDateDisplay();
333         }
334     };
335 }