diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-01-05 15:20:58 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-01-05 15:20:58 +0100 |
| commit | 36b98d48f50068c3cf2359c3caff49a690c1961c (patch) | |
| tree | 2f20fbcec3c88d38ed407d8b37f091dfd62a5b76 /main/src/cgeo/geocaching/DataStore.java | |
| parent | d4eaf2dd31d8b2d406ffbb83b84eff7cc5fdd5b9 (diff) | |
| parent | 2ddbd545f27f977e5c127179cb71c03f5c08cf5b (diff) | |
| download | cgeo-36b98d48f50068c3cf2359c3caff49a690c1961c.zip cgeo-36b98d48f50068c3cf2359c3caff49a690c1961c.tar.gz cgeo-36b98d48f50068c3cf2359c3caff49a690c1961c.tar.bz2 | |
Merge branch 'release' into upstream
Diffstat (limited to 'main/src/cgeo/geocaching/DataStore.java')
| -rw-r--r-- | main/src/cgeo/geocaching/DataStore.java | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index 0d80aea..acbf950 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(); + } } } |
