ZoneListpublic class ZoneList extends android.app.ListActivity This activity displays a list of time zones that match a filter string
such as "Africa", "Europe", etc. Choosing an item from the list will set
the time zone. Pressing Back without choosing from the list will not
result in a change in the time zone setting. |
Fields Summary |
---|
private static final String | TAG | private static final String | KEY_ID | private static final String | KEY_DISPLAYNAME | private static final String | KEY_GMT | private static final String | KEY_OFFSET | private static final String | XMLTAG_TIMEZONE | private static final int | HOURS_1 | private static final int | HOURS_24 | private static final int | HOURS_HALF | private static final int | MENU_TIMEZONE | private static final int | MENU_ALPHABETICAL | private int | mDefault | private boolean | mSortedByTimezone | private android.widget.SimpleAdapter | mTimezoneSortedAdapter | private android.widget.SimpleAdapter | mAlphabeticalAdapter |
Methods Summary |
---|
protected void | addItem(java.util.List myData, java.lang.String id, java.lang.String displayName, long date)
HashMap map = new HashMap();
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();
name.append("GMT");
if (offset < 0) {
name.append('-");
} else {
name.append('+");
}
name.append(p / (HOURS_1));
name.append(':");
int min = p / 60000;
min %= 60;
if (min < 10) {
name.append('0");
}
name.append(min);
map.put(KEY_GMT, name.toString());
map.put(KEY_OFFSET, offset);
if (id.equals(TimeZone.getDefault().getID())) {
mDefault = myData.size();
}
myData.add(map);
| private java.util.List | getZones()
List<HashMap> myData = new ArrayList<HashMap>();
long date = Calendar.getInstance().getTimeInMillis();
try {
XmlResourceParser xrp = getResources().getXml(R.xml.timezones);
while (xrp.next() != XmlResourceParser.START_TAG)
;
xrp.next();
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
while (xrp.getEventType() != XmlResourceParser.START_TAG) {
if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
return myData;
}
xrp.next();
}
if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
String id = xrp.getAttributeValue(0);
String displayName = xrp.nextText();
addItem(myData, id, displayName, date);
}
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
xrp.next();
}
xrp.next();
}
xrp.close();
} catch (XmlPullParserException xppe) {
Log.e(TAG, "Ill-formatted timezones.xml file");
} catch (java.io.IOException ioe) {
Log.e(TAG, "Unable to read timezones.xml file");
}
return myData;
| public void | onCreate(android.os.Bundle icicle)
super.onCreate(icicle);
String[] from = new String[] {KEY_DISPLAYNAME, KEY_GMT};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
MyComparator comparator = new MyComparator(KEY_OFFSET);
List<HashMap> timezoneSortedList = getZones();
Collections.sort(timezoneSortedList, comparator);
mTimezoneSortedAdapter = new SimpleAdapter(this,
(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(this,
(List) alphabeticalList,
android.R.layout.simple_list_item_2,
from,
to);
// Sets the adapter
setSorting(true);
// If current timezone is in this list, move focus to it
setSelection(mDefault);
// Assume user may press Back
setResult(RESULT_CANCELED);
| public boolean | onCreateOptionsMenu(android.view.Menu menu)
menu.add(0, MENU_ALPHABETICAL, 0, R.string.zone_list_menu_sort_alphabetically)
.setIcon(android.R.drawable.ic_menu_sort_alphabetically);
menu.add(0, MENU_TIMEZONE, 0, R.string.zone_list_menu_sort_by_timezone)
.setIcon(R.drawable.ic_menu_3d_globe);
return true;
| protected void | onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
Map map = (Map) l.getItemAtPosition(position);
// Update the system timezone value
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setTimeZone((String) map.get(KEY_ID));
setResult(RESULT_OK);
finish();
| public boolean | onOptionsItemSelected(android.view.MenuItem item)
switch (item.getItemId()) {
case MENU_TIMEZONE:
setSorting(true);
return true;
case MENU_ALPHABETICAL:
setSorting(false);
return true;
default:
return false;
}
| public boolean | onPrepareOptionsMenu(android.view.Menu menu)
if (mSortedByTimezone) {
menu.findItem(MENU_TIMEZONE).setVisible(false);
menu.findItem(MENU_ALPHABETICAL).setVisible(true);
} else {
menu.findItem(MENU_TIMEZONE).setVisible(true);
menu.findItem(MENU_ALPHABETICAL).setVisible(false);
}
return true;
| private void | setSorting(boolean timezone)
setListAdapter(timezone ? mTimezoneSortedAdapter : mAlphabeticalAdapter);
mSortedByTimezone = timezone;
|
|