Use 24-hour settings for time formatting
Test: m RunSettingsRoboTests
Bug: 72311296
Change-Id: I7c1458dbc2e4e15765f703c35a0113341b912a2b
diff --git a/src/com/android/settings/datetime/timezone/TimeZoneAdapter.java b/src/com/android/settings/datetime/timezone/TimeZoneAdapter.java
index 79075ca..62fc8c9 100644
--- a/src/com/android/settings/datetime/timezone/TimeZoneAdapter.java
+++ b/src/com/android/settings/datetime/timezone/TimeZoneAdapter.java
@@ -35,6 +35,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.Locale;
/**
* Adapter for showing {@link TimeZoneInfo} objects in a recycler view.
@@ -55,7 +56,11 @@
TimeZoneAdapter(View.OnClickListener onClickListener, Context context) {
mOnClickListener = onClickListener;
mContext = context;
- mTimeFormat = DateFormat.getTimeInstance(SimpleDateFormat.SHORT);
+ // Use android.text.format.DateFormat to observe 24-hour settings and find the best pattern
+ // using ICU with skeleton.
+ mTimeFormat = new SimpleDateFormat(
+ android.text.format.DateFormat.getTimeFormatString(context),
+ Locale.getDefault());
mDateFormat = DateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
mDateFormat.setContext(DisplayContext.CAPITALIZATION_NONE);
mCurrentTimeZone = TimeZone.getDefault().getID();
diff --git a/tests/robotests/src/com/android/settings/datetime/timezone/TimeZoneAdapterTest.java b/tests/robotests/src/com/android/settings/datetime/timezone/TimeZoneAdapterTest.java
index 5f29a0b..1377f1d 100644
--- a/tests/robotests/src/com/android/settings/datetime/timezone/TimeZoneAdapterTest.java
+++ b/tests/robotests/src/com/android/settings/datetime/timezone/TimeZoneAdapterTest.java
@@ -15,13 +15,18 @@
*/
package com.android.settings.datetime.timezone;
+import android.content.Context;
import android.icu.util.TimeZone;
+import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
+
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
+
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -29,8 +34,11 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
+import org.robolectric.annotation.Implementation;
+import org.robolectric.annotation.Implements;
import java.util.Collections;
+import java.util.Locale;
import static com.google.common.truth.Truth.assertThat;
@@ -38,17 +46,39 @@
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {
SettingsShadowResources.class,
- SettingsShadowResources.SettingsShadowTheme.class})
+ SettingsShadowResources.SettingsShadowTheme.class,
+ TimeZoneAdapterTest.ShadowDataFormat.class})
public class TimeZoneAdapterTest {
@Mock
private View.OnClickListener mOnClickListener;
private TimeZoneAdapter mTimeZoneAdapter;
+ private Context mContext;
+ private Locale mDefaultLocale;
+
@Before
- public void setUp() {
+ public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
- mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, RuntimeEnvironment.application);
+ mContext = RuntimeEnvironment.application;
+ mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+ mDefaultLocale = Locale.getDefault();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ Locale.setDefault(mDefaultLocale);
+ }
+
+ @Implements(android.text.format.DateFormat.class)
+ public static class ShadowDataFormat {
+
+ public static String mTimeFormatString = "";
+
+ @Implementation
+ public static String getTimeFormatString(Context context) {
+ return mTimeFormatString;
+ }
}
@Test
@@ -89,6 +119,38 @@
assertThat(viewHolder.mDstView.getVisibility()).isEqualTo(View.GONE);
}
+ @Test
+ public void bindViewHolder_on24Hour() {
+ Locale.setDefault(Locale.US);
+ ShadowDataFormat.mTimeFormatString = "HH:mm";
+ mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+
+ final TimeZoneInfo tzi = dummyTimeZoneInfo(TimeZone.getTimeZone("Etc/UTC"));
+ mTimeZoneAdapter.setTimeZoneInfos(Collections.singletonList(tzi));
+
+ final FrameLayout parent = new FrameLayout(RuntimeEnvironment.application);
+
+ final ViewHolder viewHolder = (ViewHolder) mTimeZoneAdapter.createViewHolder(parent, TimeZoneAdapter.VIEW_TYPE_NORMAL);
+ mTimeZoneAdapter.bindViewHolder(viewHolder, 0);
+ assertThat(viewHolder.mTimeView.getText().toString()).hasLength(5);
+ }
+
+ @Test
+ public void bindViewHolder_on12Hour() {
+ Locale.setDefault(Locale.US);
+ ShadowDataFormat.mTimeFormatString = "hh:mm a";
+ mTimeZoneAdapter = new TimeZoneAdapter(mOnClickListener, mContext);
+
+ final TimeZoneInfo tzi = dummyTimeZoneInfo(TimeZone.getTimeZone("Etc/UTC"));
+ mTimeZoneAdapter.setTimeZoneInfos(Collections.singletonList(tzi));
+
+ final FrameLayout parent = new FrameLayout(RuntimeEnvironment.application);
+
+ final ViewHolder viewHolder = (ViewHolder) mTimeZoneAdapter.createViewHolder(parent, TimeZoneAdapter.VIEW_TYPE_NORMAL);
+ mTimeZoneAdapter.bindViewHolder(viewHolder, 0);
+ assertThat(viewHolder.mTimeView.getText().toString()).hasLength(8);
+ }
+
// Pick an arbitrary time zone that's not the current default.
private static TimeZone getNonDefaultTimeZone() {
final String[] availableIDs = TimeZone.getAvailableIDs();