diff options
author | Beverly <beverlyt@google.com> | 2021-07-02 15:35:41 -0400 |
---|---|---|
committer | Beverly Tai <beverlyt@google.com> | 2021-07-07 20:45:06 +0000 |
commit | 4812dfbc6293b29e9a38909eccea398bee39c7be (patch) | |
tree | afa8cede809c3a866d57592d28043d769c417a6a /packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java | |
parent | 05be67374674da85f6c596ff44a8e4a43aafe742 (diff) |
Bring back time formatting for single line clock
Use old date format for the single line clock, so that we now
support (for single line clock) different clock representations.
For example, French Canadian uses an h instead of a colon
(13 h 01 instead of 13:01).
Test: manual
Bug: 186726401
Fixes: 191761465
Change-Id: If97a49780029f24dc99625a310729630b69e7a68
Diffstat (limited to 'packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java')
-rw-r--r-- | packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java b/packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java index 58b3865facbc..ef3104a21708 100644 --- a/packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java +++ b/packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java @@ -19,9 +19,9 @@ package com.android.keyguard; import android.annotation.FloatRange; import android.annotation.IntRange; import android.content.Context; +import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; -import android.icu.text.DateTimePatternGenerator; import android.text.format.DateFormat; import android.util.AttributeSet; import android.widget.TextView; @@ -30,6 +30,7 @@ import com.android.systemui.R; import com.android.systemui.statusbar.phone.KeyguardBypassController; import java.util.Calendar; +import java.util.Locale; import java.util.TimeZone; import kotlin.Unit; @@ -41,8 +42,6 @@ import kotlin.Unit; public class AnimatableClockView extends TextView { private static final CharSequence DOUBLE_LINE_FORMAT_12_HOUR = "hh\nmm"; private static final CharSequence DOUBLE_LINE_FORMAT_24_HOUR = "HH\nmm"; - private static final CharSequence SINGLE_LINE_FORMAT_12_HOUR = "h:mm"; - private static final CharSequence SINGLE_LINE_FORMAT_24_HOUR = "HH:mm"; private static final long DOZE_ANIM_DURATION = 300; private static final long APPEAR_ANIM_DURATION = 350; private static final long CHARGE_ANIM_DURATION_PHASE_0 = 500; @@ -259,25 +258,47 @@ public class AnimatableClockView extends TextView { } void refreshFormat() { + Patterns.update(mContext); + final boolean use24HourFormat = DateFormat.is24HourFormat(getContext()); if (mIsSingleLine && use24HourFormat) { - mFormat = SINGLE_LINE_FORMAT_24_HOUR; + mFormat = Patterns.sClockView24; } else if (!mIsSingleLine && use24HourFormat) { mFormat = DOUBLE_LINE_FORMAT_24_HOUR; } else if (mIsSingleLine && !use24HourFormat) { - mFormat = SINGLE_LINE_FORMAT_12_HOUR; + mFormat = Patterns.sClockView12; } else { mFormat = DOUBLE_LINE_FORMAT_12_HOUR; } - mDescFormat = getBestDateTimePattern(getContext(), use24HourFormat ? "Hm" : "hm"); + mDescFormat = use24HourFormat ? Patterns.sClockView24 : Patterns.sClockView12; refreshTime(); } - private static String getBestDateTimePattern(Context context, String skeleton) { - DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance( - context.getResources().getConfiguration().locale); - return dtpg.getBestPattern(skeleton); + // DateFormat.getBestDateTimePattern is extremely expensive, and refresh is called often. + // This is an optimization to ensure we only recompute the patterns when the inputs change. + private static final class Patterns { + static String sClockView12; + static String sClockView24; + static String sCacheKey; + + static void update(Context context) { + final Locale locale = Locale.getDefault(); + final Resources res = context.getResources(); + final String clockView12Skel = res.getString(R.string.clock_12hr_format); + final String clockView24Skel = res.getString(R.string.clock_24hr_format); + final String key = locale.toString() + clockView12Skel + clockView24Skel; + if (key.equals(sCacheKey)) return; + sClockView12 = DateFormat.getBestDateTimePattern(locale, clockView12Skel); + + // CLDR insists on adding an AM/PM indicator even though it wasn't in the skeleton + // format. The following code removes the AM/PM indicator if we didn't want it. + if (!clockView12Skel.contains("a")) { + sClockView12 = sClockView12.replaceAll("a", "").trim(); + } + sClockView24 = DateFormat.getBestDateTimePattern(locale, clockView24Skel); + sCacheKey = key; + } } interface DozeStateGetter { |