2016-05-16 23:54:51 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.database;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-06-16 21:54:53 +02:00
|
|
|
import android.database.Cursor;
|
2016-05-16 23:54:51 +02:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
2016-06-16 21:54:53 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.LockHandler;
|
2016-05-16 23:54:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.schema.SchemaMigration;
|
2016-06-16 21:54:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
2016-05-16 23:54:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoMaster;
|
2016-06-16 21:54:53 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.User;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.HeartRateSample;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_CUSTOM_SHORT;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_INTENSITY;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_PROVIDER;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_STEPS;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TIMESTAMP;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TYPE;
|
|
|
|
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GBACTIVITYSAMPLES;
|
2016-05-16 23:54:51 +02:00
|
|
|
|
|
|
|
public class DBOpenHelper extends DaoMaster.OpenHelper {
|
2016-06-16 21:54:53 +02:00
|
|
|
private final String updaterClassNamePrefix;
|
|
|
|
private final Context context;
|
|
|
|
|
2016-05-16 23:54:51 +02:00
|
|
|
public DBOpenHelper(Context context, String dbName, SQLiteDatabase.CursorFactory factory) {
|
|
|
|
super(context, dbName, factory);
|
2016-06-16 21:54:53 +02:00
|
|
|
updaterClassNamePrefix = dbName + "Update_";
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean importOldDbIfNecessary(DaoMaster daoMaster, DBHandler targetDBHandler) {
|
|
|
|
DaoSession tempSession = daoMaster.newSession();
|
|
|
|
try {
|
|
|
|
if (isEmpty(tempSession)) {
|
|
|
|
importActivityDatabaseInto(tempSession, targetDBHandler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
tempSession.clear();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isEmpty(DaoSession session) {
|
|
|
|
long totalSamplesCount = session.getMiBandActivitySampleDao().count();
|
|
|
|
totalSamplesCount += session.getPebbleActivitySampleDao().count();
|
|
|
|
return totalSamplesCount == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void importActivityDatabaseInto(DaoSession session, DBHandler targetDBHandler) {
|
|
|
|
ActivityDatabaseHandler handler = new ActivityDatabaseHandler(getContext());
|
|
|
|
|
|
|
|
try (SQLiteDatabase db = handler.getReadableDatabase()) {
|
|
|
|
User user = DBHelper.getUser(session);
|
|
|
|
for (DeviceCoordinator coordinator : DeviceHelper.getInstance().getAllCoordinators()) {
|
|
|
|
AbstractSampleProvider<? extends AbstractActivitySample> sampleProvider = (AbstractSampleProvider<? extends AbstractActivitySample>) coordinator.getSampleProvider(targetDBHandler);
|
|
|
|
importActivitySamples(db, session, sampleProvider, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private <T extends AbstractActivitySample> void importActivitySamples(SQLiteDatabase fromDb, DaoSession targetSession, AbstractSampleProvider<T> sampleProvider, User user) {
|
|
|
|
String order = "timestamp";
|
|
|
|
final String where = "provider=" + sampleProvider.getID();
|
|
|
|
|
|
|
|
try (Cursor cursor = fromDb.query(TABLE_GBACTIVITYSAMPLES, null, where, null, null, null, order)) {
|
|
|
|
int colTimeStamp = cursor.getColumnIndex(KEY_TIMESTAMP);
|
|
|
|
int colIntensity = cursor.getColumnIndex(KEY_INTENSITY);
|
|
|
|
int colSteps = cursor.getColumnIndex(KEY_STEPS);
|
|
|
|
int colType = cursor.getColumnIndex(KEY_TYPE);
|
|
|
|
int colCustomShort = cursor.getColumnIndex(KEY_CUSTOM_SHORT);
|
|
|
|
Long userId = user.getId();
|
|
|
|
List<T> newSamples = new ArrayList<>(cursor.getCount());
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
T newSample = sampleProvider.createActivitySample();
|
|
|
|
newSample.setUserId(userId);
|
|
|
|
newSample.setDeviceId(deviceId);
|
|
|
|
newSample.setTimestamp(cursor.getInt(colTimeStamp));
|
|
|
|
newSample.setRawKind(cursor.getInt(colType));
|
|
|
|
newSample.setProvider(sampleProvider);
|
|
|
|
newSample.setRawIntensity(cursor.getInt(colIntensity));
|
|
|
|
newSample.setSteps(cursor.getInt(colSteps));
|
|
|
|
|
|
|
|
int hrValue = cursor.getInt(colCustomShort);
|
|
|
|
if (newSample instanceof HeartRateSample) {
|
|
|
|
((HeartRateSample)newSample).setHeartRate(hrValue);
|
|
|
|
}
|
|
|
|
newSamples.add(newSample);
|
|
|
|
}
|
|
|
|
sampleProvider.getSampleDao().insertInTx(newSamples, true);
|
|
|
|
}
|
2016-05-16 23:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2016-06-16 21:54:53 +02:00
|
|
|
new SchemaMigration(updaterClassNamePrefix).onUpgrade(db, oldVersion, newVersion);
|
2016-05-16 23:54:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2016-06-16 21:54:53 +02:00
|
|
|
new SchemaMigration(updaterClassNamePrefix).onDowngrade(db, oldVersion, newVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Context getContext() {
|
|
|
|
return context;
|
2016-05-16 23:54:51 +02:00
|
|
|
}
|
|
|
|
}
|