Do not write if apply() did not change the file.

Bug: 33385963
Test: SharedPreferences CTS tests
Change-Id: I39955f8fbcdaa54faa539a3c503e12cb00808136
This commit is contained in:
Philip P. Moltmann
2016-12-13 16:32:06 -08:00
parent 31d6889f4c
commit ea10cc0fbb

View File

@@ -592,20 +592,20 @@ final class SharedPreferencesImpl implements SharedPreferences {
if (mFile.exists()) { if (mFile.exists()) {
boolean needsWrite = false; boolean needsWrite = false;
if (isFromSyncCommit) {
// Only need to write if the disk state is older than this commit // Only need to write if the disk state is older than this commit
if (mDiskStateGeneration < mcr.memoryStateGeneration) { if (mDiskStateGeneration < mcr.memoryStateGeneration) {
if (isFromSyncCommit) {
needsWrite = true; needsWrite = true;
}
} else { } else {
synchronized (this) { synchronized (this) {
// No need to persist intermediate states. Just wait for the latest state to be // No need to persist intermediate states. Just wait for the latest state to
// persisted. // be persisted.
if (mCurrentMemoryStateGeneration == mcr.memoryStateGeneration) { if (mCurrentMemoryStateGeneration == mcr.memoryStateGeneration) {
needsWrite = true; needsWrite = true;
} }
} }
} }
}
if (!needsWrite) { if (!needsWrite) {
if (DEBUG) { if (DEBUG) {