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; package nodomain.freeyourgadget.gadgetbridge.devices;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.List; import java.util.List;
import de.greenrobot.dao.AbstractDao; 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.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_PROVIDER; public abstract class AbstractSampleProvider<T extends ActivitySample> implements SampleProvider {
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TIMESTAMP; private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GBACTIVITYSAMPLES;
public abstract class AbstractSampleProvider implements SampleProvider { public List<T> getAllActivitySamples(int timestamp_from, int timestamp_to) {
public List<ActivitySample> getAllActivitySamples(int timestamp_from, int timestamp_to) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL); 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); 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); 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) { protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
QueryBuilder qb = getSampleDao().queryBuilder(); QueryBuilder<T> qb = getSampleDao().queryBuilder();
qb.where(MiBandActivitySampleDao.Properties.Timestamp.ge(timestamp_from); qb.where(MiBandActivitySampleDao.Properties.Timestamp.ge(timestamp_from))
qb.and(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to); .where(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to), getClauseForActivityType(qb, activityType));
addClauseForActivityType(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) { if (activityTypes == ActivityKind.TYPE_ALL) {
return; // no further restriction return NO_CONDITIONS;
} }
int[] dbActivityTypes = ActivityKind.mapToDBActivityTypes(activityTypes, this); int[] dbActivityTypes = ActivityKind.mapToDBActivityTypes(activityTypes, this);
WhereCondition[] activityTypeConditions = getActivityTypeConditions(dbActivityTypes); WhereCondition activityTypeCondition = getActivityTypeConditions(qb, dbActivityTypes);
qb.and(qb.or(activityTypeConditions)); return new WhereCondition[] { activityTypeCondition };
} }
private WhereCondition[] getActivityTypeConditions(int[] dbActivityTypes) { private WhereCondition getActivityTypeConditions(QueryBuilder qb, int[] dbActivityTypes) {
WhereCondition[] result = new WhereCondition[dbActivityTypes.length]; // What a crappy QueryBuilder API ;-( QueryBuilder.or(WhereCondition[]) with a runtime array length
for (int i = 0; i < dbActivityTypes.length; i++) { // check would have worked just fine.
result[i] = MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[i]); 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() { public int fetchLatestTimestamp() {
try (SQLiteDatabase db = this.getReadableDatabase()) { QueryBuilder<T> qb = getSampleDao().queryBuilder();
try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, new String[]{KEY_TIMESTAMP}, KEY_PROVIDER + "=" + String.valueOf(provider.getID()), null, null, null, KEY_TIMESTAMP + " DESC", "1")) { qb.orderDesc(MiBandActivitySampleDao.Properties.Timestamp);
if (cursor.moveToFirst()) { qb.limit(1);
return cursor.getInt(0); List<T> list = qb.build().list();
} if (list.size() >= 1) {
} return list.get(0).getTimestamp();
} }
return -1; 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()); return getProvider().normalizeType(getRawKind());
} }
@Override
public int getCustomValue() {
return customValue;
}
@Override @Override
public String toString() { public String toString() {
return "GBActivitySample{" + return "GBActivitySample{" +
"timestamp=" + DateTimeUtils.formatDateTime(DateTimeUtils.parseTimeStamp(timestamp)) + "timestamp=" + DateTimeUtils.formatDateTime(DateTimeUtils.parseTimeStamp(timestamp)) +
", intensity=" + getIntensity() + ", intensity=" + getIntensity() +
", steps=" + getSteps() + ", steps=" + getSteps() +
", customValue=" + getCustomValue() +
", type=" + getKind() + ", type=" + getKind() +
'}'; '}';
} }