2015-05-30 17:28:03 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2015-07-10 00:31:45 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBActivitySample;
|
2015-06-06 00:10:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-07-10 00:31:45 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.schema.ActivityDBCreationScript;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.*;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
public class ActivityDatabaseHandler extends SQLiteOpenHelper {
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ActivityDatabaseHandler.class);
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
private static final int DATABASE_VERSION = 5;
|
2015-05-30 17:28:03 +02:00
|
|
|
|
|
|
|
public ActivityDatabaseHandler(Context context) {
|
|
|
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2015-07-10 00:31:45 +02:00
|
|
|
try {
|
|
|
|
ActivityDBCreationScript script = new ActivityDBCreationScript();
|
|
|
|
script.createSchema(db);
|
|
|
|
} catch (RuntimeException ex) {
|
|
|
|
LOG.error("Error creatomg database", ex);
|
|
|
|
Toast.makeText(GBApplication.getContext(), "Error creating database.", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
try {
|
|
|
|
for (int i = oldVersion + 1; i <= newVersion; i++) {
|
|
|
|
DBUpdateScript updater = getUpdateScript(db, i);
|
|
|
|
if (updater != null) {
|
|
|
|
LOG.info("upgrading activity database to version " + i);
|
|
|
|
updater.upgradeSchema(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG.info("activity database is now at version " + newVersion);
|
|
|
|
} catch (RuntimeException ex) {
|
|
|
|
LOG.error("Error upgrading db version. ", ex);
|
|
|
|
Toast.makeText(GBApplication.getContext(), "Error upgrading database.", Toast.LENGTH_SHORT).show();
|
|
|
|
throw ex; // reject upgrade
|
2015-06-06 00:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 17:28:03 +02:00
|
|
|
@Override
|
2015-07-10 00:31:45 +02:00
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
|
|
try {
|
|
|
|
for (int i = oldVersion; i >= newVersion; i--) {
|
|
|
|
DBUpdateScript updater = getUpdateScript(db, i);
|
|
|
|
if (updater != null) {
|
|
|
|
LOG.info("downgrading activity database to version " + (i - 1));
|
|
|
|
updater.downgradeSchema(db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG.info("activity database is now at version " + newVersion);
|
|
|
|
} catch (RuntimeException ex) {
|
|
|
|
LOG.error("Error downgrading db version. ", ex);
|
|
|
|
Toast.makeText(GBApplication.getContext(), "Error downgrading database.", Toast.LENGTH_SHORT).show();
|
|
|
|
throw ex; // reject downgrade
|
2015-06-04 21:37:48 +02:00
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 00:31:45 +02:00
|
|
|
private DBUpdateScript getUpdateScript(SQLiteDatabase db, int version) {
|
|
|
|
try {
|
|
|
|
Class<?> updateClass = getClass().getClassLoader().loadClass(getClass().getPackage().getName() + ".schema.ActivityDBUpdate_" + version);
|
|
|
|
return (DBUpdateScript) updateClass.newInstance();
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
} catch (InstantiationException | IllegalAccessException e) {
|
|
|
|
throw new RuntimeException("Error instantiating DBUpdate class for version " + version, e);
|
|
|
|
}
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
|
|
|
|
public void addGBActivitySample(GBActivitySample GBActivitySample) {
|
2015-06-04 23:45:46 +02:00
|
|
|
try (SQLiteDatabase db = this.getWritableDatabase()) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(KEY_TIMESTAMP, GBActivitySample.getTimestamp());
|
|
|
|
values.put(KEY_PROVIDER, GBActivitySample.getProvider());
|
|
|
|
values.put(KEY_INTENSITY, GBActivitySample.getIntensity());
|
|
|
|
values.put(KEY_STEPS, GBActivitySample.getSteps());
|
|
|
|
values.put(KEY_TYPE, GBActivitySample.getType());
|
|
|
|
|
|
|
|
db.insert(TABLE_GBACTIVITYSAMPLES, null, values);
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addGBActivitySample(int timestamp, byte provider, short intensity, byte steps, byte type) {
|
2015-06-04 23:45:46 +02:00
|
|
|
try (SQLiteDatabase db = this.getWritableDatabase()) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(KEY_TIMESTAMP, timestamp);
|
|
|
|
values.put(KEY_PROVIDER, provider);
|
|
|
|
values.put(KEY_INTENSITY, intensity);
|
|
|
|
values.put(KEY_STEPS, steps);
|
|
|
|
values.put(KEY_TYPE, type);
|
|
|
|
|
|
|
|
db.insert(TABLE_GBACTIVITYSAMPLES, null, values);
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<GBActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, byte provider) {
|
|
|
|
if (timestamp_to == -1) {
|
2015-06-04 23:45:46 +02:00
|
|
|
timestamp_to = Integer.MAX_VALUE; // dont know what happens when I use more than max of a signed int
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
ArrayList<GBActivitySample> GBActivitySampleList = new ArrayList<GBActivitySample>();
|
|
|
|
String selectQuery = "SELECT * FROM " + TABLE_GBACTIVITYSAMPLES
|
2015-05-30 17:40:23 +02:00
|
|
|
+ " where (provider=" + provider + " and timestamp>=" + timestamp_from + " and timestamp<=" + timestamp_to + ") order by timestamp";
|
2015-05-30 17:28:03 +02:00
|
|
|
|
2015-06-04 23:45:46 +02:00
|
|
|
try (SQLiteDatabase db = this.getWritableDatabase()) {
|
|
|
|
Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
|
|
|
|
|
if (cursor.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
GBActivitySample GBActivitySample = new GBActivitySample(
|
|
|
|
cursor.getInt(cursor.getColumnIndex(KEY_TIMESTAMP)),
|
|
|
|
(byte) cursor.getInt(cursor.getColumnIndex(KEY_PROVIDER)),
|
|
|
|
(short) cursor.getInt(cursor.getColumnIndex(KEY_INTENSITY)),
|
|
|
|
(byte) cursor.getInt(cursor.getColumnIndex(KEY_STEPS)),
|
|
|
|
(byte) cursor.getInt(cursor.getColumnIndex(KEY_TYPE)));
|
|
|
|
GBActivitySampleList.add(GBActivitySample);
|
|
|
|
} while (cursor.moveToNext());
|
|
|
|
}
|
2015-05-30 17:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return GBActivitySampleList;
|
|
|
|
}
|
2015-06-12 21:26:11 +02:00
|
|
|
}
|