Merge "Save brightness into persistent data store using a handler" into tm-dev am: bd35c8dabf

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17654877

Change-Id: I63e848246b07310b3a8c7db249e94ba92232b9f0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Piotr Wilczyński
2022-06-01 19:43:21 +00:00
committed by Automerger Merge Worker
2 changed files with 69 additions and 35 deletions

View File

@@ -20,6 +20,7 @@ import android.annotation.Nullable;
import android.graphics.Point; import android.graphics.Point;
import android.hardware.display.BrightnessConfiguration; import android.hardware.display.BrightnessConfiguration;
import android.hardware.display.WifiDisplay; import android.hardware.display.WifiDisplay;
import android.os.Handler;
import android.util.AtomicFile; import android.util.AtomicFile;
import android.util.Slog; import android.util.Slog;
import android.util.SparseArray; import android.util.SparseArray;
@@ -31,12 +32,14 @@ import android.util.Xml;
import android.view.Display; import android.view.Display;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.XmlUtils; import com.android.internal.util.XmlUtils;
import libcore.io.IoUtils; import libcore.io.IoUtils;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@@ -141,13 +144,22 @@ final class PersistentDataStore {
// The interface for methods which should be replaced by the test harness. // The interface for methods which should be replaced by the test harness.
private Injector mInjector; private Injector mInjector;
private final Handler mHandler;
private final Object mFileAccessLock = new Object();
public PersistentDataStore() { public PersistentDataStore() {
this(new Injector()); this(new Injector());
} }
@VisibleForTesting @VisibleForTesting
PersistentDataStore(Injector injector) { PersistentDataStore(Injector injector) {
this(injector, BackgroundThread.getHandler());
}
@VisibleForTesting
PersistentDataStore(Injector injector, Handler handler) {
mInjector = injector; mInjector = injector;
mHandler = handler;
} }
public void saveIfNeeded() { public void saveIfNeeded() {
@@ -418,45 +430,60 @@ final class PersistentDataStore {
} }
private void load() { private void load() {
clearState(); synchronized (mFileAccessLock) {
final InputStream is;
try {
is = mInjector.openRead();
} catch (FileNotFoundException ex) {
return;
}
TypedXmlPullParser parser;
try {
parser = Xml.resolvePullParser(is);
loadFromXml(parser);
} catch (IOException ex) {
Slog.w(TAG, "Failed to load display manager persistent store data.", ex);
clearState(); clearState();
} catch (XmlPullParserException ex) {
Slog.w(TAG, "Failed to load display manager persistent store data.", ex); final InputStream is;
clearState(); try {
} finally { is = mInjector.openRead();
IoUtils.closeQuietly(is); } catch (FileNotFoundException ex) {
return;
}
TypedXmlPullParser parser;
try {
parser = Xml.resolvePullParser(is);
loadFromXml(parser);
} catch (IOException ex) {
Slog.w(TAG, "Failed to load display manager persistent store data.", ex);
clearState();
} catch (XmlPullParserException ex) {
Slog.w(TAG, "Failed to load display manager persistent store data.", ex);
clearState();
} finally {
IoUtils.closeQuietly(is);
}
} }
} }
private void save() { private void save() {
final OutputStream os; final ByteArrayOutputStream os;
try { try {
os = mInjector.startWrite(); os = new ByteArrayOutputStream();
boolean success = false;
try { TypedXmlSerializer serializer = Xml.resolveSerializer(os);
TypedXmlSerializer serializer = Xml.resolveSerializer(os); saveToXml(serializer);
saveToXml(serializer); serializer.flush();
serializer.flush();
success = true; mHandler.removeCallbacksAndMessages(/* token */ null);
} finally { mHandler.post(() -> {
mInjector.finishWrite(os, success); synchronized (mFileAccessLock) {
} OutputStream fileOutput = null;
try {
fileOutput = mInjector.startWrite();
os.writeTo(fileOutput);
fileOutput.flush();
} catch (IOException ex) {
Slog.w(TAG, "Failed to save display manager persistent store data.", ex);
} finally {
if (fileOutput != null) {
mInjector.finishWrite(fileOutput, true);
}
}
}
});
} catch (IOException ex) { } catch (IOException ex) {
Slog.w(TAG, "Failed to save display manager persistent store data.", ex); Slog.w(TAG, "Failed to process the XML serializer.", ex);
} }
} }

View File

@@ -24,6 +24,8 @@ import static org.junit.Assert.assertTrue;
import android.content.Context; import android.content.Context;
import android.hardware.display.BrightnessConfiguration; import android.hardware.display.BrightnessConfiguration;
import android.os.Handler;
import android.os.test.TestLooper;
import android.util.Pair; import android.util.Pair;
import androidx.test.InstrumentationRegistry; import androidx.test.InstrumentationRegistry;
@@ -47,11 +49,14 @@ import java.nio.charset.StandardCharsets;
public class PersistentDataStoreTest { public class PersistentDataStoreTest {
private PersistentDataStore mDataStore; private PersistentDataStore mDataStore;
private TestInjector mInjector; private TestInjector mInjector;
private TestLooper mTestLooper;
@Before @Before
public void setUp() { public void setUp() {
mInjector = new TestInjector(); mInjector = new TestInjector();
mDataStore = new PersistentDataStore(mInjector); mTestLooper = new TestLooper();
Handler handler = new Handler(mTestLooper.getLooper());
mDataStore = new PersistentDataStore(mInjector, handler);
} }
@Test @Test
@@ -147,7 +152,7 @@ public class PersistentDataStoreTest {
} }
@Test @Test
public void testStoreAndReloadOfDisplayBrightnessConfigurations() { public void testStoreAndReloadOfDisplayBrightnessConfigurations() throws InterruptedException {
final String uniqueDisplayId = "test:123"; final String uniqueDisplayId = "test:123";
int userSerial = 0; int userSerial = 0;
String packageName = "pdsTestPackage"; String packageName = "pdsTestPackage";
@@ -178,6 +183,7 @@ public class PersistentDataStoreTest {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
mInjector.setWriteStream(baos); mInjector.setWriteStream(baos);
mDataStore.saveIfNeeded(); mDataStore.saveIfNeeded();
mTestLooper.dispatchAll();
assertTrue(mInjector.wasWriteSuccessful()); assertTrue(mInjector.wasWriteSuccessful());
TestInjector newInjector = new TestInjector(); TestInjector newInjector = new TestInjector();
PersistentDataStore newDataStore = new PersistentDataStore(newInjector); PersistentDataStore newDataStore = new PersistentDataStore(newInjector);
@@ -222,7 +228,7 @@ public class PersistentDataStoreTest {
} }
@Test @Test
public void testStoreAndReloadOfBrightnessConfigurations() { public void testStoreAndReloadOfBrightnessConfigurations() throws InterruptedException {
final float[] lux = { 0f, 10f }; final float[] lux = { 0f, 10f };
final float[] nits = {1f, 100f }; final float[] nits = {1f, 100f };
final BrightnessConfiguration config = new BrightnessConfiguration.Builder(lux, nits) final BrightnessConfiguration config = new BrightnessConfiguration.Builder(lux, nits)
@@ -238,6 +244,7 @@ public class PersistentDataStoreTest {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ByteArrayOutputStream baos = new ByteArrayOutputStream();
mInjector.setWriteStream(baos); mInjector.setWriteStream(baos);
mDataStore.saveIfNeeded(); mDataStore.saveIfNeeded();
mTestLooper.dispatchAll();
assertTrue(mInjector.wasWriteSuccessful()); assertTrue(mInjector.wasWriteSuccessful());
TestInjector newInjector = new TestInjector(); TestInjector newInjector = new TestInjector();