Merge "Bring back time formatting for single line clock" into sc-dev

This commit is contained in:
Beverly Tai
2021-07-07 20:45:17 +00:00
committed by Android (Google) Code Review
4 changed files with 35 additions and 46 deletions

View File

@@ -35,13 +35,13 @@
android:id="@+id/animatable_clock_view" android:id="@+id/animatable_clock_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="start"
android:gravity="center_horizontal" android:gravity="start"
android:textSize="@dimen/clock_text_size" android:textSize="@dimen/clock_text_size"
android:fontFamily="@font/clock" android:fontFamily="@font/clock"
android:typeface="monospace"
android:elegantTextHeight="false" android:elegantTextHeight="false"
android:singleLine="true" android:singleLine="true"
android:fontFeatureSettings="pnum"
chargeAnimationDelay="350" chargeAnimationDelay="350"
dozeWeight="200" dozeWeight="200"
lockScreenWeight="400" lockScreenWeight="400"

View File

@@ -212,6 +212,7 @@ public class AnimatableClockController extends ViewController<AnimatableClockVie
} else { } else {
mView.setLineSpacingScale(mDefaultLineSpacing); mView.setLineSpacingScale(mDefaultLineSpacing);
} }
mView.refreshFormat();
} }
} }

View File

@@ -19,9 +19,9 @@ package com.android.keyguard;
import android.annotation.FloatRange; import android.annotation.FloatRange;
import android.annotation.IntRange; import android.annotation.IntRange;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.icu.text.DateTimePatternGenerator;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.TextView; import android.widget.TextView;
@@ -30,6 +30,7 @@ import com.android.systemui.R;
import com.android.systemui.statusbar.phone.KeyguardBypassController; import com.android.systemui.statusbar.phone.KeyguardBypassController;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import kotlin.Unit; import kotlin.Unit;
@@ -41,8 +42,6 @@ import kotlin.Unit;
public class AnimatableClockView extends TextView { public class AnimatableClockView extends TextView {
private static final CharSequence DOUBLE_LINE_FORMAT_12_HOUR = "hh\nmm"; 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 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 DOZE_ANIM_DURATION = 300;
private static final long APPEAR_ANIM_DURATION = 350; private static final long APPEAR_ANIM_DURATION = 350;
private static final long CHARGE_ANIM_DURATION_PHASE_0 = 500; private static final long CHARGE_ANIM_DURATION_PHASE_0 = 500;
@@ -259,25 +258,47 @@ public class AnimatableClockView extends TextView {
} }
void refreshFormat() { void refreshFormat() {
Patterns.update(mContext);
final boolean use24HourFormat = DateFormat.is24HourFormat(getContext()); final boolean use24HourFormat = DateFormat.is24HourFormat(getContext());
if (mIsSingleLine && use24HourFormat) { if (mIsSingleLine && use24HourFormat) {
mFormat = SINGLE_LINE_FORMAT_24_HOUR; mFormat = Patterns.sClockView24;
} else if (!mIsSingleLine && use24HourFormat) { } else if (!mIsSingleLine && use24HourFormat) {
mFormat = DOUBLE_LINE_FORMAT_24_HOUR; mFormat = DOUBLE_LINE_FORMAT_24_HOUR;
} else if (mIsSingleLine && !use24HourFormat) { } else if (mIsSingleLine && !use24HourFormat) {
mFormat = SINGLE_LINE_FORMAT_12_HOUR; mFormat = Patterns.sClockView12;
} else { } else {
mFormat = DOUBLE_LINE_FORMAT_12_HOUR; mFormat = DOUBLE_LINE_FORMAT_12_HOUR;
} }
mDescFormat = getBestDateTimePattern(getContext(), use24HourFormat ? "Hm" : "hm"); mDescFormat = use24HourFormat ? Patterns.sClockView24 : Patterns.sClockView12;
refreshTime(); refreshTime();
} }
private static String getBestDateTimePattern(Context context, String skeleton) { // DateFormat.getBestDateTimePattern is extremely expensive, and refresh is called often.
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance( // This is an optimization to ensure we only recompute the patterns when the inputs change.
context.getResources().getConfiguration().locale); private static final class Patterns {
return dtpg.getBestPattern(skeleton); 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 { interface DozeStateGetter {

View File

@@ -20,9 +20,7 @@ import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import android.app.WallpaperManager; import android.app.WallpaperManager;
import android.content.res.Resources;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
@@ -367,37 +365,6 @@ public class KeyguardClockSwitchController extends ViewController<KeyguardClockS
return mColorExtractor.getColors(WallpaperManager.FLAG_LOCK); return mColorExtractor.getColors(WallpaperManager.FLAG_LOCK);
} }
// 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(Resources res) {
final Locale locale = Locale.getDefault();
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);
// Use fancy colon.
sClockView24 = sClockView24.replace(':', '\uee01');
sClockView12 = sClockView12.replace(':', '\uee01');
sCacheKey = key;
}
}
private int getCurrentLayoutDirection() { private int getCurrentLayoutDirection() {
return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()); return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault());
} }