diff options
| author | 2022-12-01 15:20:35 +0100 | |
|---|---|---|
| committer | 2022-12-06 12:01:52 +0000 | |
| commit | f49d3de387c1107b6d650089e75b6849bc6d2468 (patch) | |
| tree | 4665a8f418113b4efc8afb9a7adea672f84195ef | |
| parent | 5932f31d5807aacea83b851e07c9472cf1f817c4 (diff) | |
Initialise the brightness value with Float.NaN in PersistentDataStore
If there is no DisplayState, and it gets initialised because of any
other field, for instance refresh-rate, the brightness gets initialised
by 0. Since 0 is a permissible value for brightness, is causes issues by
changing the brightness to 0.
Bug: 259517441
Test: atest PersistentDataStoreTest
Change-Id: I532afc9e9d437199922f83f3c4440127a86a88f5
| -rw-r--r-- | services/core/java/com/android/server/display/PersistentDataStore.java | 12 | ||||
| -rw-r--r-- | services/tests/servicestests/src/com/android/server/display/PersistentDataStoreTest.java | 34 |
2 files changed, 43 insertions, 3 deletions
diff --git a/services/core/java/com/android/server/display/PersistentDataStore.java b/services/core/java/com/android/server/display/PersistentDataStore.java index a11f1721a4b9..73131a1dc220 100644 --- a/services/core/java/com/android/server/display/PersistentDataStore.java +++ b/services/core/java/com/android/server/display/PersistentDataStore.java @@ -619,7 +619,7 @@ final class PersistentDataStore { private static final class DisplayState { private int mColorMode; - private float mBrightness; + private float mBrightness = Float.NaN; private int mWidth; private int mHeight; private float mRefreshRate; @@ -700,7 +700,11 @@ final class PersistentDataStore { break; case TAG_BRIGHTNESS_VALUE: String brightness = parser.nextText(); - mBrightness = Float.parseFloat(brightness); + try { + mBrightness = Float.parseFloat(brightness); + } catch (NumberFormatException e) { + mBrightness = Float.NaN; + } break; case TAG_BRIGHTNESS_CONFIGURATIONS: mDisplayBrightnessConfigurations.loadFromXml(parser); @@ -727,7 +731,9 @@ final class PersistentDataStore { serializer.endTag(null, TAG_COLOR_MODE); serializer.startTag(null, TAG_BRIGHTNESS_VALUE); - serializer.text(Float.toString(mBrightness)); + if (!Float.isNaN(mBrightness)) { + serializer.text(Float.toString(mBrightness)); + } serializer.endTag(null, TAG_BRIGHTNESS_VALUE); serializer.startTag(null, TAG_BRIGHTNESS_CONFIGURATIONS); diff --git a/services/tests/servicestests/src/com/android/server/display/PersistentDataStoreTest.java b/services/tests/servicestests/src/com/android/server/display/PersistentDataStoreTest.java index 3b0a22f80c30..35a677e0f816 100644 --- a/services/tests/servicestests/src/com/android/server/display/PersistentDataStoreTest.java +++ b/services/tests/servicestests/src/com/android/server/display/PersistentDataStoreTest.java @@ -344,6 +344,40 @@ public class PersistentDataStoreTest { assertEquals(85.3f, newDataStore.getUserPreferredRefreshRate(testDisplayDevice), 0.1f); } + @Test + public void testBrightnessInitialisesWithInvalidFloat() { + final String uniqueDisplayId = "test:123"; + DisplayDevice testDisplayDevice = new DisplayDevice(null, null, uniqueDisplayId, null) { + @Override + public boolean hasStableUniqueId() { + return true; + } + + @Override + public DisplayDeviceInfo getDisplayDeviceInfoLocked() { + return null; + } + }; + + // Set any value which initialises Display state + float refreshRate = 85.3f; + mDataStore.loadIfNeeded(); + mDataStore.setUserPreferredRefreshRate(testDisplayDevice, refreshRate); + + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mInjector.setWriteStream(baos); + mDataStore.saveIfNeeded(); + mTestLooper.dispatchAll(); + assertTrue(mInjector.wasWriteSuccessful()); + TestInjector newInjector = new TestInjector(); + PersistentDataStore newDataStore = new PersistentDataStore(newInjector); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + newInjector.setReadStream(bais); + newDataStore.loadIfNeeded(); + assertTrue(Float.isNaN(mDataStore.getBrightness(testDisplayDevice))); + } + + public class TestInjector extends PersistentDataStore.Injector { private InputStream mReadStream; private OutputStream mWriteStream; |