Skip to content

Commit

Permalink
* #190: Backup/Restore lat/long/tags/rating in local DB when recreati…
Browse files Browse the repository at this point in the history
…ng apm-database
  • Loading branch information
k3b committed Apr 17, 2021
1 parent 6d7a3a4 commit 8022af6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper instance = null;
private static DatabaseContext databaseContext = null;

private final Context context;

public DatabaseHelper(final Context context, final String databaseName) {
super(context, databaseName, null, DatabaseHelper.DATABASE_VERSION);
this.context = context.getApplicationContext();
}

public static SQLiteDatabase getWritableDatabase(Context context) {
Expand All @@ -72,7 +75,8 @@ private static DatabaseHelper getInstance(Context context) {
}


public static void version2Upgrade_ReCreateMediaDbTable(final SQLiteDatabase db) {
public static void version2Upgrade_ReCreateMediaDbTable(Context context, final SQLiteDatabase db) {
MediaDBRepository.saveSyncStats(context, 1, 1);
execSql(db, "(Re)CreateMediaDbTable:", MediaDBRepository.Impl.DDL);
}

Expand Down Expand Up @@ -130,7 +134,7 @@ private static void execSql(SQLiteDatabase db, String dbgContext, String... ddlS
public void onCreate(final SQLiteDatabase db) {
execSql(db, "First Create DB: ", TransactionLogSql.CREATE_TABLE);

version2Upgrade_ReCreateMediaDbTable(db);
version2Upgrade_ReCreateMediaDbTable(context, db);
}

@Override
Expand All @@ -139,7 +143,7 @@ public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
Log.w(this.getClass().toString(), "Upgrading database from version "
+ oldVersion + " to " + newVersion + ". (Old data is kept.)");
if (oldVersion < DatabaseHelper.DATABASE_VERSION_2_MEDIA_DB_COPY) {
version2Upgrade_ReCreateMediaDbTable(db);
version2Upgrade_ReCreateMediaDbTable(context, db);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public MediaContent2DBUpdateService(Context context, SQLiteDatabase writableData
}

public void clearMediaCopy() {
DatabaseHelper.version2Upgrade_ReCreateMediaDbTable(writableDatabase);
DatabaseHelper.version2Upgrade_ReCreateMediaDbTable(context, writableDatabase);
}

public int rebuild(Context context, IProgessListener progessListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ public static ContentValues getContentValues(String fullFilePathFilter, ContentV
}


public static void saveSyncStats(Context context, long maxDateAddedSecs, long maxDateUpdatedSecs) {
SharedPreferences prefsInstance = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());

SharedPreferences.Editor prefs = prefsInstance.edit();
if (maxDateUpdatedSecs > 0) prefs.putLong("maxDateUpdatedSecs", maxDateUpdatedSecs + 1);
if (maxDateAddedSecs > 0) prefs.putLong("maxDateAddedSecs", maxDateAddedSecs + 1);
prefs.apply();
}

public static class Impl {
public static final String DATABASE_TABLE_NAME = "files";
public static final String DATABASE_TABLE_NAME_BACKUP = "backup";
Expand Down Expand Up @@ -752,7 +762,7 @@ public static int updateMediaCopy(
} // while over all old items
db.setTransactionSuccessful(); // This commits the transaction if there were no exceptions

saveStats(context, maxDateAddedSecs, maxDateUpdatedSecs);
saveSyncStats(context, maxDateAddedSecs, maxDateUpdatedSecs);

if (Global.debugEnabledSql) {
java.util.Date endTime = new java.util.Date();
Expand Down Expand Up @@ -786,16 +796,6 @@ public static int updateMediaCopy(
return progress;
}

private static void saveStats(Context context, long maxDateAddedSecs, long maxDateUpdatedSecs) {
SharedPreferences prefsInstance = PreferenceManager
.getDefaultSharedPreferences(context.getApplicationContext());

SharedPreferences.Editor prefs = prefsInstance.edit();
if (maxDateUpdatedSecs > 0) prefs.putLong("maxDateUpdatedSecs", maxDateUpdatedSecs + 1);
if (maxDateAddedSecs > 0) prefs.putLong("maxDateAddedSecs", maxDateAddedSecs + 1);
prefs.apply();
}

protected static long getDateInSecs(Cursor c, int colPosition) {
long dateInSecs = (c.isNull(colPosition)) ? 0 : c.getLong(colPosition);
if (dateInSecs > nextMonthTimeInSecs) {
Expand All @@ -805,4 +805,5 @@ protected static long getDateInSecs(Cursor c, int colPosition) {
return dateInSecs;
}
}

}

0 comments on commit 8022af6

Please sign in to comment.