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

Commit a3f641f2 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick Committed by Android Git Automerger
Browse files

am cf2c68c9: am 11709ab0: Merge "Avoid unnecessary SharedPrefences disk writes." into gingerbread

Merge commit 'cf2c68c9'

* commit 'cf2c68c9':
  Avoid unnecessary SharedPrefences disk writes.
parents 4bd3e95c cf2c68c9
Loading
Loading
Loading
Loading
+28 −6
Original line number Diff line number Diff line
@@ -2882,6 +2882,7 @@ class ContextImpl extends Context {
                boolean returnValue;

                boolean hasListeners;
                boolean changesMade = false;
                List<String> keysModified = null;
                Set<OnSharedPreferenceChangeListener> listeners = null;

@@ -2895,17 +2896,31 @@ class ContextImpl extends Context {

                    synchronized (this) {
                        if (mClear) {
                            if (!mMap.isEmpty()) {
                                changesMade = true;
                                mMap.clear();
                            }
                            mClear = false;
                        }

                        for (Entry<String, Object> e : mModified.entrySet()) {
                            String k = e.getKey();
                            Object v = e.getValue();
                            if (v == this) {
                            if (v == this) {  // magic value for a removal mutation
                                if (mMap.containsKey(k)) {
                                    mMap.remove(k);
                                    changesMade = true;
                                }
                            } else {
                                boolean isSame = false;
                                if (mMap.containsKey(k)) {
                                    Object existingValue = mMap.get(k);
                                    isSame = existingValue != null && existingValue.equals(v);
                                }
                                if (!isSame) {
                                    mMap.put(k, v);
                                    changesMade = true;
                                }
                            }

                            if (hasListeners) {
@@ -2916,7 +2931,7 @@ class ContextImpl extends Context {
                        mModified.clear();
                    }

                    returnValue = writeFileLocked();
                    returnValue = writeFileLocked(changesMade);
                }

                if (hasListeners) {
@@ -2961,9 +2976,16 @@ class ContextImpl extends Context {
            return str;
        }

        private boolean writeFileLocked() {
        private boolean writeFileLocked(boolean changesMade) {
            // Rename the current file so it may be used as a backup during the next read
            if (mFile.exists()) {
                if (!changesMade) {
                    // If the file already exists, but no changes were
                    // made to the underlying map, it's wasteful to
                    // re-write the file.  Return as if we wrote it
                    // out.
                    return true;
                }
                if (!mBackupFile.exists()) {
                    if (!mFile.renameTo(mBackupFile)) {
                        Log.e(TAG, "Couldn't rename file " + mFile