1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 10:56:50 +01:00

Cleanup, remove duplicated code, fix some TODOs

This commit is contained in:
cpfeiffer 2016-06-14 23:10:35 +02:00
parent a01507a924
commit 71d99384c1
8 changed files with 64 additions and 165 deletions

View File

@ -3,17 +3,17 @@ package nodomain.freeyourgadget.gadgetbridge.devices;
import java.util.List;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.dao.query.WhereCondition;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
/**
* Base class for all sample providers. A Sample provider is device specific and provides
* access to the device specific samples. There are both read and write operations.
* @param <T>
* @param <T> the sample type
*/
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
@ -45,7 +45,7 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
@Override
public int fetchLatestTimestamp() {
QueryBuilder<T> qb = getSampleDao().queryBuilder();
qb.orderDesc(MiBandActivitySampleDao.Properties.Timestamp);
qb.orderDesc(getTimestampSampleProperty());
qb.limit(1);
List<T> list = qb.build().list();
if (list.size() >= 1) {
@ -64,44 +64,6 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
getSampleDao().insertOrReplaceInTx(activitySamples);
}
// @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;
// }
//
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
List<T> samples = getAllActivitySamples(timestampFrom, timestampTo);
for (T sample : samples) {
@ -120,8 +82,9 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
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));
Property timestampProperty = getTimestampSampleProperty();
qb.where(timestampProperty.ge(timestamp_from))
.where(timestampProperty.le(timestamp_to), getClauseForActivityType(qb, activityType));
List<T> samples = qb.build().list();
for (T sample : samples) {
sample.setProvider(this);
@ -145,23 +108,27 @@ public abstract class AbstractSampleProvider<T extends AbstractActivitySample> i
if (dbActivityTypes.length == 0) {
return null;
}
Property rawKindProperty = getRawKindSampleProperty();
if (dbActivityTypes.length == 1) {
return MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]);
return rawKindProperty.eq(dbActivityTypes[0]);
}
if (dbActivityTypes.length == 2) {
return qb.or(MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]),
MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[1]));
return qb.or(rawKindProperty.eq(dbActivityTypes[0]),
rawKindProperty.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]);
trailingConditions[i] = rawKindProperty.eq(dbActivityTypes[i + offset]);
}
return qb.or(MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[0]),
MiBandActivitySampleDao.Properties.RawKind.eq(dbActivityTypes[1]),
return qb.or(rawKindProperty.eq(dbActivityTypes[0]),
rawKindProperty.eq(dbActivityTypes[1]),
trailingConditions);
}
protected abstract AbstractDao<T,?> getSampleDao();
protected abstract Property getRawKindSampleProperty();
protected abstract Property getTimestampSampleProperty();
}

View File

@ -1,10 +1,12 @@
package nodomain.freeyourgadget.gadgetbridge.devices.miband;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivitySample> {
@ -80,4 +82,14 @@ public class MiBandSampleProvider extends AbstractSampleProvider<MiBandActivityS
protected AbstractDao<MiBandActivitySample, ?> getSampleDao() {
return getSession().getMiBandActivitySampleDao();
}
@Override
protected Property getTimestampSampleProperty() {
return MiBandActivitySampleDao.Properties.Timestamp;
}
@Override
protected Property getRawKindSampleProperty() {
return MiBandActivitySampleDao.Properties.RawKind;
}
}

View File

@ -0,0 +1,29 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySampleDao;
public abstract class AbstractPebbleSampleProvider extends AbstractSampleProvider<PebbleActivitySample> {
protected AbstractPebbleSampleProvider(DaoSession session) {
super(session);
}
@Override
protected AbstractDao<PebbleActivitySample, ?> getSampleDao() {
return getSession().getPebbleActivitySampleDao();
}
@Override
protected Property getTimestampSampleProperty() {
return PebbleActivitySampleDao.Properties.Timestamp;
}
@Override
protected Property getRawKindSampleProperty() {
return PebbleActivitySampleDao.Properties.RawKind;
}
}

View File

