Merge "Save brightness into persistent data store using a handler" into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
bd35c8dabf
@@ -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,6 +430,7 @@ final class PersistentDataStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
private void load() {
|
||||||
|
synchronized (mFileAccessLock) {
|
||||||
clearState();
|
clearState();
|
||||||
|
|
||||||
final InputStream is;
|
final InputStream is;
|
||||||
@@ -441,22 +454,36 @@ final class PersistentDataStore {
|
|||||||
IoUtils.closeQuietly(is);
|
IoUtils.closeQuietly(is);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void save() {
|
private void save() {
|
||||||
final OutputStream os;
|
final ByteArrayOutputStream os;
|
||||||
try {
|
|
||||||
os = mInjector.startWrite();
|
|
||||||
boolean success = false;
|
|
||||||
try {
|
try {
|
||||||
|
os = new ByteArrayOutputStream();
|
||||||
|
|
||||||
TypedXmlSerializer serializer = Xml.resolveSerializer(os);
|
TypedXmlSerializer serializer = Xml.resolveSerializer(os);
|
||||||
saveToXml(serializer);
|
saveToXml(serializer);
|
||||||
serializer.flush();
|
serializer.flush();
|
||||||
success = true;
|
|
||||||
} finally {
|
mHandler.removeCallbacksAndMessages(/* token */ null);
|
||||||
mInjector.finishWrite(os, success);
|
mHandler.post(() -> {
|
||||||
}
|
synchronized (mFileAccessLock) {
|
||||||
|
OutputStream fileOutput = null;
|
||||||
|
try {
|
||||||
|
fileOutput = mInjector.startWrite();
|
||||||
|
os.writeTo(fileOutput);
|
||||||
|
fileOutput.flush();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Slog.w(TAG, "Failed to save display manager persistent store data.", ex);
|
Slog.w(TAG, "Failed to save display manager persistent store data.", ex);
|
||||||
|
} finally {
|
||||||
|
if (fileOutput != null) {
|
||||||
|
mInjector.finishWrite(fileOutput, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException ex) {
|
||||||
|
Slog.w(TAG, "Failed to process the XML serializer.", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user