diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java index df2353d0..b5737568 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java @@ -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 implements SampleProvider { + private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0]; -public abstract class AbstractSampleProvider implements SampleProvider { - public List getAllActivitySamples(int timestamp_from, int timestamp_to) { + public List getAllActivitySamples(int timestamp_from, int timestamp_to) { return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL); } - public List getActivitySamples(int tsFrom, int tsTo) { + public List getActivitySamples(int timestamp_from, int timestamp_to) { return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY); } - public List getSleepSamples(int tsFrom, int tsTo) { + public List 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 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 getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) { + QueryBuilder 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 qb = getSampleDao().queryBuilder(); + qb.orderDesc(MiBandActivitySampleDao.Properties.Timestamp); + qb.limit(1); + List list = qb.build().list(); + if (list.size() >= 1) { + return list.get(0).getTimestamp(); } return -1; } - protected abstract AbstractDao getSampleDao(); + protected abstract AbstractDao getSampleDao(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBActivitySample2.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBActivitySample2.java index d01b322e..fd0bd2ce 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBActivitySample2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBActivitySample2.java @@ -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() + '}'; }