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

Commit 5edae415 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Frameworks: Move SharedPreferencesImpl to tristate"

parents 68ad3d11 1aa0d01c
Loading
Loading
Loading
Loading
+34 −9
Original line number Diff line number Diff line
@@ -71,6 +71,8 @@ final class SharedPreferencesImpl implements SharedPreferences {

    @GuardedBy("mLock")
    private Map<String, Object> mMap;
    @GuardedBy("mLock")
    private Throwable mThrowable;

    @GuardedBy("mLock")
    private int mDiskWritesInFlight = 0;
@@ -107,6 +109,7 @@ final class SharedPreferencesImpl implements SharedPreferences {
        mMode = mode;
        mLoaded = false;
        mMap = null;
        mThrowable = null;
        startLoadFromDisk();
    }

@@ -139,6 +142,7 @@ final class SharedPreferencesImpl implements SharedPreferences {

        Map<String, Object> map = null;
        StructStat stat = null;
        Throwable thrown = null;
        try {
            stat = Os.stat(mFile.getPath());
            if (mFile.canRead()) {
@@ -154,11 +158,21 @@ final class SharedPreferencesImpl implements SharedPreferences {
                }
            }
        } catch (ErrnoException e) {
            /* ignore */
            // An errno exception means the stat failed. Treat as empty/non-existing by
            // ignoring.
        } catch (Throwable t) {
            thrown = t;
        }

        synchronized (mLock) {
            mLoaded = true;
            mThrowable = thrown;

            // It's important that we always signal waiters, even if we'll make
            // them fail with an exception. The try-finally is pretty wide, but
            // better safe than sorry.
            try {
                if (thrown == null) {
                    if (map != null) {
                        mMap = map;
                        mStatTimestamp = stat.st_mtim;
@@ -166,9 +180,16 @@ final class SharedPreferencesImpl implements SharedPreferences {
                    } else {
                        mMap = new HashMap<>();
                    }
                }
                // In case of a thrown exception, we retain the old map. That allows
                // any open editors to commit and store updates.
            } catch (Throwable t) {
                mThrowable = t;
            } finally {
                mLock.notifyAll();
            }
        }
    }

    static File makeBackupFile(File prefsFile) {
        return new File(prefsFile.getPath() + ".bak");
@@ -226,6 +247,7 @@ final class SharedPreferencesImpl implements SharedPreferences {
        }
    }

    @GuardedBy("mLock")
    private void awaitLoadedLocked() {
        if (!mLoaded) {
            // Raise an explicit StrictMode onReadFromDisk for this
@@ -239,6 +261,9 @@ final class SharedPreferencesImpl implements SharedPreferences {
            } catch (InterruptedException unused) {
            }
        }
        if (mThrowable != null) {
            throw new IllegalStateException(mThrowable);
        }
    }

    @Override