Move ZoneGetter into SettingsLib

Change-Id: Ia998ae0bd7fc4c974e03df7c8fc380d98a3e71de
This commit is contained in:
Tony Mantler
2015-05-28 14:48:29 -07:00
parent 7c3b9f28b5
commit 550015e476
3 changed files with 16 additions and 226 deletions

View File

@@ -16,12 +16,12 @@
package com.android.settings;
import android.app.admin.DevicePolicyManager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -34,20 +34,15 @@ import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import com.android.internal.logging.MetricsLogger;
import java.text.SimpleDateFormat;
import com.android.internal.logging.MetricsLogger;
import com.android.settingslib.datetime.ZoneGetter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class DateTimeSettings extends SettingsPreferenceFragment
implements OnSharedPreferenceChangeListener,
@@ -170,7 +165,7 @@ public class DateTimeSettings extends SettingsPreferenceFragment
Date dummyDate = mDummyDate.getTime();
mDatePref.setSummary(DateFormat.getLongDateFormat(context).format(now.getTime()));
mTimePref.setSummary(DateFormat.getTimeFormat(getActivity()).format(now.getTime()));
mTimeZone.setSummary(getTimeZoneText(now.getTimeZone(), true));
mTimeZone.setSummary(ZoneGetter.getTimeZoneText(now.getTimeZone(), true));
mTime24Pref.setSummary(DateFormat.getTimeFormat(getActivity()).format(dummyDate));
}
@@ -350,34 +345,6 @@ public class DateTimeSettings extends SettingsPreferenceFragment
}
}
public static String getTimeZoneText(TimeZone tz, boolean includeName) {
Date now = new Date();
// Use SimpleDateFormat to format the GMT+00:00 string.
SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
gmtFormatter.setTimeZone(tz);
String gmtString = gmtFormatter.format(now);
// Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
BidiFormatter bidiFormatter = BidiFormatter.getInstance();
Locale l = Locale.getDefault();
boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL;
gmtString = bidiFormatter.unicodeWrap(gmtString,
isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
if (!includeName) {
return gmtString;
}
// Optionally append the time zone name.
SimpleDateFormat zoneNameFormatter = new SimpleDateFormat("zzzz");
zoneNameFormatter.setTimeZone(tz);
String zoneNameString = zoneNameFormatter.format(now);
// We don't use punctuation here to avoid having to worry about localizing that too!
return gmtString + " " + zoneNameString;
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

View File

@@ -20,9 +20,8 @@ import android.app.Activity;
import android.app.AlarmManager;
import android.app.ListFragment;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@@ -32,21 +31,14 @@ import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.xmlpull.v1.XmlPullParserException;
import com.android.settingslib.datetime.ZoneGetter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import libcore.icu.TimeZoneNames;
/**
* The class displaying a list of time zones that match a filter string
@@ -57,19 +49,11 @@ import libcore.icu.TimeZoneNames;
public class ZonePicker extends ListFragment {
private static final String TAG = "ZonePicker";
public static interface ZoneSelectionListener {
public interface ZoneSelectionListener {
// You can add any argument if you really need it...
public void onZoneSelected(TimeZone tz);
void onZoneSelected(TimeZone tz);
}
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;
private static final int MENU_TIMEZONE = Menu.FIRST+1;
private static final int MENU_ALPHABETICAL = Menu.FIRST;
@@ -98,12 +82,12 @@ public class ZonePicker extends ListFragment {
*/
public static SimpleAdapter constructTimezoneAdapter(Context context,
boolean sortedByName, int layoutId) {
final String[] from = new String[] {KEY_DISPLAYNAME, KEY_GMT};
final String[] from = new String[] {ZoneGetter.KEY_DISPLAYNAME, ZoneGetter.KEY_GMT};
final int[] to = new int[] {android.R.id.text1, android.R.id.text2};
final String sortKey = (sortedByName ? KEY_DISPLAYNAME : KEY_OFFSET);
final String sortKey = (sortedByName ? ZoneGetter.KEY_DISPLAYNAME : ZoneGetter.KEY_OFFSET);
final MyComparator comparator = new MyComparator(sortKey);
ZoneGetter zoneGetter = new ZoneGetter();
final ZoneGetter zoneGetter = new ZoneGetter();
final List<HashMap<String, Object>> sortedList = zoneGetter.getZones(context);
Collections.sort(sortedList, comparator);
final SimpleAdapter adapter = new SimpleAdapter(context,
@@ -131,7 +115,7 @@ public class ZonePicker extends ListFragment {
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);
final String id = (String)map.get(ZoneGetter.KEY_ID);
if (defaultId.equals(id)) {
// If current timezone is in this list, move focus to it
return i;
@@ -146,7 +130,7 @@ public class ZonePicker extends ListFragment {
* @return TimeZone object corresponding to the item.
*/
public static TimeZone obtainTimeZoneFromItem(Object item) {
return TimeZone.getTimeZone((String)((Map<?, ?>)item).get(KEY_ID));
return TimeZone.getTimeZone((String)((Map<?, ?>)item).get(ZoneGetter.KEY_ID));
}
@Override
@@ -163,7 +147,7 @@ public class ZonePicker extends ListFragment {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
final ListView list = (ListView) view.findViewById(android.R.id.list);
@@ -223,82 +207,12 @@ public class ZonePicker extends ListFragment {
}
}
static class ZoneGetter {
private final List<HashMap<String, Object>> mZones =
new ArrayList<HashMap<String, Object>>();
private final HashSet<String> mLocalZones = new HashSet<String>();
private final Date mNow = Calendar.getInstance().getTime();
private final SimpleDateFormat mZoneNameFormatter = new SimpleDateFormat("zzzz");
private List<HashMap<String, Object>> getZones(Context context) {
for (String olsonId : TimeZoneNames.forLocale(Locale.getDefault())) {
mLocalZones.add(olsonId);
}
try {
XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
while (xrp.next() != XmlResourceParser.START_TAG) {
continue;
}
xrp.next();
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
while (xrp.getEventType() != XmlResourceParser.START_TAG) {
if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
return mZones;
}
xrp.next();
}
if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
String olsonId = xrp.getAttributeValue(0);
addTimeZone(olsonId);
}
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 mZones;
}
private void addTimeZone(String olsonId) {
// We always need the "GMT-07:00" string.
final TimeZone tz = TimeZone.getTimeZone(olsonId);
// For the display name, we treat time zones within the country differently
// from other countries' time zones. So in en_US you'd get "Pacific Daylight Time"
// but in de_DE you'd get "Los Angeles" for the same time zone.
String displayName;
if (mLocalZones.contains(olsonId)) {
// Within a country, we just use the local name for the time zone.
mZoneNameFormatter.setTimeZone(tz);
displayName = mZoneNameFormatter.format(mNow);
} else {
// For other countries' time zones, we use the exemplar location.
final String localeName = Locale.getDefault().toString();
displayName = TimeZoneNames.getExemplarLocation(localeName, olsonId);
}
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(KEY_ID, olsonId);
map.put(KEY_DISPLAYNAME, displayName);
map.put(KEY_GMT, DateTimeSettings.getTimeZoneText(tz, false));
map.put(KEY_OFFSET, tz.getOffset(mNow.getTime()));
mZones.add(map);
}
}
@Override
public void onListItemClick(ListView listView, View v, int position, long id) {
// Ignore extra clicks
if (!isResumed()) return;
final Map<?, ?> map = (Map<?, ?>)listView.getItemAtPosition(position);
final String tzId = (String) map.get(KEY_ID);
final String tzId = (String) map.get(ZoneGetter.KEY_ID);
// Update the system timezone value
final Activity activity = getActivity();