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
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user