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
This commit is contained in:
@@ -14,66 +14,61 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
-->
|
-->
|
||||||
<!-- TODO: too many LinearLayout. -->
|
<!-- TODO: too many LinearLayout. -->
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<RelativeLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="fill_parent"
|
android:layout_height="match_parent"
|
||||||
android:keepScreenOn="true"
|
android:keepScreenOn="true"
|
||||||
android:paddingTop="70dip"
|
android:paddingTop="60dip"
|
||||||
android:paddingBottom="100dip"
|
android:paddingLeft="100dip"
|
||||||
android:paddingLeft="60dip"
|
android:paddingRight="100dip"
|
||||||
android:paddingRight="60dip">
|
android:paddingBottom="40dip">
|
||||||
|
|
||||||
<!-- Title: Set date & time-->
|
<!-- Title: Set date & time-->
|
||||||
<TextView android:id="@+id/title"
|
<TextView
|
||||||
android:layout_width="fill_parent"
|
android:id="@+id/title"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="0"
|
android:layout_alignParentLeft="true"
|
||||||
android:gravity="center"
|
|
||||||
android:layout_marginBottom="30dip"
|
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:textSize="64dip"
|
android:textSize="40dip"
|
||||||
android:textColor="#FF99cc00"
|
android:textColor="#FF99cc00"
|
||||||
android:text="@string/date_and_time_settings_title"/>
|
android:text="@string/date_and_time_settings_title"/>
|
||||||
|
|
||||||
<LinearLayout android:id="@+id/main"
|
<!-- Divider -->
|
||||||
android:orientation="horizontal"
|
<View
|
||||||
android:layout_width="fill_parent"
|
android:id="@+id/top_divider"
|
||||||
android:layout_height="fill_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_weight="1">
|
android:layout_height="1px"
|
||||||
<!-- Left side: time zone setting -->
|
android:layout_below="@id/title"
|
||||||
<RelativeLayout android:layout_width="0px"
|
android:background="@color/divider_color"
|
||||||
android:layout_weight="1"
|
android:layout_marginBottom="5dip" />
|
||||||
android:layout_height="fill_parent"
|
|
||||||
android:layout_marginRight="50dip">
|
|
||||||
<LinearLayout android:id="@+id/timezone"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:layout_alignParentTop="true">
|
|
||||||
|
|
||||||
<!--
|
<LinearLayout
|
||||||
<CheckBox android:id="@+id/time_zone_auto"
|
android:layout_width="match_parent"
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="right|center_horizontal"
|
android:layout_below="@id/top_divider"
|
||||||
android:layout_marginBottom="5dip"
|
android:layout_alignParentLeft="true"
|
||||||
android:textSize="32dip"
|
android:layout_marginTop="60dip"
|
||||||
android:text="@string/time_zone_auto_stub"/> -->
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<!-- left: timezone -->
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="0px"
|
||||||
|
android:layout_weight=".48"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
<!-- text should manually be set. -->
|
<!-- text should manually be set. -->
|
||||||
<Button android:id="@+id/current_time_zone"
|
<Button
|
||||||
android:layout_width="fill_parent"
|
android:id="@+id/time_zone_button"
|
||||||
android:layout_height="wrap_content"
|
android:layout_width="400dip"
|
||||||
android:textSize="24dip"
|
android:layout_height="60dip"
|
||||||
android:layout_alignParentTop="true" />
|
android:textSize="24dip" />
|
||||||
<LinearLayout android:id="@+id/zone_picker"
|
<!-- <LinearLayout android:id="@+id/zone_picker"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="350dip"
|
android:layout_height="350dip"
|
||||||
android:paddingLeft="10dip"
|
|
||||||
android:paddingBottom="20dip"
|
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
android:clickable="true">
|
android:clickable="true">
|
||||||
@@ -81,68 +76,75 @@
|
|||||||
class="com.android.settings.ZonePicker"
|
class="com.android.settings.ZonePicker"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
</LinearLayout> -->
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</LinearLayout>
|
|
||||||
<Button android:id="@+id/skip_button"
|
|
||||||
android:layout_width="300dip"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="0"
|
|
||||||
android:layout_alignParentBottom="true"
|
|
||||||
android:layout_centerHorizontal="true"
|
|
||||||
android:textSize="32dip"
|
|
||||||
android:text="@string/skip_label"/>
|
|
||||||
</RelativeLayout>
|
|
||||||
|
|
||||||
<!-- Right side: date & time setting -->
|
<!-- spacer on center -->
|
||||||
<RelativeLayout android:layout_width="0px"
|
<View
|
||||||
android:layout_weight="1"
|
android:layout_width="0px"
|
||||||
android:layout_height="fill_parent"
|
android:layout_weight=".04"
|
||||||
android:layout_marginLeft="50dip">
|
android:layout_height="0dip"
|
||||||
<LinearLayout android:id="@+id/datetime"
|
android:visibility="invisible" />
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:layout_alignParentTop="true">
|
|
||||||
|
|
||||||
<CheckBox android:id="@+id/date_time_auto"
|
<!-- right: DateTime -->
|
||||||
|
<RelativeLayout
|
||||||
|
android:layout_width="0px"
|
||||||
|
android:layout_weight=".48"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<CheckBox
|
||||||
|
android:id="@+id/date_time_auto"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="right|center_horizontal"
|
android:layout_alignParentTop="true"
|
||||||
android:textSize="32dip"
|
android:gravity="left"
|
||||||
|
android:textSize="22dip"
|
||||||
android:text="@string/date_time_auto" />
|
android:text="@string/date_time_auto" />
|
||||||
|
|
||||||
<LinearLayout android:orientation="horizontal"
|
<!-- Divider -->
|
||||||
android:layout_width="fill_parent"
|
<View
|
||||||
android:layout_height="wrap_content">
|
android:id="@+id/datetime_divider"
|
||||||
<TimePicker android:id="@+id/time_picker"
|
android:layout_width="match_parent"
|
||||||
android:layout_width="0px"
|
android:layout_height="1px"
|
||||||
android:layout_weight=".5"
|
android:layout_below="@id/date_time_auto"
|
||||||
|
android:background="@color/divider_color"
|
||||||
|
android:layout_marginTop="15dip"
|
||||||
|
android:layout_marginBottom="80dip" />
|
||||||
|
|
||||||
|
<TimePicker
|
||||||
|
android:id="@+id/time_picker"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:visibility="visible"/>
|
android:layout_alignParentLeft="true"
|
||||||
<DatePicker android:id="@+id/date_picker"
|
android:layout_below="@id/datetime_divider" />
|
||||||
android:layout_width="0px"
|
|
||||||
android:layout_weight=".5"
|
<DatePicker
|
||||||
android:layout_height="wrap_content"/>
|
android:id="@+id/date_picker"
|
||||||
</LinearLayout>
|
android:layout_width="wrap_content"
|
||||||
</LinearLayout>
|
|
||||||
<Button android:id="@+id/next_button"
|
|
||||||
android:layout_width="300dip"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_weight="0"
|
android:layout_alignParentRight="true"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_below="@id/datetime_divider" />
|
||||||
android:layout_centerHorizontal="true"
|
|
||||||
android:textSize="32dip"
|
|
||||||
android:text="@string/next_label" />
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<LinearLayout android:id="@+id/bottom"
|
<Button
|
||||||
android:orientation="horizontal"
|
android:id="@+id/skip_button"
|
||||||
android:layout_width="fill_parent"
|
android:layout_width="250dip"
|
||||||
android:gravity="center"
|
android:layout_height="80dip"
|
||||||
android:layout_height="wrap_content"
|
android:layout_alignParentBottom="true"
|
||||||
android:layout_weight="0">
|
android:layout_toLeftOf="@+id/next_button"
|
||||||
|
android:layout_marginRight="20dip"
|
||||||
|
android:textSize="24dip"
|
||||||
|
android:text="@string/skip_label"/>
|
||||||
|
|
||||||
</LinearLayout>
|
<Button
|
||||||
</LinearLayout>
|
android:id="@+id/next_button"
|
||||||
|
android:layout_width="250dip"
|
||||||
|
android:layout_height="80dip"
|
||||||
|
android:layout_alignParentBottom="true"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:textSize="24dip"
|
||||||
|
android:text="@string/next_label" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
@@ -30,7 +30,6 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentLeft="true"
|
android:layout_alignParentLeft="true"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:gravity="left"
|
|
||||||
android:textSize="40dip"
|
android:textSize="40dip"
|
||||||
android:textColor="#FF99cc00"
|
android:textColor="#FF99cc00"
|
||||||
android:text="@string/wifi_setup_title"/>
|
android:text="@string/wifi_setup_title"/>
|
||||||
|
@@ -16,30 +16,34 @@
|
|||||||
|
|
||||||
package com.android.settings;
|
package com.android.settings;
|
||||||
|
|
||||||
import com.android.settings.ZonePicker.ZoneSelectionListener;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.AlarmManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.provider.Settings.SettingNotFoundException;
|
import android.provider.Settings.SettingNotFoundException;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.AdapterView.OnItemClickListener;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||||
import android.widget.DatePicker;
|
import android.widget.DatePicker;
|
||||||
|
import android.widget.ListPopupWindow;
|
||||||
|
import android.widget.SimpleAdapter;
|
||||||
import android.widget.TimePicker;
|
import android.widget.TimePicker;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public class DateTimeSettingsSetupWizard extends Activity
|
public class DateTimeSettingsSetupWizard extends Activity
|
||||||
implements OnClickListener, ZoneSelectionListener, OnCheckedChangeListener{
|
implements OnClickListener, OnItemClickListener, OnCheckedChangeListener{
|
||||||
private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
|
private static final String TAG = DateTimeSettingsSetupWizard.class.getSimpleName();
|
||||||
|
|
||||||
// force the first status of auto datetime flag.
|
// force the first status of auto datetime flag.
|
||||||
@@ -51,7 +55,12 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
/* Available only in XL */
|
/* Available only in XL */
|
||||||
private CompoundButton mAutoDateTimeButton;
|
private CompoundButton mAutoDateTimeButton;
|
||||||
// private CompoundButton mAutoTimeZoneButton;
|
// private CompoundButton mAutoTimeZoneButton;
|
||||||
private Button mTimeZone;
|
|
||||||
|
private Button mTimeZoneButton;
|
||||||
|
private ListPopupWindow mTimeZonePopup;
|
||||||
|
private SimpleAdapter mTimeZoneAdapter;
|
||||||
|
private TimeZone mSelectedTimeZone;
|
||||||
|
|
||||||
private TimePicker mTimePicker;
|
private TimePicker mTimePicker;
|
||||||
private DatePicker mDatePicker;
|
private DatePicker mDatePicker;
|
||||||
private InputMethodManager mInputMethodManager;
|
private InputMethodManager mInputMethodManager;
|
||||||
@@ -83,10 +92,12 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
R.string.zone_auto_summaryOff);*/
|
R.string.zone_auto_summaryOff);*/
|
||||||
|
|
||||||
final TimeZone tz = TimeZone.getDefault();
|
final TimeZone tz = TimeZone.getDefault();
|
||||||
mTimeZone = (Button)findViewById(R.id.current_time_zone);
|
mSelectedTimeZone = tz;
|
||||||
mTimeZone.setText(DateTimeSettings.getTimeZoneText(tz));
|
mTimeZoneButton = (Button)findViewById(R.id.time_zone_button);
|
||||||
mTimeZone.setOnClickListener(this);
|
mTimeZoneButton.setText(tz.getDisplayName());
|
||||||
// mTimeZone.setEnabled(!autoTimeZoneEnabled);
|
// mTimeZoneButton.setText(DateTimeSettings.getTimeZoneText(tz));
|
||||||
|
mTimeZoneButton.setOnClickListener(this);
|
||||||
|
mTimeZoneAdapter = ZonePicker.constructTimezoneAdapter(this, false);
|
||||||
|
|
||||||
final boolean autoDateTimeEnabled;
|
final boolean autoDateTimeEnabled;
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
@@ -109,9 +120,6 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
|
|
||||||
mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
|
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.next_button)).setOnClickListener(this);
|
||||||
((Button)findViewById(R.id.skip_button)).setOnClickListener(this);
|
((Button)findViewById(R.id.skip_button)).setOnClickListener(this);
|
||||||
}
|
}
|
||||||
@@ -119,9 +127,14 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
switch (view.getId()) {
|
switch (view.getId()) {
|
||||||
case R.id.current_time_zone: {
|
case R.id.time_zone_button: {
|
||||||
findViewById(R.id.current_time_zone).setVisibility(View.GONE);
|
mTimeZonePopup = new ListPopupWindow(this, null);
|
||||||
findViewById(R.id.zone_picker).setVisibility(View.VISIBLE);
|
mTimeZonePopup.setWidth(mTimeZoneButton.getWidth());
|
||||||
|
mTimeZonePopup.setAnchorView(mTimeZoneButton);
|
||||||
|
mTimeZonePopup.setAdapter(mTimeZoneAdapter);
|
||||||
|
mTimeZonePopup.setOnItemClickListener(this);
|
||||||
|
mTimeZonePopup.setModal(true);
|
||||||
|
mTimeZonePopup.show();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case R.id.next_button: {
|
case R.id.next_button: {
|
||||||
@@ -130,6 +143,15 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
mAutoTimeZoneButton.isChecked() ? 1 : 0); */
|
mAutoTimeZoneButton.isChecked() ? 1 : 0); */
|
||||||
Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
|
Settings.System.putInt(getContentResolver(), Settings.System.AUTO_TIME,
|
||||||
mAutoDateTimeButton.isChecked() ? 1 : 0);
|
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,
|
// 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
|
// so we don't need to save those values there, while in XL, we need to as
|
||||||
// we don't use those Dialogs.
|
// we don't use those Dialogs.
|
||||||
@@ -182,15 +204,18 @@ public class DateTimeSettingsSetupWizard extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onZoneSelected(TimeZone tz) {
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||||
findViewById(R.id.current_time_zone).setVisibility(View.VISIBLE);
|
final TimeZone tz = ZonePicker.obtainTimeZoneFromItem(parent.getItemAtPosition(position));
|
||||||
findViewById(R.id.zone_picker).setVisibility(View.GONE);
|
mSelectedTimeZone = tz;
|
||||||
|
|
||||||
final Calendar now = Calendar.getInstance(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),
|
mDatePicker.updateDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH),
|
||||||
now.get(Calendar.DAY_OF_MONTH));
|
now.get(Calendar.DAY_OF_MONTH));
|
||||||
mTimePicker.setCurrentHour(now.get(Calendar.HOUR));
|
mTimePicker.setCurrentHour(now.get(Calendar.HOUR));
|
||||||
mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
|
mTimePicker.setCurrentMinute(now.get(Calendar.MINUTE));
|
||||||
|
mTimeZonePopup.dismiss();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAutoDateTimeEnabled() {
|
private boolean isAutoDateTimeEnabled() {
|
||||||
|
@@ -55,10 +55,10 @@ public class ZonePicker extends ListFragment {
|
|||||||
public void onZoneSelected(TimeZone tz);
|
public void onZoneSelected(TimeZone tz);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String KEY_ID = "id";
|
private static final String KEY_ID = "id"; // value: String
|
||||||
private static final String KEY_DISPLAYNAME = "name";
|
private static final String KEY_DISPLAYNAME = "name"; // value: String
|
||||||
private static final String KEY_GMT = "gmt";
|
private static final String KEY_GMT = "gmt"; // value: String
|
||||||
private static final String KEY_OFFSET = "offset";
|
private static final String KEY_OFFSET = "offset"; // value: int (Integer)
|
||||||
private static final String XMLTAG_TIMEZONE = "timezone";
|
private static final String XMLTAG_TIMEZONE = "timezone";
|
||||||
|
|
||||||
private static final int HOURS_1 = 60 * 60000;
|
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_TIMEZONE = Menu.FIRST+1;
|
||||||
private static final int MENU_ALPHABETICAL = Menu.FIRST;
|
private static final int MENU_ALPHABETICAL = Menu.FIRST;
|
||||||
|
|
||||||
// Initial focus position
|
|
||||||
private int mDefault;
|
|
||||||
|
|
||||||
private boolean mSortedByTimezone;
|
private boolean mSortedByTimezone;
|
||||||
|
|
||||||
private SimpleAdapter mTimezoneSortedAdapter;
|
private SimpleAdapter mTimezoneSortedAdapter;
|
||||||
@@ -76,38 +73,73 @@ public class ZonePicker extends ListFragment {
|
|||||||
|
|
||||||
private ZoneSelectionListener mListener;
|
private ZoneSelectionListener mListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanseState) {
|
public void onActivityCreated(Bundle savedInstanseState) {
|
||||||
super.onActivityCreated(savedInstanseState);
|
super.onActivityCreated(savedInstanseState);
|
||||||
|
|
||||||
final String[] from = new String[] {KEY_DISPLAYNAME, KEY_GMT};
|
final Activity activity = getActivity();
|
||||||
final int[] to = new int[] {android.R.id.text1, android.R.id.text2};
|
mTimezoneSortedAdapter = constructTimezoneAdapter(activity, false);
|
||||||
|
mAlphabeticalAdapter = constructTimezoneAdapter(activity, true);
|
||||||
MyComparator comparator = new MyComparator(KEY_OFFSET);
|
|
||||||
|
|
||||||
Activity activity = getActivity();
|
|
||||||
List<HashMap> timezoneSortedList = getZones();
|
|
||||||
Collections.sort(timezoneSortedList, comparator);
|
|
||||||
mTimezoneSortedAdapter = new SimpleAdapter(activity,
|
|
||||||
(List) timezoneSortedList,
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Sets the adapter
|
// Sets the adapter
|
||||||
setSorting(true);
|
setSorting(true);
|
||||||
|
|
||||||
// If current timezone is in this list, move focus to it
|
|
||||||
setSelection(mDefault);
|
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,16 +184,22 @@ public class ZonePicker extends ListFragment {
|
|||||||
mListener = listener;
|
mListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setSorting(boolean timezone) {
|
private void setSorting(boolean sortByTimezone) {
|
||||||
setListAdapter(timezone ? mTimezoneSortedAdapter : mAlphabeticalAdapter);
|
final SimpleAdapter adapter =
|
||||||
mSortedByTimezone = timezone;
|
sortByTimezone ? mTimezoneSortedAdapter : mAlphabeticalAdapter;
|
||||||
|
setListAdapter(adapter);
|
||||||
|
mSortedByTimezone = sortByTimezone;
|
||||||
|
final int defaultIndex = getTimeZoneIndex(adapter, TimeZone.getDefault());
|
||||||
|
if (defaultIndex >= 0) {
|
||||||
|
setSelection(defaultIndex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<HashMap> getZones() {
|
private static List<HashMap<String, Object>> getZones(Context context) {
|
||||||
List<HashMap> myData = new ArrayList<HashMap>();
|
final List<HashMap<String, Object>> myData = new ArrayList<HashMap<String, Object>>();
|
||||||
long date = Calendar.getInstance().getTimeInMillis();
|
final long date = Calendar.getInstance().getTimeInMillis();
|
||||||
try {
|
try {
|
||||||
XmlResourceParser xrp = getActivity().getResources().getXml(R.xml.timezones);
|
XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
|
||||||
while (xrp.next() != XmlResourceParser.START_TAG)
|
while (xrp.next() != XmlResourceParser.START_TAG)
|
||||||
continue;
|
continue;
|
||||||
xrp.next();
|
xrp.next();
|
||||||
@@ -192,15 +230,15 @@ public class ZonePicker extends ListFragment {
|
|||||||
return myData;
|
return myData;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addItem(List<HashMap> myData, String id, String displayName,
|
private static void addItem(
|
||||||
long date) {
|
List<HashMap<String, Object>> myData, String id, String displayName, long date) {
|
||||||
HashMap map = new HashMap();
|
final HashMap<String, Object> map = new HashMap<String, Object>();
|
||||||
map.put(KEY_ID, id);
|
map.put(KEY_ID, id);
|
||||||
map.put(KEY_DISPLAYNAME, displayName);
|
map.put(KEY_DISPLAYNAME, displayName);
|
||||||
TimeZone tz = TimeZone.getTimeZone(id);
|
final TimeZone tz = TimeZone.getTimeZone(id);
|
||||||
int offset = tz.getOffset(date);
|
final int offset = tz.getOffset(date);
|
||||||
int p = Math.abs(offset);
|
final int p = Math.abs(offset);
|
||||||
StringBuilder name = new StringBuilder();
|
final StringBuilder name = new StringBuilder();
|
||||||
name.append("GMT");
|
name.append("GMT");
|
||||||
|
|
||||||
if (offset < 0) {
|
if (offset < 0) {
|
||||||
@@ -223,20 +261,17 @@ public class ZonePicker extends ListFragment {
|
|||||||
map.put(KEY_GMT, name.toString());
|
map.put(KEY_GMT, name.toString());
|
||||||
map.put(KEY_OFFSET, offset);
|
map.put(KEY_OFFSET, offset);
|
||||||
|
|
||||||
if (id.equals(TimeZone.getDefault().getID())) {
|
|
||||||
mDefault = myData.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
myData.add(map);
|
myData.add(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
public void onListItemClick(ListView listView, View v, int position, long id) {
|
||||||
Map map = (Map) l.getItemAtPosition(position);
|
final Map<?, ?> map = (Map<?, ?>)listView.getItemAtPosition(position);
|
||||||
|
final String tzId = (String) map.get(KEY_ID);
|
||||||
|
|
||||||
// Update the system timezone value
|
// Update the system timezone value
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
AlarmManager alarm = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
|
final AlarmManager alarm = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
|
||||||
String tzId = (String) map.get(KEY_ID);
|
|
||||||
alarm.setTimeZone(tzId);
|
alarm.setTimeZone(tzId);
|
||||||
final TimeZone tz = TimeZone.getTimeZone(tzId);
|
final TimeZone tz = TimeZone.getTimeZone(tzId);
|
||||||
if (mListener != null) {
|
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;
|
private String mSortingKey;
|
||||||
|
|
||||||
public MyComparator(String sortingKey) {
|
public MyComparator(String sortingKey) {
|
||||||
@@ -257,7 +292,7 @@ public class ZonePicker extends ListFragment {
|
|||||||
mSortingKey = sortingKey;
|
mSortingKey = sortingKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int compare(HashMap map1, HashMap map2) {
|
public int compare(HashMap<?, ?> map1, HashMap<?, ?> map2) {
|
||||||
Object value1 = map1.get(mSortingKey);
|
Object value1 = map1.get(mSortingKey);
|
||||||
Object value2 = map2.get(mSortingKey);
|
Object value2 = map2.get(mSortingKey);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user