add ContentDatabaseManager
check existing timestamp of installed db.
if this timestamp does not exists (e.g. when transitioning from pwa 1.15) a timestamp of 0 is returned.
// db timestamp
// N.B. returns 0 if content_info table not found
private Integer existingDbTimestamp(String dbName) {
Integer dbTimestamp = 0;
SQLiteDatabase the_db = null;
Cursor c = null;
try {
the_db = openDatabase(dbName);
String tableName = "content_info";
String[] cols = { "timestamp" }; // case-sensitive
String where = "";
String[] args = { };
// "select timestamp from content_info"
c = the_db.query(tableName, cols, where, args, null, null, null, null);
if (c.moveToFirst()) {
do {
dbTimestamp = c.getInt(c.getColumnIndex("timestamp"));
}
while (c.moveToNext());
}
}
catch (SQLiteException ex) {
Log.e("SQLiteException", "ex=" + ex);
}
finally {
if (c != null) {
c.close();
}
if (the_db != null) {
the_db.close();
}
}
return dbTimestamp;
}
copy for first time if size is 0.
copy if newer timestamp.
// copy db stuff
public File copyDatabaseIfNecessary(Context ctx, String dbName) {
// TODO do the copy on a different thread
File f = ctx.getDatabasePath(dbName);
long size = f.length();
Log.i("PrepwareDebug", String.format("db size=%d", size));
Boolean shouldCopyDatabase = false;
// a size of 0 means we haven't copied the db over the first time
if (size == 0)
{
shouldCopyDatabase = true;
String msg = String.format(Locale.getDefault(), "copy %s for the first time", dbName);
//Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
//Toast.makeText(ctx, "initializing the content database", Toast.LENGTH_SHORT).show();
}
else // size is non-zero so a db exists
{
// only copy the db from the assets folder if the existing timestamp
// indicates an out-of-date database
Integer existingDbTimeStamp = existingDbTimestamp(dbName);
Integer latestDbTimestamp = latestDbTimestamp();
String msg = String.format(Locale.getDefault(),
"existing timestamp:%d, latest timestamp %d", existingDbTimeStamp, latestDbTimestamp);
Log.i("PrepwareDebug", msg);
if (existingDbTimeStamp < latestDbTimestamp) {
shouldCopyDatabase = true;
msg = String.format(Locale.getDefault(), "copy new version of %s", dbName);
//Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
//Toast.makeText(ctx, "updating the content database", Toast.LENGTH_SHORT).show();
}
}
if (shouldCopyDatabase) {
Log.i("PrepwareDebug", dbName + " will be copied");
// this creates a default db (and makes the path available)
SQLiteDatabase db = ctx.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
db.close();
try {
copyLocalAsset(ctx, dbName, f.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("PrepwareDebug", "Exception=" + e);
}
} else {
Log.i("PrepwareDebug", dbName + " already exists and will not will be copied");
//String msg = String.format(Locale.getDefault(), "most recent version of %s already exists", dbName);
//Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
}
return f;
}
##################################################
# first run
##################################################
02-12 09:14:28.040 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: db size=0
02-12 09:14:28.040 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: falcon.db will be copied
02-12 09:14:28.091 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: falcon db fileExists=true
02-12 09:14:28.092 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: db size=0
02-12 09:14:28.092 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: questions.jet will be copied
02-12 09:14:28.577 16059-16059/com.asa2fly.prepware.airframe I/PrepwareDebug: questions db fileExists=true
##################################################
# second run
##################################################
02-12 09:15:49.732 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: db size=92160
02-12 09:15:49.738 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: existing timestamp:1455278400, latest timestamp 1455278400
02-12 09:15:49.738 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: falcon.db already exists and will not will be copied
02-12 09:15:49.738 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: falcon db fileExists=true
02-12 09:15:49.738 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: db size=8298496
02-12 09:15:49.740 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: existing timestamp:1455278400, latest timestamp 1455278400
02-12 09:15:49.740 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: questions.jet already exists and will not will be copied
02-12 09:15:49.740 16838-16838/com.asa2fly.prepware.airframe I/PrepwareDebug: questions db fileExists=true