packages\apps\Settings\res\xml\time_zone_prefs.xml
android:title="@string/date_time_select_region"
android:summary="@string/summary_placeholder" />
android:title="@string/date_time_set_timezone_title"
android:summary="@string/summary_placeholder" />
settings:controller="com.android.settings.datetime.timezone.TimeZoneInfoPreferenceController" />
packages\apps\Settings\src\com\android\settings\datetime\timezone\TimeZoneSettings.java
protected int getPreferenceScreenResId() {
return R.xml.time_zone_prefs;
}
packages\apps\Settings\src\com\android\settings\datetime\timezone\RegionSearchPicker.java
//创建Region list view ,并添加item 点击事件
protected BaseTimeZoneAdapter createAdapter(TimeZoneData timeZoneData) { mTimeZoneData = timeZoneData; mAdapter = new BaseTimeZoneAdapter<>(createAdapterItem(timeZoneData.getRegionIds()), this::onListItemClick, getLocale(), false /* showItemSummary */, null /* headerText */); return mAdapter; }
用户点击,保存Region 和Zone
packages\apps\Settings\src\com\android\settings\datetime\timezone\TimeZoneSettings.java
private void saveTimeZone(String regionId, String tzId) { SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit(); if (regionId == null) { editor.remove(PREF_KEY_REGION); } else { editor.putString(PREF_KEY_REGION, regionId); } editor.apply(); getActivity().getSystemService(AlarmManager.class).setTimeZone(tzId); }
libcore/luni/src/main/java/libcore/util/
import libcore.util.CountryTimeZones;
CountryZonesFinder.java
表示所有的区域(Region)
TimeZoneFinder.java 这里通过 getCountryZonesFinder 来创建 CountryZonesFinder
getCountryZonesFinder 处理 /system/usr/share/zoneinfo/tzlookup.xml 这个文件
public CountryZonesFinder getCountryZonesFinder() { CountryZonesLookupExtractor extractor = new CountryZonesLookupExtractor(); try { processXml(extractor); return extractor.getCountryZonesLookup(); } catch (XmlPullParserException | IOException e) { System.logW("Error reading country zones ", e); return null; } }
/system/usr/share/zoneinfo/tzdata
/system/usr/share/zoneinfo/tzlookup.xml 这个文件记录了区域的code 并对应的时区
/**
* @return the country from the system's locale.
*/
protected Country getLocaleCountry() {
Locale defaultLocale = Locale.getDefault();
if (defaultLocale != null) {
return new Country(defaultLocale.getCountry(), Country.COUNTRY_SOURCE_LOCALE);
} else {
return null;
}
}