Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20c5ef73 authored by Joachim Sauer's avatar Joachim Sauer
Browse files

Extract is24HourLocale method.

This method is needed in settings to avoid code duplication.

Add a test to verify that is24HourLocale does not take the 12/24h clock
system setting into account.

Bug: 32761619
Test: bit CtsTextTestCases:android.text.format.cts.DateFormatTest
Test: adb shell am instrument -w -e class android.text.format.DateFormatTest \
        com.android.frameworks.coretests/android.support.test.runner.AndroidJUnitRunner
Change-Id: I21a871c11149550c7cf91aea299724a8b4fbff78
parent 2164608b
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.text.format;

import android.annotation.NonNull;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
@@ -183,8 +184,17 @@ public class DateFormat {
            return value.equals("24");
        }

        final Locale locale = context.getResources().getConfiguration().locale;
        return is24HourLocale(context.getResources().getConfiguration().locale);
    }

    /**
     * Returns true if the specified locale uses a 24-hour time format by default, ignoring user
     * settings.
     * @param locale the locale to check
     * @return true if the locale uses a 24 hour time format by default, false otherwise
     * @hide
     */
    public static boolean is24HourLocale(@NonNull Locale locale) {
        synchronized (sLocaleLock) {
            if (sIs24HourLocale != null && sIs24HourLocale.equals(locale)) {
                return sIs24Hour;
+8 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Locale;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class DateFormatTest {
@@ -44,4 +46,10 @@ public class DateFormatTest {

        assertFalse(DateFormat.hasDesignator("hh:mm 'yyyy'", DateFormat.YEAR));
    }

    @Test
    public void testIs24HourLocale() {
        assertFalse(DateFormat.is24HourLocale(Locale.US));
        assertTrue(DateFormat.is24HourLocale(Locale.GERMANY));
    }
}