android 设置设置中Region & Time Zone

罗祺
2023-12-01

packages\apps\Settings\res\xml\time_zone_prefs.xml


    <PreferenceCategory
        android:key="time_zone_region_preference_category">
        <com.android.settingslib.RestrictedPreference
            android:key="region"
            android:title="@string/date_time_select_region"
            android:summary="@string/summary_placeholder" />
        <com.android.settingslib.RestrictedPreference
            android:key="region_zone"
            android:title="@string/date_time_set_timezone_title"
            android:summary="@string/summary_placeholder" />
        <com.android.settingslib.widget.FooterPreference
            android:key="footer_preference"
            settings:controller="com.android.settings.datetime.timezone.TimeZoneInfoPreferenceController" />
    </PreferenceCategory>

 

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;
    }
}

 

 类似资料: