diff options
| author | Daisuke Miyakawa <dmiyakawa@google.com> | 2010-10-29 15:01:56 -0700 |
|---|---|---|
| committer | Daisuke Miyakawa <dmiyakawa@google.com> | 2010-11-16 09:44:47 -0800 |
| commit | 2571f0dcb1cc96b7220687aff6fcddc623b57c3c (patch) | |
| tree | aeb1fd7dc47e01a734a7648c709b19065a2e92d9 /src/com/android | |
| parent | 9a7c56010eed8ce441e190ae8f9e4d4e8315ab86 (diff) | |
| download | packages_apps_Settings-2571f0dcb1cc96b7220687aff6fcddc623b57c3c.tar.gz packages_apps_Settings-2571f0dcb1cc96b7220687aff6fcddc623b57c3c.tar.bz2 packages_apps_Settings-2571f0dcb1cc96b7220687aff6fcddc623b57c3c.zip | |
Refactor DateTimeSettingsSetup.
- Use Popup instead of bare fragment.
- Expose Adapters in ZonePicker.java so that it can be used
outside the fragment.
- Fix layout
Bug: 3175603
Change-Id: I2726fde4fa1a9aea1ecb29b6aa2d23dbc54232b9
Diffstat (limited to 'src/com/android')
| -rw-r--r-- | src/com/android/settings/DateTimeSettingsSetupWizard.java | 61 | ||||
| -rw-r--r-- | src/com/android/settings/ZonePicker.java | 141 |
2 files changed, 131 insertions, 71 deletions
diff --git a/src/com/android/settings/DateTimeSettingsSetupWizard.java b/src/com/android/settings/DateTimeSettingsSetupWizard.java index 92bcf7d6c..ad529f946 100644 --- a/src/com/android/settings/DateTimeSettingsSetupWizard.java +++ b/src/com/android/settings/DateTimeSettingsSetupWizard.java @@ -16,30 +16,34 @@ package com.android.settings; -import com.android.settings.ZonePicker.ZoneSelectionListener; - import android.app.Activity; +import android.app.AlarmManager; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; +import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.inputmethod.InputMethodManager; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.DatePicker; +import android.widget.ListPopupWindow; +import android.widget.SimpleAdapter; import android.widget.TimePicker; import java.util.Calendar; import java.util.TimeZone; public class DateTimeSettingsSetupWizard extends Activity - implements OnClickListener, ZoneSelectionListener, OnCheckedChangeListener{ + implements OnClickListener, OnItemClickListener, OnCheckedChangeListener{ private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName(); // force the first status of auto datetime flag. @@ -51,7 +55,12 @@ public class DateTimeSettingsSetupWizard extends Activity /* Available only in XL */ private CompoundButton mAutoDateTimeButton; // private CompoundButton mAutoTimeZoneButton; - private Button mTimeZone; + + private Button mTimeZoneButton; + private ListPopupWindow mTimeZonePopup; + private SimpleAdapter mTimeZoneAdapter; + private TimeZone mSelectedTimeZone; + private TimePicker mTimePicker; private DatePicker mDatePicker; private InputMethodManager mInputMethodManager; @@ -83,10 +92,12 @@ public class DateTimeSettingsSetupWizard extends Activity R.string.zone_auto_summaryOff);*/ final TimeZone tz = TimeZone.getDefault(); - mTimeZone = (Button)findViewById(R.id.current_time_zone); - mTimeZone.setText(DateTimeSettings.getTimeZoneText(tz)); - mTimeZone.setOnClickListener(this); - // mTimeZone.setEnabled(!autoTimeZoneEnabled); + mSelectedTimeZone = tz; + mTimeZoneButton = (Button)findViewById(R.id.time_zone_button); + mTimeZoneButton.setText(tz.getDisplayName()); + // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz)); + mTimeZoneButton.setOnClickListener(this); + mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false); final boolean autoDateTimeEnabled; final Intent intent = getIntent(); @@ -109,9 +120,6 @@ public class DateTimeSettingsSetupWizard extends Activity mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); - ((ZonePicker)getFragmentManager().findFragmentById(R.id.zone_picker_fragment)) - .setZoneSelectionListener(this); - ((Button)findViewById(R.id.next_button)).setOnClickListener(this); ((Button)findViewById(R.id.skip_button)).setOnClickListener(this); } @@ -119,9 +127,14 @@ public class DateTimeSettingsSetupWizard extends Activity @Override public void onClick(View view) { switch (view.getId()) { - case R.id.current_time_zone: { - findViewById(R.id.current_time_zone).setVisibility(View.GONE); - findViewById(R.id.zone_picker).setVisibility(View.VISIBLE); + case R.id.time_zone_button: { + mTimeZonePopup = new ListPopupWindow(this, null); + mTimeZonePopup.setWidth(mTimeZoneButton.getWidth()); + mTimeZonePopup.setAnchorView(mTimeZoneButton); + mTimeZonePopup.setAdapter(mTimeZoneAdapter); + mTimeZonePopup.setOnItemClickListener(this); + mTimeZonePopup.setModal(true); + mTimeZonePopup.show(); break; } case R.id.next_button: { @@ -130,6 +143,15 @@ public class DateTimeSettingsSetupWizard extends Activity mAutoTimeZoneButton.isChecked() ? 1 : 0); */ Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME, mAutoDateTimeButton.isChecked() ? 1 : 0); + + final TimeZone systemTimeZone = TimeZone.getDefault(); + if (!systemTimeZone.equals(mSelectedTimeZone)) { + Log.i(TAG, "Another TimeZone is selected by a user. Changing system TimeZone."); + final AlarmManager alarm = (AlarmManager) + getSystemService(Context.ALARM_SERVICE); + alarm.setTimeZone(mSelectedTimeZone.getID()); + } + // Note: in non-XL, Date & Time is stored by DatePickerDialog/TimePickerDialog, // so we don't need to save those values there, while in XL, we need to as // we don't use those Dialogs. @@ -182,15 +204,18 @@ public class DateTimeSettingsSetupWizard extends Activity } @Override - public void onZoneSelected(TimeZone tz) { - findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE); - findViewById(R.id.zone_picker).setVisibility(View.GONE); + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position)); + mSelectedTimeZone = tz; + final Calendar now = Calendar.getInstance(tz); - mTimeZone.setText(DateTimeSettings.getTimeZoneText(tz)); + mTimeZoneButton.setText(tz.getDisplayName()); + // mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz)); mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)); mTimePicker.setCurrentHour(now.get(Calendar.HOUR)); mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE)); + mTimeZonePopup.dismiss(); } private boolean isAutoDateTimeEnabled() { diff --git a/src/com/android/settings/ZonePicker.java b/src/com/android/settings/ZonePicker.java index 3de409045..83d46cb35 100644 --- a/src/com/android/settings/ZonePicker.java +++ b/src/com/android/settings/ZonePicker.java @@ -55,10 +55,10 @@ public class ZonePicker extends ListFragment { public void onZoneSelected(TimeZone tz); } - private static final String KEY_ID = "id"; - private static final String KEY_DISPLAYNAME = "name"; - private static final String KEY_GMT = "gmt"; - private static final String KEY_OFFSET = "offset"; + private static final String KEY_ID = "id"; // value: String + private static final String KEY_DISPLAYNAME = "name"; // value: String + private static final String KEY_GMT = "gmt"; // value: String + private static final String KEY_OFFSET = "offset"; // value: int (Integer) private static final String XMLTAG_TIMEZONE = "timezone"; private static final int HOURS_1 = 60 * 60000; @@ -66,9 +66,6 @@ public class ZonePicker extends ListFragment { private static final int MENU_TIMEZONE = Menu.FIRST+1; private static final int MENU_ALPHABETICAL = Menu.FIRST; - // Initial focus position - private int mDefault; - private boolean mSortedByTimezone; private SimpleAdapter mTimezoneSortedAdapter; @@ -76,38 +73,73 @@ public class ZonePicker extends ListFragment { private ZoneSelectionListener mListener; - @Override - public void onActivityCreated(Bundle savedInstanseState) { - super.onActivityCreated(savedInstanseState); - + /** + * Constructs an adapter with TimeZone list. Sorted by TimeZone in default. + * + * @param sortedByName use Name for sorting the list. + */ + public static SimpleAdapter constructTimezoneAdapter(Context context, + boolean sortedByName) { final String[] from = new String[] {KEY_DISPLAYNAME, KEY_GMT}; final int[] to = new int[] {android.R.id.text1, android.R.id.text2}; - MyComparator comparator = new MyComparator(KEY_OFFSET); - - Activity activity = getActivity(); - List<HashMap> timezoneSortedList = getZones(); - Collections.sort(timezoneSortedList, comparator); - mTimezoneSortedAdapter = new SimpleAdapter(activity, - (List) timezoneSortedList, + final String sortKey = (sortedByName ? KEY_DISPLAYNAME : KEY_OFFSET); + final MyComparator comparator = new MyComparator(sortKey); + final List<HashMap<String, Object>> sortedList = getZones(context); + Collections.sort(sortedList, comparator); + final SimpleAdapter adapter = new SimpleAdapter(context, + sortedList, android.R.layout.simple_list_item_2, from, to); - List<HashMap> alphabeticalList = new ArrayList<HashMap>(timezoneSortedList); - comparator.setSortingKey(KEY_DISPLAYNAME); - Collections.sort(alphabeticalList, comparator); - mAlphabeticalAdapter = new SimpleAdapter(getActivity(), - (List) alphabeticalList, - android.R.layout.simple_list_item_2, - from, - to); + return adapter; + } + + /** + * Searches {@link TimeZone} from the given {@link SimpleAdapter} object, and returns + * the index for the TimeZone. + * + * @param adapter SimpleAdapter constructed by + * {@link #constructTimezoneAdapter(Context, boolean)}. + * @param tz TimeZone to be searched. + * @return Index for the given TimeZone. -1 when there's no corresponding list item. + * returned. + */ + public static int getTimeZoneIndex(SimpleAdapter adapter, TimeZone tz) { + final String defaultId = tz.getID(); + final int listSize = adapter.getCount(); + for (int i = 0; i < listSize; i++) { + // Using HashMap<String, Object> induces unnecessary warning. + final HashMap<?,?> map = (HashMap<?,?>)adapter.getItem(i); + final String id = (String)map.get(KEY_ID); + if (defaultId.equals(id)) { + // If current timezone is in this list, move focus to it + return i; + } + } + return -1; + } + + /** + * @param item one of items in adapters. The adapter should be constructed by + * {@link #constructTimezoneAdapter(Context, boolean)}. + * @return TimeZone object corresponding to the item. + */ + public static TimeZone obtainTimeZoneFromItem(Object item) { + return TimeZone.getTimeZone((String)((Map<?, ?>)item).get(KEY_ID)); + } + + @Override + public void onActivityCreated(Bundle savedInstanseState) { + super.onActivityCreated(savedInstanseState); + + final Activity activity = getActivity(); + mTimezoneSortedAdapter = constructTimezoneAdapter(activity, false); + mAlphabeticalAdapter = constructTimezoneAdapter(activity, true); // Sets the adapter setSorting(true); - - // If current timezone is in this list, move focus to it - setSelection(mDefault); setHasOptionsMenu(true); } @@ -152,16 +184,22 @@ public class ZonePicker extends ListFragment { mListener = listener; } - private void setSorting(boolean timezone) { - setListAdapter(timezone ? mTimezoneSortedAdapter : mAlphabeticalAdapter); - mSortedByTimezone = timezone; + private void setSorting(boolean sortByTimezone) { + final SimpleAdapter adapter = + sortByTimezone ? mTimezoneSortedAdapter : mAlphabeticalAdapter; + setListAdapter(adapter); + mSortedByTimezone = sortByTimezone; + final int defaultIndex = getTimeZoneIndex(adapter, TimeZone.getDefault()); + if (defaultIndex >= 0) { + setSelection(defaultIndex); + } } - private List<HashMap> getZones() { - List<HashMap> myData = new ArrayList<HashMap>(); - long date = Calendar.getInstance().getTimeInMillis(); + private static List<HashMap<String, Object>> getZones(Context context) { + final List<HashMap<String, Object>> myData = new ArrayList<HashMap<String, Object>>(); + final long date = Calendar.getInstance().getTimeInMillis(); try { - XmlResourceParser xrp = getActivity().getResources().getXml(R.xml.timezones); + XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones); while (xrp.next() != XmlResourceParser.START_TAG) continue; xrp.next(); @@ -192,15 +230,15 @@ public class ZonePicker extends ListFragment { return myData; } - protected void addItem(List<HashMap> myData, String id, String displayName, - long date) { - HashMap map = new HashMap(); + private static void addItem( + List<HashMap<String, Object>> myData, String id, String displayName, long date) { + final HashMap<String, Object> map = new HashMap<String, Object>(); map.put(KEY_ID, id); map.put(KEY_DISPLAYNAME, displayName); - TimeZone tz = TimeZone.getTimeZone(id); - int offset = tz.getOffset(date); - int p = Math.abs(offset); - StringBuilder name = new StringBuilder(); + final TimeZone tz = TimeZone.getTimeZone(id); + final int offset = tz.getOffset(date); + final int p = Math.abs(offset); + final StringBuilder name = new StringBuilder(); name.append("GMT"); if (offset < 0) { @@ -223,20 +261,17 @@ public class ZonePicker extends ListFragment { map.put(KEY_GMT, name.toString()); map.put(KEY_OFFSET, offset); - if (id.equals(TimeZone.getDefault().getID())) { - mDefault = myData.size(); - } - myData.add(map); } @Override - public void onListItemClick(ListView l, View v, int position, long id) { - Map map = (Map) l.getItemAtPosition(position); + public void onListItemClick(ListView listView, View v, int position, long id) { + final Map<?, ?> map = (Map<?, ?>)listView.getItemAtPosition(position); + final String tzId = (String) map.get(KEY_ID); + // Update the system timezone value final Activity activity = getActivity(); - AlarmManager alarm = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE); - String tzId = (String) map.get(KEY_ID); + final AlarmManager alarm = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE); alarm.setTimeZone(tzId); final TimeZone tz = TimeZone.getTimeZone(tzId); if (mListener != null) { @@ -246,7 +281,7 @@ public class ZonePicker extends ListFragment { } } - private static class MyComparator implements Comparator<HashMap> { + private static class MyComparator implements Comparator<HashMap<?, ?>> { private String mSortingKey; public MyComparator(String sortingKey) { @@ -257,7 +292,7 @@ public class ZonePicker extends ListFragment { mSortingKey = sortingKey; } - public int compare(HashMap map1, HashMap map2) { + public int compare(HashMap<?, ?> map1, HashMap<?, ?> map2) { Object value1 = map1.get(mSortingKey); Object value2 = map2.get(mSortingKey); |
