WIP: some more query work

This commit is contained in:
cpfeiffer 2016-05-08 22:58:50 +02:00
parent cc5941f7eb
commit 21cafa83d8
2 changed files with 42 additions and 37 deletions

View File

@ -1,8 +1,5 @@
package nodomain.freeyourgadget.gadgetbridge.devices;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import de.greenrobot.dao.AbstractDao;
@ -12,20 +9,18 @@ import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_PROVIDER;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TIMESTAMP;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GBACTIVITYSAMPLES;
public abstract class AbstractSampleProvider<T extends ActivitySample> implements SampleProvider {
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
public abstract class AbstractSampleProvider implements SampleProvider {
public List<ActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
public List<T> getAllActivitySamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
}
public List<ActivitySample> getActivitySamples(int tsFrom, int tsTo) {
public List<T> getActivitySamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
}
public List<ActivitySample> getSleepSamples(int tsFrom, int tsTo) {
public List<T> getSleepSamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP);
}
@ -47,41 +42,57 @@ public abstract class AbstractSampleProvider implements SampleProvider {
}
protected List<ActivitySample> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
QueryBuilder qb = getSampleDao().queryBuilder();
qb.where(MiBandActivitySampleDao.Properties.Timestamp.ge(timestamp_from);
qb.and(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to);
addClauseForActivityType(qb, activityType);
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));
return qb.build().list();
}
private void addClauseForActivityType(QueryBuilder qb, int activityTypes) {
private WhereCondition[] getClauseForActivityType(QueryBuilder qb, int activityTypes) {
if (activityTypes == ActivityKind.TYPE_ALL) {
return; // no further restriction
return NO_CONDITIONS;
}
int[] dbActivityTypes = ActivityKind.mapToDBActivityTypes(activityTypes, this);
WhereCondition[] activityTypeConditions = getActivityTypeConditions(dbActivityTypes);
qb.and(qb.or(activityTypeConditions));
WhereCondition activityTypeCondition = getActivityTypeConditions(qb, dbActivityTypes);
return new WhereCondition[] { activityTypeCondition };
}
private WhereCondition[] getActivityTypeConditions(int[] dbActivityTypes) {
WhereCondition[] result = new WhereCondition[dbActivityTypes.length];
for (int i = 0; i < dbActivityTypes.length; i++) {
result[i] = MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[i]);
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;
}
return result;
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]);
}
return qb.or(MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]),
MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[1]),
trailingConditions);
}
public int fetchLatestTimestamp() {
try (SQLiteDatabase db = this.getReadableDatabase()) {
try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, new String[]{KEY_TIMESTAMP}, KEY_PROVIDER + "=" + String.valueOf(provider.getID()), null, null, null, KEY_TIMESTAMP + " DESC", "1")) {
if (cursor.moveToFirst()) {
return cursor.getInt(0);
}
}
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;
}
protected abstract AbstractDao<? extends ActivitySample,?> getSampleDao();
protected abstract AbstractDao<T,?> getSampleDao();
}

View File

@ -76,18 +76,12 @@ public class GBActivitySample2 implements ActivitySample {
return getProvider().normalizeType(getRawKind());
}
@Override
public int getCustomValue() {
return customValue;
}
@Override
public String toString() {
return "GBActivitySample{" +
"timestamp=" + DateTimeUtils.formatDateTime(DateTimeUtils.parseTimeStamp(timestamp)) +
", intensity=" + getIntensity() +
", steps=" + getSteps() +
", customValue=" + getCustomValue() +
", type=" + getKind() +
'}';
}