resolved conflicts for merge of 64591009 to gingerbread-plus-aosp
Change-Id: Id9ebca9993f97a79cb2a6c41a7566d473ccc5b7e
This commit is contained in:
@@ -334,54 +334,54 @@ class ContextImpl extends Context {
|
|||||||
@Override
|
@Override
|
||||||
public SharedPreferences getSharedPreferences(String name, int mode) {
|
public SharedPreferences getSharedPreferences(String name, int mode) {
|
||||||
SharedPreferencesImpl sp;
|
SharedPreferencesImpl sp;
|
||||||
|
File prefsFile;
|
||||||
|
boolean needInitialLoad = false;
|
||||||
synchronized (sSharedPrefs) {
|
synchronized (sSharedPrefs) {
|
||||||
sp = sSharedPrefs.get(name);
|
sp = sSharedPrefs.get(name);
|
||||||
if (sp != null && !sp.hasFileChanged()) {
|
if (sp != null && !sp.hasFileChangedUnexpectedly()) {
|
||||||
//Log.i(TAG, "Returning existing prefs " + name + ": " + sp);
|
|
||||||
return sp;
|
return sp;
|
||||||
}
|
}
|
||||||
}
|
prefsFile = getSharedPrefsFile(name);
|
||||||
File f = getSharedPrefsFile(name);
|
if (sp == null) {
|
||||||
FileInputStream str = null;
|
sp = new SharedPreferencesImpl(prefsFile, mode, null);
|
||||||
File backup = makeBackupFile(f);
|
sSharedPrefs.put(name, sp);
|
||||||
if (backup.exists()) {
|
needInitialLoad = true;
|
||||||
f.delete();
|
|
||||||
backup.renameTo(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debugging
|
|
||||||
if (f.exists() && !f.canRead()) {
|
|
||||||
Log.w(TAG, "Attempt to read preferences file " + f + " without permission");
|
|
||||||
}
|
|
||||||
|
|
||||||
Map map = null;
|
|
||||||
if (f.exists() && f.canRead()) {
|
|
||||||
try {
|
|
||||||
str = new FileInputStream(f);
|
|
||||||
map = XmlUtils.readMapXml(str);
|
|
||||||
str.close();
|
|
||||||
} catch (org.xmlpull.v1.XmlPullParserException e) {
|
|
||||||
Log.w(TAG, "getSharedPreferences", e);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
Log.w(TAG, "getSharedPreferences", e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.w(TAG, "getSharedPreferences", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (sSharedPrefs) {
|
synchronized (sp) {
|
||||||
if (sp != null) {
|
if (needInitialLoad && sp.isLoaded()) {
|
||||||
//Log.i(TAG, "Updating existing prefs " + name + " " + sp + ": " + map);
|
// lost the race to load; another thread handled it
|
||||||
sp.replace(map);
|
return sp;
|
||||||
} else {
|
}
|
||||||
sp = sSharedPrefs.get(name);
|
File backup = makeBackupFile(prefsFile);
|
||||||
if (sp == null) {
|
if (backup.exists()) {
|
||||||
sp = new SharedPreferencesImpl(f, mode, map);
|
prefsFile.delete();
|
||||||
sSharedPrefs.put(name, sp);
|
backup.renameTo(prefsFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugging
|
||||||
|
if (prefsFile.exists() && !prefsFile.canRead()) {
|
||||||
|
Log.w(TAG, "Attempt to read preferences file " + prefsFile + " without permission");
|
||||||
|
}
|
||||||
|
|
||||||
|
Map map = null;
|
||||||
|
if (prefsFile.exists() && prefsFile.canRead()) {
|
||||||
|
try {
|
||||||
|
FileInputStream str = new FileInputStream(prefsFile);
|
||||||
|
map = XmlUtils.readMapXml(str);
|
||||||
|
str.close();
|
||||||
|
} catch (org.xmlpull.v1.XmlPullParserException e) {
|
||||||
|
Log.w(TAG, "getSharedPreferences", e);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.w(TAG, "getSharedPreferences", e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w(TAG, "getSharedPreferences", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sp;
|
sp.replace(map);
|
||||||
}
|
}
|
||||||
|
return sp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getPreferencesDir() {
|
private File getPreferencesDir() {
|
||||||
@@ -2709,6 +2709,10 @@ class ContextImpl extends Context {
|
|||||||
|
|
||||||
private static final class SharedPreferencesImpl implements SharedPreferences {
|
private static final class SharedPreferencesImpl implements SharedPreferences {
|
||||||
|
|
||||||
|
// Lock ordering rules:
|
||||||
|
// - acquire SharedPreferencesImpl.this before EditorImpl.this
|
||||||
|
// - acquire mWritingToDiskLock before EditorImpl.this
|
||||||
|
|
||||||
private final File mFile;
|
private final File mFile;
|
||||||
private final File mBackupFile;
|
private final File mBackupFile;
|
||||||
private final int mMode;
|
private final int mMode;
|
||||||
@@ -2716,6 +2720,7 @@ class ContextImpl extends Context {
|
|||||||
private Map<String, Object> mMap; // guarded by 'this'
|
private Map<String, Object> mMap; // guarded by 'this'
|
||||||
private long mTimestamp; // guarded by 'this'
|
private long mTimestamp; // guarded by 'this'
|
||||||
private int mDiskWritesInFlight = 0; // guarded by 'this'
|
private int mDiskWritesInFlight = 0; // guarded by 'this'
|
||||||
|
private boolean mLoaded = false; // guarded by 'this'
|
||||||
|
|
||||||
private final Object mWritingToDiskLock = new Object();
|
private final Object mWritingToDiskLock = new Object();
|
||||||
private static final Object mContent = new Object();
|
private static final Object mContent = new Object();
|
||||||
@@ -2726,6 +2731,7 @@ class ContextImpl extends Context {
|
|||||||
mFile = file;
|
mFile = file;
|
||||||
mBackupFile = makeBackupFile(file);
|
mBackupFile = makeBackupFile(file);
|
||||||
mMode = mode;
|
mMode = mode;
|
||||||
|
mLoaded = initialContents != null;
|
||||||
mMap = initialContents != null ? initialContents : new HashMap<String, Object>();
|
mMap = initialContents != null ? initialContents : new HashMap<String, Object>();
|
||||||
FileStatus stat = new FileStatus();
|
FileStatus stat = new FileStatus();
|
||||||
if (FileUtils.getFileStatus(file.getPath(), stat)) {
|
if (FileUtils.getFileStatus(file.getPath(), stat)) {
|
||||||
@@ -2734,7 +2740,23 @@ class ContextImpl extends Context {
|
|||||||
mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, Object>();
|
mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, Object>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasFileChanged() {
|
// Has this SharedPreferences ever had values assigned to it?
|
||||||
|
boolean isLoaded() {
|
||||||
|
synchronized (this) {
|
||||||
|
return mLoaded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has the file changed out from under us? i.e. writes that
|
||||||
|
// we didn't instigate.
|
||||||
|
public boolean hasFileChangedUnexpectedly() {
|
||||||
|
synchronized (this) {
|
||||||
|
if (mDiskWritesInFlight > 0) {
|
||||||
|
// If we know we caused it, it's not unexpected.
|
||||||
|
Log.d(TAG, "disk write in flight, not unexpected.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
FileStatus stat = new FileStatus();
|
FileStatus stat = new FileStatus();
|
||||||
if (!FileUtils.getFileStatus(mFile.getPath(), stat)) {
|
if (!FileUtils.getFileStatus(mFile.getPath(), stat)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -2745,8 +2767,9 @@ class ContextImpl extends Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void replace(Map newContents) {
|
public void replace(Map newContents) {
|
||||||
if (newContents != null) {
|
synchronized (this) {
|
||||||
synchronized (this) {
|
mLoaded = true;
|
||||||
|
if (newContents != null) {
|
||||||
mMap = newContents;
|
mMap = newContents;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user