Replace @CorePlatformApi APIs usages in TimeZoneInfoPreferenceControllerTest
The time zone transitions can be obtained via the public API
ZoneId.getRules() instead.
Bug: 119751170
Test: atest SettingsRoboTests
com.android.settings.datetime.timezone.TimeZoneInfoPreferenceControllerTest passes
Change-Id: I2c7864580b2a36b725ee250253e97f6cc86d72a4
diff --git a/src/com/android/settings/datetime/timezone/TimeZoneInfo.java b/src/com/android/settings/datetime/timezone/TimeZoneInfo.java
index f74614d..f9e819c 100644
--- a/src/com/android/settings/datetime/timezone/TimeZoneInfo.java
+++ b/src/com/android/settings/datetime/timezone/TimeZoneInfo.java
@@ -151,7 +151,7 @@
public TimeZoneInfo format(TimeZone timeZone) {
String canonicalZoneId = getCanonicalZoneId(timeZone);
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,
javaTimeZone, mNow);
return new TimeZoneInfo.Builder(timeZone)
@@ -165,15 +165,24 @@
.setGmtOffset(gmtOffset)
.build();
}
+ }
- private static String getCanonicalZoneId(TimeZone timeZone) {
- final String id = timeZone.getID();
- final String canonicalId = TimeZone.getCanonicalID(id);
- if (canonicalId != null) {
- return canonicalId;
- }
- return id;
+ /* package-private */ java.util.TimeZone getJavaTimeZone() {
+ String canonicalZoneId = getCanonicalZoneId(mTimeZone);
+ return toJavaTimeZone(canonicalZoneId);
+ }
+
+ private static java.util.TimeZone toJavaTimeZone(String canonicalZoneId) {
+ 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;
}
}
diff --git a/src/com/android/settings/datetime/timezone/TimeZoneInfoPreferenceController.java b/src/com/android/settings/datetime/timezone/TimeZoneInfoPreferenceController.java
index 80cefb6..c6ac328 100644
--- a/src/com/android/settings/datetime/timezone/TimeZoneInfoPreferenceController.java
+++ b/src/com/android/settings/datetime/timezone/TimeZoneInfoPreferenceController.java
@@ -20,17 +20,17 @@
import android.icu.text.DateFormat;
import android.icu.text.DisplayContext;
import android.icu.text.SimpleDateFormat;
-import android.icu.util.BasicTimeZone;
import android.icu.util.Calendar;
import android.icu.util.TimeZone;
-import android.icu.util.TimeZoneTransition;
import androidx.annotation.VisibleForTesting;
-import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
+import java.time.Instant;
+import java.time.zone.ZoneOffsetTransition;
+import java.time.zone.ZoneRules;
import java.util.Date;
public class TimeZoneInfoPreferenceController extends BasePreferenceController {
@@ -86,11 +86,11 @@
return mContext.getString(R.string.zone_info_footer_no_dst, offsetAndName);
}
- final TimeZoneTransition nextDstTransition = findNextDstTransition(timeZone);
- if (nextDstTransition == null) {
- return null;
+ final ZoneOffsetTransition nextDstTransition = findNextDstTransition(item);
+ if (nextDstTransition == null) { // No future transition
+ 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();
if (timeType == null) {
// Fall back to generic "summer time" and "standard time" if the time zone has no
@@ -101,26 +101,37 @@
}
final Calendar transitionTime = Calendar.getInstance(timeZone);
- transitionTime.setTimeInMillis(nextDstTransition.getTime());
+ transitionTime.setTimeInMillis(nextDstTransition.getInstant().toEpochMilli());
final String date = mDateFormat.format(transitionTime);
return SpannableUtil.getResourcesText(mContext.getResources(),
R.string.zone_info_footer, offsetAndName, timeType, date);
}
- private TimeZoneTransition findNextDstTransition(TimeZone timeZone) {
- if (!(timeZone instanceof BasicTimeZone)) {
- return null;
- }
- final BasicTimeZone basicTimeZone = (BasicTimeZone) timeZone;
- TimeZoneTransition transition = basicTimeZone.getNextTransition(
- mDate.getTime(), /* inclusive */ false);
- do {
- if (transition.getTo().getDSTSavings() != transition.getFrom().getDSTSavings()) {
+ private ZoneOffsetTransition findNextDstTransition(TimeZoneInfo timeZoneInfo) {
+ TimeZone timeZone = timeZoneInfo.getTimeZone();
+ ZoneRules zoneRules = timeZoneInfo.getJavaTimeZone().toZoneId().getRules();
+
+ Instant from = mDate.toInstant();
+
+ ZoneOffsetTransition transition;
+ while (true) { // Find next transition with different DST offsets
+ transition = zoneRules.nextTransition(from);
+ if (transition == null) {
break;
}
- transition = basicTimeZone.getNextTransition(
- transition.getTime(), /*inclusive */ false);
- } while (transition != null);
+ Instant to = transition.getInstant();
+ if (getDSTSavings(timeZone, from) != getDSTSavings(timeZone, to)) {
+ break;
+ }
+ from = to;
+ }
+
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];
+ }
}