aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/DataStore.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-01-05 15:19:09 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-01-05 15:20:43 +0100
commit2ddbd545f27f977e5c127179cb71c03f5c08cf5b (patch)
tree6183336886ad7518569c9dd934571fa95de3ef91 /main/src/cgeo/geocaching/DataStore.java
parenta69a89f0f1e8faf2ade4b86ceb0c86a11727c8d5 (diff)
downloadcgeo-2ddbd545f27f977e5c127179cb71c03f5c08cf5b.zip
cgeo-2ddbd545f27f977e5c127179cb71c03f5c08cf5b.tar.gz
cgeo-2ddbd545f27f977e5c127179cb71c03f5c08cf5b.tar.bz2
work on #2315: preserve corrupted database on SD storage
Also, switch the DB to internal storage if we could not use external one.
Diffstat (limited to 'main/src/cgeo/geocaching/DataStore.java')
-rw-r--r--main/src/cgeo/geocaching/DataStore.java41
1 files changed, 28 insertions, 13 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java
index 11efbd2..55a2025 100644
--- a/main/src/cgeo/geocaching/DataStore.java
+++ b/main/src/cgeo/geocaching/DataStore.java
@@ -301,19 +301,34 @@ public class DataStore {
database = dbHelper.getWritableDatabase();
} catch (Exception e) {
Log.e("DataStore.init: unable to open database for R/W", e);
- // Attempt to recreate the database if opening has failed
- final File path = databasePath();
- final File corruptedPath = new File(path + ".corrupted");
- if (path.renameTo(corruptedPath)) {
- Log.i("DataStore.init: renamed " + path + " into " + corruptedPath);
- try {
- database = dbHelper.getWritableDatabase();
- } catch (Exception f) {
- Log.e("DataStore.init: unable to recreate database and open it for R/W", f);
- }
- } else {
- Log.e("DataStore.init: unable to rename corrupted database");
- }
+ recreateDatabase(dbHelper);
+
+ }
+ }
+
+ /**
+ * Attempt to recreate the database if opening has failed
+ *
+ * @param dbHelper dbHelper to use to reopen the database
+ */
+ private static void recreateDatabase(final DbHelper dbHelper) {
+ final File dbPath = databasePath();
+ final File corruptedPath = new File(LocalStorage.getStorage(), dbPath.getName() + ".corrupted");
+ if (LocalStorage.copy(dbPath, corruptedPath)) {
+ Log.i("DataStore.init: renamed " + dbPath + " into " + corruptedPath);
+ } else {
+ Log.e("DataStore.init: unable to rename corrupted database");
+ }
+ try {
+ database = dbHelper.getWritableDatabase();
+ } catch (Exception f) {
+ Log.e("DataStore.init: unable to recreate database and open it for R/W", f);
+ if (Settings.isDbOnSDCard()) {
+ Log.i("DataStore.init: trying to switch to internal database");
+ Settings.setDbOnSDCard(false);
+ // Since the DB is now internal, we can retry the full procedure.
+ init();
+ }
}
}