Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Improve logging shared preferences ex
Browse files Browse the repository at this point in the history
Refs #1094
  • Loading branch information
M66B committed Jan 11, 2014
1 parent 5dbb960 commit 025265f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ For interested developers:

* Hook *Service.onCreate* for uncaught exception
* Dialogs for all the things in *Requirements.java* (no intents without user consent)
* Update *SharedPreferencesEx* with KitKat changes
* Import should clear app settings
* Accessibility: *android:labelFor="..."*
* Secure usage data
Expand Down
23 changes: 13 additions & 10 deletions src/biz/bokhorst/xprivacy/SharedPreferencesEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ public final class SharedPreferencesEx implements SharedPreferences {
private long mLastModified;
private long mFileSize;

private static int cMaxTries = 10;
private static int cTryWaitMs = 50;

public SharedPreferencesEx(File prefFile) {
mFile = prefFile;
mBackupFile = makeBackupFile(prefFile);
startLoadFromDisk();
}

public SharedPreferencesEx(String packageName) {
this(packageName, packageName + "_preferences");
}

public SharedPreferencesEx(String packageName, String prefFileName) {
mFile = new File(Util.getUserDataDirectory(Process.myUid()) + File.pathSeparator + "shared_prefs"
+ File.pathSeparator + prefFileName + ".xml");
Expand All @@ -62,7 +61,7 @@ public void run() {
@SuppressWarnings({ "rawtypes", "unchecked" })
private void loadFromDiskLocked() {
int tries = 0;
while (!mLoaded && (mFile.exists() || mBackupFile.exists()) && ++tries < 10) {
while (++tries <= cMaxTries && !mLoaded && (mFile.exists() || mBackupFile.exists())) {
if (mFile.exists() && mFile.canRead() && !mBackupFile.exists()) {
Map map = null;
long lastModified = mFile.lastModified();
Expand All @@ -72,15 +71,15 @@ private void loadFromDiskLocked() {
str = new BufferedInputStream(new FileInputStream(mFile), 16 * 1024);
map = XmlUtils.readMapXml(str);
} catch (Throwable ex) {
Util.bug(null, ex);
Util.log(null, Log.WARN, "Error reading " + mFile + ": " + ex);
} finally {
if (str != null) {
try {
str.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Throwable ignored) {
Util.bug(null, ignored);
} catch (Throwable ex) {
Util.log(null, Log.WARN, "Error closing " + mFile + ": " + ex);
}
}
}
Expand All @@ -92,14 +91,18 @@ private void loadFromDiskLocked() {
notifyAll();
}
}

if (!mLoaded)
try {
Thread.sleep(200);
Util.log(null, Log.WARN, "Load " + mFile + " try " + tries);
if (tries < cMaxTries)
Thread.sleep(cTryWaitMs);
Util.log(null, Log.WARN, "Load " + mFile + " try=" + tries + " exists=" + mFile.exists()
+ " readable=" + mFile.canRead() + " backup=" + mBackupFile.exists());
} catch (Throwable ex) {
Util.bug(null, ex);
}
}

if (!mLoaded) {
mLoaded = true;
mMap = new HashMap<String, Object>();
Expand Down

0 comments on commit 025265f

Please sign in to comment.