From 5ecdfa15559482676402d61463cc51faeb6e18c8 Mon Sep 17 00:00:00 2001 From: Mark Renouf Date: Thu, 9 Jun 2022 03:35:24 +0000 Subject: [PATCH] Fixes a crash ColorScheme#toString Fixes an IndexOutOfBoundsException in stringForColor whenever the value is between 0 and 15. Integer#toHexString does not zero pad. Bug: 234413479 Test: atest ColorSchemeTest#testToString Change-Id: I50f95ee8f2b1924bb1778b66ed39fa40685ef7e2 --- .../src/com/android/systemui/monet/ColorScheme.kt | 2 +- .../android/systemui/monet/ColorSchemeTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/SystemUI/monet/src/com/android/systemui/monet/ColorScheme.kt b/packages/SystemUI/monet/src/com/android/systemui/monet/ColorScheme.kt index 8e34c4757f377..b3dd95553ed09 100644 --- a/packages/SystemUI/monet/src/com/android/systemui/monet/ColorScheme.kt +++ b/packages/SystemUI/monet/src/com/android/systemui/monet/ColorScheme.kt @@ -435,7 +435,7 @@ class ColorScheme( val h = "H${hct.hue.roundToInt().toString().padEnd(width)}" val c = "C${hct.chroma.roundToInt().toString().padEnd(width)}" val t = "T${CamUtils.lstarFromInt(color).roundToInt().toString().padEnd(width)}" - val hex = Integer.toHexString(color).replaceRange(0, 2, "").uppercase() + val hex = Integer.toHexString(color and 0xffffff).padStart(6, '0').uppercase() return "$h$c$t = #$hex" } diff --git a/packages/SystemUI/tests/src/com/android/systemui/monet/ColorSchemeTest.java b/packages/SystemUI/tests/src/com/android/systemui/monet/ColorSchemeTest.java index 63dca3bafc5b0..0badd861787df 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/monet/ColorSchemeTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/monet/ColorSchemeTest.java @@ -146,6 +146,20 @@ public class ColorSchemeTest extends SysuiTestCase { Assert.assertTrue(cam.getChroma() <= 8.0); } + @Test + @SuppressWarnings("ResultOfMethodCallIgnored") + public void testToString() { + new ColorScheme(Color.TRANSPARENT, false /* darkTheme */).toString(); + new ColorScheme(Color.argb(0, 0, 0, 0xf), false /* darkTheme */).toString(); + new ColorScheme(Color.argb(0xff, 0xff, 0, 0), false /* darkTheme */).toString(); + new ColorScheme(0xFFFFFFFF, false /* darkTheme */).toString(); + + new ColorScheme(Color.TRANSPARENT, true /* darkTheme */).toString(); + new ColorScheme(Color.argb(0, 0, 0, 0xf), true /* darkTheme */).toString(); + new ColorScheme(0xFFFF0000, true /* darkTheme */).toString(); + new ColorScheme(0xFFFFFFFF, true /* darkTheme */).toString(); + } + /** * Generate xml for SystemPaletteTest#testThemeStyles(). */