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

Commit 5913630e authored by Brad Fitzpatrick's avatar Brad Fitzpatrick
Browse files

Don't re-read SharedPreferences unnecessarily.

Previously reads were only cached once a write occurred because
loading didn't set the stat metadata, thus the
sp.hasFileChangedUnexpectedly() check always fired on reading.

Now the initial read populates the stat info, so getSharedPreference()
repeatedly bypasses reading from disk, even wtihout writes.

This was probably a regression from apply() being added in
Gingerbread.

Bug: 3211034
Change-Id: Ifa0bbb27c53a4099544950a4f822fab1fc23a47d
parent de8da848
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -575,7 +575,8 @@ class ContextImpl extends Context {
            }

            Map map = null;
            if (prefsFile.exists() && prefsFile.canRead()) {
            FileStatus stat = new FileStatus();
            if (FileUtils.getFileStatus(prefsFile.getPath(), stat) && prefsFile.canRead()) {
                try {
                    FileInputStream str = new FileInputStream(prefsFile);
                    map = XmlUtils.readMapXml(str);
@@ -588,7 +589,7 @@ class ContextImpl extends Context {
                    Log.w(TAG, "getSharedPreferences", e);
                }
            }
            sp.replace(map);
            sp.replace(map, stat);
        }
        return sp;
    }
+5 −1
Original line number Diff line number Diff line
@@ -103,12 +103,16 @@ final class SharedPreferencesImpl implements SharedPreferences {
        }
    }

    public void replace(Map newContents) {
    /*package*/ void replace(Map newContents, FileStatus stat) {
        synchronized (this) {
            mLoaded = true;
            if (newContents != null) {
                mMap = newContents;
            }
            if (stat != null) {
                mStatTimestamp = stat.mtime;
                mStatSize = stat.size;
            }
        }
    }