Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db7e88af authored by Piotr Wilczyński's avatar Piotr Wilczyński Committed by Automerger Merge Worker
Browse files

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

parents f73e28f2 bd35c8da
Loading
Loading
Loading
Loading
+58 −31
Original line number Original line Diff line number Diff line
@@ -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;

            mHandler.removeCallbacksAndMessages(/* token */ null);
            mHandler.post(() -> {
                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 {
                    } finally {
                mInjector.finishWrite(os, success);
                        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);
        }
        }
    }
    }


+10 −3
Original line number Original line Diff line number Diff line
@@ -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();