OSDN Git Service

remove disabling of Back/Dismiss button for 10" mode
[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.text.format.DateFormat;
35 import android.util.Log;
36 import android.view.View;
37 import android.view.View.OnClickListener;
38 import android.view.Window;
39 import android.view.inputmethod.InputMethodManager;
40 import android.widget.AdapterView;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.Button;
43 import android.widget.CompoundButton;
44 import android.widget.CompoundButton.OnCheckedChangeListener;
45 import android.widget.DatePicker;
46 import android.widget.LinearLayout;
47 import android.widget.ListPopupWindow;
48 import android.widget.SimpleAdapter;
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.setText(autoDateTimeEnabled ? R.string.date_time_auto_summaryOn :
140                 R.string.date_time_auto_summaryOff);
141         mAutoDateTimeButton.setOnCheckedChangeListener(this);
142
143         mTimePicker = (TimePicker)findViewById(R.id.time_picker);
144         mTimePicker.setEnabled(!autoDateTimeEnabled);
145         mDatePicker = (DatePicker)findViewById(R.id.date_picker);
146         mDatePicker.setEnabled(!autoDateTimeEnabled);
147         mDatePicker.setCalendarViewShown(false);
148
149         mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
150
151         ((Button)findViewById(R.id.next_button)).setOnClickListener(this);
152         final Button skipButton = (Button)findViewById(R.id.skip_button);
153         if (skipButton != null) {
154             skipButton.setOnClickListener(this);
155         }
156     }
157
158     @Override
159     public void onResume() {
160         super.onResume();
161         IntentFilter filter = new IntentFilter();
162         filter.addAction(Intent.ACTION_TIME_TICK);
163         filter.addAction(Intent.ACTION_TIME_CHANGED);
164         filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
165         registerReceiver(mIntentReceiver, filter, null, null);
166     }
167
168     @Override
169     public void onPause() {
170         super.onPause();
171         unregisterReceiver(mIntentReceiver);
172     }
173
174     @Override
175     public void onClick(View view) {
176         switch (view.getId()) {
177         case R.id.time_zone_button: {
178             showTimezonePicker(R.id.time_zone_button);
179             break;
180         }
181         case R.id.next_button: {
182             if (mSelectedTimeZone != null) {
183                 final TimeZone systemTimeZone = TimeZone.getDefault();
184                 if (!systemTimeZone.equals(mSelectedTimeZone)) {
185                     Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone.");
186                     final AlarmManager alarm = (AlarmManager)
187                             getSystemService(Context.ALARM_SERVICE);
188                     alarm.setTimeZone(mSelectedTimeZone.getID());
189                 }
190             }
191             if (mAutoDateTimeButton != null) {
192                 Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
193                       mAutoDateTimeButton.isChecked() ? 1 : 0);
194                 if (!mAutoDateTimeButton.isChecked()) {
195                     DateTimeSettings.setDate(mDatePicker.getYear(), mDatePicker.getMonth(),
196                             mDatePicker.getDayOfMonth());
197                     DateTimeSettings.setTime(
198                             mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute());
199                 }
200             }
201         }  // $FALL-THROUGH$
202         case R.id.skip_button: {
203             setResult(RESULT_OK);
204             finish();
205             break;
206         }
207         }
208     }
209
210     @Override
211     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
212         final boolean autoEnabled = isChecked;  // just for readibility.
213         /*if (buttonView == mAutoTimeZoneButton) {
214             // In XL screen, we save all the state only when the next button is pressed.
215             if (!mUsingXLargeLayout) {
216                 Settings.System.putInt(getContentResolver(),
217                         Settings.System.AUTO_TIME_ZONE,
218                         isChecked ? 1 : 0);
219             }
220             mTimeZone.setEnabled(!autoEnabled);
221             if (isChecked) {
222                 findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
223                 findViewById(R.id.zone_picker).setVisibility(View.GONE);
224             }
225         } else */
226         if (buttonView == mAutoDateTimeButton) {
227             Settings.System.putInt(getContentResolver(),
228                     Settings.System.AUTO_TIME,
229                     isChecked ? 1 : 0);
230             mTimePicker.setEnabled(!autoEnabled);
231             mDatePicker.setEnabled(!autoEnabled);
232         }
233         if (autoEnabled) {
234             final View focusedView = getCurrentFocus();
235             if (focusedView != null) {
236                 mInputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
237                 focusedView.clearFocus();
238             }
239         }
240     }
241
242     @Override
243     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
244         final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
245         if (mUsingXLargeLayout) {
246             mSelectedTimeZone = tz;
247             final Calendar now = Calendar.getInstance(tz);
248             if (mTimeZoneButton != null) {
249                 mTimeZoneButton.setText(tz.getDisplayName());
250             }
251             // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
252             mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
253                     now.get(Calendar.DAY_OF_MONTH));
254             mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
255             mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
256         } else {
257             // in prefs mode, we actually change the setting right now, as opposed to waiting
258             // until Next is pressed in xLarge mode
259             final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
260             alarm.setTimeZone(tz.getID());
261             DateTimeSettings settingsFragment = (DateTimeSettings) getFragmentManager().
262                     findFragmentById(R.id.date_time_settings_fragment);
263             settingsFragment.updateTimeAndDateDisplay(this);
264         }
265         mTimeZonePopup.dismiss();
266     }
267
268     /**
269      * If this is called, that means we're in prefs style portrait mode for a large display
270      * and the user has tapped on the time zone preference. If we were a PreferenceActivity,
271      * we'd then launch the timezone fragment in a new activity, but we aren't, and here
272      * on a tablet display, we really want more of a popup picker look' like the one we use
273      * for the xlarge version of this activity. So we just take this opportunity to launch that.
274      *
275      * TODO: For phones, we might want to change this to do the "normal" opening
276      * of the zonepicker fragment in its own activity. Or we might end up just
277      * creating a separate DateTimeSettingsSetupWizardPhone activity that subclasses
278      * PreferenceActivity in the first place to handle all that automatically.
279      */
280     @Override
281     public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
282         showTimezonePicker(R.id.timezone_dropdown_anchor);
283         return true;
284     }
285
286     private void showTimezonePicker(int anchorViewId) {
287         View anchorView = findViewById(anchorViewId);
288         if (anchorView == null) {
289             Log.e(TAG, "Unable to find zone picker anchor view " + anchorViewId);
290             return;
291         }
292         mTimeZonePopup = new ListPopupWindow(this, null);
293         mTimeZonePopup.setWidth(anchorView.getWidth());
294         mTimeZonePopup.setAnchorView(anchorView);
295         mTimeZonePopup.setAdapter(mTimeZoneAdapter);
296         mTimeZonePopup.setOnItemClickListener(this);
297         mTimeZonePopup.setModal(true);
298         mTimeZonePopup.show();
299     }
300
301     private boolean isAutoDateTimeEnabled() {
302         try {
303             return Settings.System.getInt(getContentResolver(), Settings.System.AUTO_TIME) > 0;
304         } catch (SettingNotFoundException e) {
305             return true;
306         }
307     }
308
309     /*
310     private boolean isAutoTimeZoneEnabled() {
311         try {
312             return Settings.System.getInt(getContentResolver(),
313                     Settings.System.AUTO_TIME_ZONE) > 0;
314         } catch (SettingNotFoundException e) {
315             return true;
316         }
317     }*/
318
319     private void updateTimeAndDateDisplay() {
320         if (!mUsingXLargeLayout) {
321             return;
322         }
323         final Calendar now = Calendar.getInstance();
324         mTimeZoneButton.setText(now.getTimeZone().getDisplayName());
325         mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
326                 now.get(Calendar.DAY_OF_MONTH));
327         mTimePicker.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
328         mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
329     }
330
331     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
332         @Override
333         public void onReceive(Context context, Intent intent) {
334             updateTimeAndDateDisplay();
335         }
336     };
337 }