@ -1,13 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import de.greenrobot.dao.AbstractDao;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class HealthSampleProvider extends AbstractSampleProvider<PebbleActivitySample> {
public class HealthSampleProvider extends AbstractPebbleSampleProvider {
public static final int TYPE_DEEP_SLEEP = 5;
public static final int TYPE_LIGHT_SLEEP = 4;
public static final int TYPE_ACTIVITY = -1;
@ -57,9 +54,4 @@ public class HealthSampleProvider extends AbstractSampleProvider<PebbleActivityS
public int getID() {
return SampleProvider.PROVIDER_PEBBLE_HEALTH;
}
@Override
protected AbstractDao<PebbleActivitySample, ?> getSampleDao() {
return getSession().getPebbleActivitySampleDao();
}
}

View File

@ -1,12 +1,9 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import de.greenrobot.dao.AbstractDao;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
public class MisfitSampleProvider extends AbstractSampleProvider<PebbleActivitySample> {
public class MisfitSampleProvider extends AbstractPebbleSampleProvider {
protected final float movementDivisor = 300f;
@ -16,7 +13,7 @@ public class MisfitSampleProvider extends AbstractSampleProvider<PebbleActivityS
@Override
public int normalizeType(int rawType) {
return (int) rawType;
return rawType;
}
@Override
@ -35,9 +32,4 @@ public class MisfitSampleProvider extends AbstractSampleProvider<PebbleActivityS
public int getID() {
return SampleProvider.PROVIDER_PEBBLE_MISFIT;
}
@Override
protected AbstractDao<PebbleActivitySample, ?> getSampleDao() {
return getSession().getPebbleActivitySampleDao();
}
}

View File

@ -1,13 +1,10 @@
package nodomain.freeyourgadget.gadgetbridge.devices.pebble;
import de.greenrobot.dao.AbstractDao;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class MorpheuzSampleProvider extends AbstractSampleProvider<PebbleActivitySample> {
public class MorpheuzSampleProvider extends AbstractPebbleSampleProvider {
// raw types
public static final int TYPE_DEEP_SLEEP = 5;
public static final int TYPE_LIGHT_SLEEP = 4;
@ -59,9 +56,4 @@ public class MorpheuzSampleProvider extends AbstractSampleProvider<PebbleActivit
public int getID() {
return SampleProvider.PROVIDER_PEBBLE_MORPHEUZ;
}
@Override
protected AbstractDao<PebbleActivitySample, ?> getSampleDao() {
return getSession().getPebbleActivitySampleDao();
}
}

View File

@ -4,6 +4,9 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
/**
* Can be removed entirely together with ActivityDatabaseHandler
*/
public class GBActivitySample implements ActivitySample {
private final int timestamp;
private final SampleProvider provider;

View File

@ -1,88 +0,0 @@
package nodomain.freeyourgadget.gadgetbridge.impl;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
public class GBActivitySample2 implements ActivitySample {
private final int timestamp;
private final SampleProvider provider;
private final int intensity;
private final int steps;
private final int type;
private final int customValue;
public GBActivitySample2(SampleProvider provider, int timestamp, int intensity, int steps, int type) {
this(provider, timestamp, intensity, steps, type, 0);
}
public GBActivitySample2(SampleProvider provider, int timestamp, int intensity, int steps, int type, int customValue) {
this.timestamp = timestamp;
this.provider = provider;
this.intensity = intensity;
this.steps = steps;
this.customValue = customValue;
this.type = type;
validate();
}
private void validate() {
if (steps < 0) {
throw new IllegalArgumentException("steps must be >= 0");
}
if (intensity < 0) {
throw new IllegalArgumentException("intensity must be >= 0");
}
if (timestamp < 0) {
throw new IllegalArgumentException("timestamp must be >= 0");
}
if (customValue < 0) {
throw new IllegalArgumentException("customValue must be >= 0");
}
}
@Override
public int getTimestamp() {
return timestamp;
}
@Override
public SampleProvider getProvider() {
return provider;
}
@Override
public int getRawIntensity() {
return intensity;
}
@Override
public float getIntensity() {
return getProvider().normalizeIntensity(getRawIntensity());
}
@Override
public int getSteps() {
return steps;
}
@Override
public int getRawKind() {
return type;
}
@Override
public int getKind() {
return getProvider().normalizeType(getRawKind());
}
@Override
public String toString() {
return "GBActivitySample{" +
"timestamp=" + DateTimeUtils.formatDateTime(DateTimeUtils.parseTimeStamp(timestamp)) +
", intensity=" + getIntensity() +
", steps=" + getSteps() +
", type=" + getKind() +
'}';
}
}