2016-05-05 10:19:01 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import de.greenrobot.dao.AbstractDao;
|
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
|
|
|
import de.greenrobot.dao.query.WhereCondition;
|
2016-05-13 23:47:47 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
2016-05-08 23:14:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
2016-05-05 10:19:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
|
2016-05-16 23:36:54 +02:00
|
|
|
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
|
2016-05-08 22:58:50 +02:00
|
|
|
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
|
2016-05-08 23:14:58 +02:00
|
|
|
private final DaoSession mSession;
|
|
|
|
|
|
|
|
protected AbstractSampleProvider(DaoSession session) {
|
|
|
|
mSession = session;
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:41:34 +02:00
|
|
|
public DaoSession getSession() {
|
2016-05-08 23:14:58 +02:00
|
|
|
return mSession;
|
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getAllActivitySamples(int timestamp_from, int timestamp_to) {
|
2016-05-05 10:19:01 +02:00
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getActivitySamples(int timestamp_from, int timestamp_to) {
|
2016-05-05 10:19:01 +02:00
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getSleepSamples(int timestamp_from, int timestamp_to) {
|
2016-05-05 10:19:01 +02:00
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP);
|
|
|
|
}
|
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
2016-05-16 23:00:04 +02:00
|
|
|
public int fetchLatestTimestamp() {
|
|
|
|
QueryBuilder<T> qb = getSampleDao().queryBuilder();
|
|
|
|
qb.orderDesc(MiBandActivitySampleDao.Properties.Timestamp);
|
|
|
|
qb.limit(1);
|
|
|
|
List<T> list = qb.build().list();
|
|
|
|
if (list.size() >= 1) {
|
|
|
|
return list.get(0).getTimestamp();
|
|
|
|
}
|
|
|
|
return -1;
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
2016-05-16 23:36:54 +02:00
|
|
|
public void addGBActivitySample(T activitySample) {
|
|
|
|
getSampleDao().insert(activitySample);
|
2016-05-13 23:47:47 +02:00
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
2016-05-16 23:36:54 +02:00
|
|
|
public void addGBActivitySamples(T[] activitySamples) {
|
|
|
|
getSampleDao().insertInTx(activitySamples);
|
2016-05-13 23:47:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
// @Override
|
|
|
|
// public void close() {
|
|
|
|
// // TESTING: NOOP
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public SQLiteOpenHelper getHelper() {
|
|
|
|
// // TESTING: NOOP
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public void release() {
|
|
|
|
// // TESTING: NOOP
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public List<ActivitySample> getAllActivitySamples(int tsFrom, int tsTo, SampleProvider provider) {
|
|
|
|
// return (List<ActivitySample>) getGBActivitySamples(tsFrom, tsTo, ActivityKind.TYPE_ALL);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public List<ActivitySample> getActivitySamples(int tsFrom, int tsTo, SampleProvider provider) {
|
|
|
|
// return (List<ActivitySample>) getGBActivitySamples(tsFrom, tsTo, ActivityKind.TYPE_ACTIVITY);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public List<ActivitySample> getSleepSamples(int tsFrom, int tsTo, SampleProvider provider) {
|
|
|
|
// return (List<ActivitySample>) getGBActivitySamples(tsFrom, tsTo, ActivityKind.TYPE_SLEEP);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @Override
|
|
|
|
// public SQLiteDatabase getWritableDatabase() {
|
|
|
|
// // TESTING: NOOP
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
//
|
2016-05-16 23:36:54 +02:00
|
|
|
|
|
|
|
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) {
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
//// SQLiteDatabase getWritableDatabase();
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
|
|
|
|
QueryBuilder<T> qb = getSampleDao().queryBuilder();
|
|
|
|
qb.where(MiBandActivitySampleDao.Properties.Timestamp.ge(timestamp_from))
|
|
|
|
.where(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to), getClauseForActivityType(qb, activityType));
|
2016-05-16 23:00:04 +02:00
|
|
|
List<T> samples = qb.build().list();
|
|
|
|
for (T sample : samples) {
|
2016-05-16 23:36:54 +02:00
|
|
|
sample.setProvider(this);
|
2016-05-16 23:00:04 +02:00
|
|
|
}
|
|
|
|
return samples;
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
private WhereCondition[] getClauseForActivityType(QueryBuilder qb, int activityTypes) {
|
2016-05-05 10:19:01 +02:00
|
|
|
if (activityTypes == ActivityKind.TYPE_ALL) {
|
2016-05-08 22:58:50 +02:00
|
|
|
return NO_CONDITIONS;
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int[] dbActivityTypes = ActivityKind.mapToDBActivityTypes(activityTypes, this);
|
2016-05-08 22:58:50 +02:00
|
|
|
WhereCondition activityTypeCondition = getActivityTypeConditions(qb, dbActivityTypes);
|
|
|
|
return new WhereCondition[] { activityTypeCondition };
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
private WhereCondition getActivityTypeConditions(QueryBuilder qb, int[] dbActivityTypes) {
|
|
|
|
// What a crappy QueryBuilder API ;-( QueryBuilder.or(WhereCondition[]) with a runtime array length
|
|
|
|
// check would have worked just fine.
|
|
|
|
if (dbActivityTypes.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (dbActivityTypes.length == 1) {
|
|
|
|
return MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]);
|
|
|
|
}
|
|
|
|
if (dbActivityTypes.length == 2) {
|
|
|
|
return qb.or(MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]),
|
|
|
|
MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[1]));
|
|
|
|
}
|
|
|
|
final int offset = 2;
|
|
|
|
int len = dbActivityTypes.length - offset;
|
|
|
|
WhereCondition[] trailingConditions = new WhereCondition[len];
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
trailingConditions[i] = MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[i + offset]);
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
2016-05-08 22:58:50 +02:00
|
|
|
return qb.or(MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]),
|
|
|
|
MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[1]),
|
|
|
|
trailingConditions);
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
protected abstract AbstractDao<T,?> getSampleDao();
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|