Merge "Replace @CorePlatformApi APIs usages in TimeZoneInfoPreferenceControllerTest" am: 3166166bdd
am: e6bf1a83d8
am: b9bebd5bd9
am: eca37a8849
Original change: https://android-review.googlesource.com/c/platform/packages/apps/Settings/+/1831314 Change-Id: Ibcbd24530e7e94a2941d840701158457bbcde835
This commit is contained in:
@@ -151,7 +151,7 @@ public class TimeZoneInfo {
|
|||||||
public TimeZoneInfo format(TimeZone timeZone) {
|
public TimeZoneInfo format(TimeZone timeZone) {
|
||||||
String canonicalZoneId = getCanonicalZoneId(timeZone);
|
String canonicalZoneId = getCanonicalZoneId(timeZone);
|
||||||
final TimeZoneNames timeZoneNames = mTimeZoneFormat.getTimeZoneNames();
|
final TimeZoneNames timeZoneNames = mTimeZoneFormat.getTimeZoneNames();
|
||||||
final java.util.TimeZone javaTimeZone = java.util.TimeZone.getTimeZone(canonicalZoneId);
|
final java.util.TimeZone javaTimeZone = toJavaTimeZone(canonicalZoneId);
|
||||||
final CharSequence gmtOffset = ZoneGetter.getGmtOffsetText(mTimeZoneFormat, mLocale,
|
final CharSequence gmtOffset = ZoneGetter.getGmtOffsetText(mTimeZoneFormat, mLocale,
|
||||||
javaTimeZone, mNow);
|
javaTimeZone, mNow);
|
||||||
return new TimeZoneInfo.Builder(timeZone)
|
return new TimeZoneInfo.Builder(timeZone)
|
||||||
@@ -165,15 +165,24 @@ public class TimeZoneInfo {
|
|||||||
.setGmtOffset(gmtOffset)
|
.setGmtOffset(gmtOffset)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String getCanonicalZoneId(TimeZone timeZone) {
|
/* package-private */ java.util.TimeZone getJavaTimeZone() {
|
||||||
final String id = timeZone.getID();
|
String canonicalZoneId = getCanonicalZoneId(mTimeZone);
|
||||||
final String canonicalId = TimeZone.getCanonicalID(id);
|
return toJavaTimeZone(canonicalZoneId);
|
||||||
if (canonicalId != null) {
|
}
|
||||||
return canonicalId;
|
|
||||||
}
|
private static java.util.TimeZone toJavaTimeZone(String canonicalZoneId) {
|
||||||
return id;
|
return java.util.TimeZone.getTimeZone(canonicalZoneId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getCanonicalZoneId(TimeZone timeZone) {
|
||||||
|
final String id = timeZone.getID();
|
||||||
|
final String canonicalId = TimeZone.getCanonicalID(id);
|
||||||
|
if (canonicalId != null) {
|
||||||
|
return canonicalId;
|
||||||
}
|
}
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -20,17 +20,17 @@ import android.content.Context;
|
|||||||
import android.icu.text.DateFormat;
|
import android.icu.text.DateFormat;
|
||||||
import android.icu.text.DisplayContext;
|
import android.icu.text.DisplayContext;
|
||||||
import android.icu.text.SimpleDateFormat;
|
import android.icu.text.SimpleDateFormat;
|
||||||
import android.icu.util.BasicTimeZone;
|
|
||||||
import android.icu.util.Calendar;
|
import android.icu.util.Calendar;
|
||||||
import android.icu.util.TimeZone;
|
import android.icu.util.TimeZone;
|
||||||
import android.icu.util.TimeZoneTransition;
|
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.Preference;
|
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.BasePreferenceController;
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.zone.ZoneOffsetTransition;
|
||||||
|
import java.time.zone.ZoneRules;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class TimeZoneInfoPreferenceController extends BasePreferenceController {
|
public class TimeZoneInfoPreferenceController extends BasePreferenceController {
|
||||||
@@ -86,11 +86,11 @@ public class TimeZoneInfoPreferenceController extends BasePreferenceController {
|
|||||||
return mContext.getString(R.string.zone_info_footer_no_dst, offsetAndName);
|
return mContext.getString(R.string.zone_info_footer_no_dst, offsetAndName);
|
||||||
}
|
}
|
||||||
|
|
||||||
final TimeZoneTransition nextDstTransition = findNextDstTransition(timeZone);
|
final ZoneOffsetTransition nextDstTransition = findNextDstTransition(item);
|
||||||
if (nextDstTransition == null) {
|
if (nextDstTransition == null) { // No future transition
|
||||||
return null;
|
return mContext.getString(R.string.zone_info_footer_no_dst, offsetAndName);
|
||||||
}
|
}
|
||||||
final boolean toDst = nextDstTransition.getTo().getDSTSavings() != 0;
|
final boolean toDst = getDSTSavings(timeZone, nextDstTransition.getInstant()) != 0;
|
||||||
String timeType = toDst ? item.getDaylightName() : item.getStandardName();
|
String timeType = toDst ? item.getDaylightName() : item.getStandardName();
|
||||||
if (timeType == null) {
|
if (timeType == null) {
|
||||||
// Fall back to generic "summer time" and "standard time" if the time zone has no
|
// Fall back to generic "summer time" and "standard time" if the time zone has no
|
||||||
@@ -101,26 +101,37 @@ public class TimeZoneInfoPreferenceController extends BasePreferenceController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
final Calendar transitionTime = Calendar.getInstance(timeZone);
|
final Calendar transitionTime = Calendar.getInstance(timeZone);
|
||||||
transitionTime.setTimeInMillis(nextDstTransition.getTime());
|
transitionTime.setTimeInMillis(nextDstTransition.getInstant().toEpochMilli());
|
||||||
final String date = mDateFormat.format(transitionTime);
|
final String date = mDateFormat.format(transitionTime);
|
||||||
return SpannableUtil.getResourcesText(mContext.getResources(),
|
return SpannableUtil.getResourcesText(mContext.getResources(),
|
||||||
R.string.zone_info_footer, offsetAndName, timeType, date);
|
R.string.zone_info_footer, offsetAndName, timeType, date);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TimeZoneTransition findNextDstTransition(TimeZone timeZone) {
|
private ZoneOffsetTransition findNextDstTransition(TimeZoneInfo timeZoneInfo) {
|
||||||
if (!(timeZone instanceof BasicTimeZone)) {
|
TimeZone timeZone = timeZoneInfo.getTimeZone();
|
||||||
return null;
|
ZoneRules zoneRules = timeZoneInfo.getJavaTimeZone().toZoneId().getRules();
|
||||||
}
|
|
||||||
final BasicTimeZone basicTimeZone = (BasicTimeZone) timeZone;
|
Instant from = mDate.toInstant();
|
||||||
TimeZoneTransition transition = basicTimeZone.getNextTransition(
|
|
||||||
mDate.getTime(), /* inclusive */ false);
|
ZoneOffsetTransition transition;
|
||||||
do {
|
while (true) { // Find next transition with different DST offsets
|
||||||
if (transition.getTo().getDSTSavings() != transition.getFrom().getDSTSavings()) {
|
transition = zoneRules.nextTransition(from);
|
||||||
|
if (transition == null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
transition = basicTimeZone.getNextTransition(
|
Instant to = transition.getInstant();
|
||||||
transition.getTime(), /*inclusive */ false);
|
if (getDSTSavings(timeZone, from) != getDSTSavings(timeZone, to)) {
|
||||||
} while (transition != null);
|
break;
|
||||||
|
}
|
||||||
|
from = to;
|
||||||
|
}
|
||||||
|
|
||||||
return transition;
|
return transition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int getDSTSavings(TimeZone timeZone, Instant instant) {
|
||||||
|
int[] offsets = new int[2];
|
||||||
|
timeZone.getOffset(instant.toEpochMilli(), false /* local time */, offsets);
|
||||||
|
return offsets[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user