use of android.timezone.CountryTimeZones in project android_frameworks_opt_telephony by LineageOS.
the class TimeZoneLookupHelper method lookupByCountry.
/**
* Returns information about the time zones used in a country at a given time.
*
* {@code null} can be returned if a problem occurs during lookup, e.g. if the country code is
* unrecognized, if the country is uninhabited, or if there is a problem with the data.
*/
@VisibleForTesting
@Nullable
public CountryResult lookupByCountry(@NonNull String isoCountryCode, long whenMillis) {
CountryTimeZones countryTimeZones = getCountryTimeZones(isoCountryCode);
if (countryTimeZones == null) {
// Unknown country code.
return null;
}
TimeZone countryDefaultZone = countryTimeZones.getDefaultTimeZone();
if (countryDefaultZone == null) {
// This is not expected: the country default should have been validated before.
return null;
}
String debugInfo;
int matchQuality;
if (countryTimeZones.isDefaultTimeZoneBoosted()) {
matchQuality = CountryResult.QUALITY_DEFAULT_BOOSTED;
debugInfo = "Country default is boosted";
} else {
List<TimeZoneMapping> effectiveTimeZoneMappings = countryTimeZones.getEffectiveTimeZoneMappingsAt(whenMillis);
if (effectiveTimeZoneMappings.isEmpty()) {
// This should never happen unless there's been an error loading the data.
// Treat it the same as a low quality answer.
matchQuality = QUALITY_MULTIPLE_ZONES_DIFFERENT_OFFSETS;
debugInfo = "No effective time zones found at whenMillis=" + whenMillis;
} else if (effectiveTimeZoneMappings.size() == 1) {
// The default is the only zone so it's a good candidate.
matchQuality = CountryResult.QUALITY_SINGLE_ZONE;
debugInfo = "One effective time zone found at whenMillis=" + whenMillis;
} else {
boolean countryUsesDifferentOffsets = countryUsesDifferentOffsets(whenMillis, effectiveTimeZoneMappings, countryDefaultZone);
matchQuality = countryUsesDifferentOffsets ? QUALITY_MULTIPLE_ZONES_DIFFERENT_OFFSETS : QUALITY_MULTIPLE_ZONES_SAME_OFFSET;
debugInfo = "countryUsesDifferentOffsets=" + countryUsesDifferentOffsets + " at" + " whenMillis=" + whenMillis;
}
}
return new CountryResult(countryDefaultZone.getID(), matchQuality, debugInfo);
}
use of android.timezone.CountryTimeZones in project android_frameworks_opt_telephony by LineageOS.
the class TimeZoneLookupHelper method lookupByNitzCountry.
/**
* Looks for a time zone for the supplied NITZ and country information.
*
* <p><em>Note:</em> When there are multiple matching zones then one of the matching candidates
* will be returned in the result. If the current device default zone matches it will be
* returned in preference to other candidates. This method can return {@code null} if no
* matching time zones are found.
*/
@VisibleForTesting
@Nullable
public OffsetResult lookupByNitzCountry(@NonNull NitzData nitzData, @NonNull String isoCountryCode) {
CountryTimeZones countryTimeZones = getCountryTimeZones(isoCountryCode);
if (countryTimeZones == null) {
return null;
}
TimeZone bias = TimeZone.getDefault();
// Android NITZ time zone matching doesn't try to do a precise match using the DST offset
// supplied by the carrier. It only considers whether or not the carrier suggests local time
// is DST (if known). NITZ is limited in only being able to express DST offsets in whole
// hours and the DST info is optional.
Integer dstAdjustmentMillis = nitzData.getDstAdjustmentMillis();
if (dstAdjustmentMillis == null) {
return countryTimeZones.lookupByOffsetWithBias(nitzData.getCurrentTimeInMillis(), bias, nitzData.getLocalOffsetMillis());
} else {
// We don't try to match the exact DST offset given, we just use it to work out if
// the country is in DST.
boolean isDst = dstAdjustmentMillis != 0;
return countryTimeZones.lookupByOffsetWithBias(nitzData.getCurrentTimeInMillis(), bias, nitzData.getLocalOffsetMillis(), isDst);
}
}
Aggregations