1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 02:25:50 +01:00

More WIP: remove now unused code, better use of generics

This commit is contained in:
cpfeiffer 2016-05-16 23:36:54 +02:00
parent 40a376bbd0
commit 876bdac918
17 changed files with 76 additions and 152 deletions

View File

@ -39,52 +39,13 @@ public class LockHandler implements DBHandler {
return null; return null;
} }
@Override
public List<ActivitySample> getAllActivitySamples(int tsFrom, int tsTo, SampleProvider provider) {
return provider.getAllActivitySamples(tsFrom, tsTo);
}
@Override
public List<ActivitySample> getActivitySamples(int tsFrom, int tsTo, SampleProvider provider) {
return provider.getActivitySamples(tsFrom, tsTo);
}
@Override
public List<ActivitySample> getSleepSamples(int tsFrom, int tsTo, SampleProvider provider) {
return provider.getSleepSamples(tsFrom, tsTo);
}
@Override
public int fetchLatestTimestamp(SampleProvider provider) {
return provider.fetchLatestTimestamp();
}
@Override @Override
public DaoSession getDaoSession() { public DaoSession getDaoSession() {
return session; return session;
} }
@Override
public void addGBActivitySample(AbstractActivitySample sample) {
}
@Override
public void addGBActivitySamples(AbstractActivitySample[] activitySamples) {
}
@Override @Override
public SQLiteDatabase getWritableDatabase() { public SQLiteDatabase getWritableDatabase() {
return null; return null;
} }
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider) {
}
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind, SampleProvider provider) {
}
} }

View File

@ -45,6 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBAccess;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
@ -293,7 +294,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
return akActivity.color; return akActivity.color;
} }
protected SampleProvider getProvider(DBHandler db, GBDevice device) { protected SampleProvider<AbstractActivitySample> getProvider(DBHandler db, GBDevice device) {
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
return coordinator.getSampleProvider(db); return coordinator.getSampleProvider(db);
} }
@ -306,27 +307,27 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
* @param tsFrom * @param tsFrom
* @param tsTo * @param tsTo
*/ */
protected List<ActivitySample> getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
SampleProvider provider = getProvider(db, device); SampleProvider<? extends ActivitySample> provider = getProvider(db, device);
return db.getAllActivitySamples(tsFrom, tsTo, provider); return provider.getAllActivitySamples(tsFrom, tsTo);
} }
private int getTSLast24Hours(int tsTo) { private int getTSLast24Hours(int tsTo) {
return (tsTo) - (24 * 60 * 60); // -24 hours return (tsTo) - (24 * 60 * 60); // -24 hours
} }
protected List<ActivitySample> getActivitySamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getActivitySamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
SampleProvider provider = getProvider(db, device); SampleProvider<AbstractActivitySample> provider = getProvider(db, device);
return db.getActivitySamples(tsFrom, tsTo, provider); return provider.getActivitySamples(tsFrom, tsTo);
} }
protected List<ActivitySample> getSleepSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getSleepSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
SampleProvider provider = getProvider(db, device); SampleProvider<? extends ActivitySample> provider = getProvider(db, device);
return db.getSleepSamples(tsFrom, tsTo, provider); return provider.getSleepSamples(tsFrom, tsTo);
} }
protected List<ActivitySample> getTestSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getTestSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
cal.clear(); cal.clear();
cal.set(2015, Calendar.JUNE, 10, 6, 40); cal.set(2015, Calendar.JUNE, 10, 6, 40);
@ -334,8 +335,8 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
tsTo = (int) ((cal.getTimeInMillis() / 1000)); tsTo = (int) ((cal.getTimeInMillis() / 1000));
tsFrom = tsTo - (24 * 60 * 60); tsFrom = tsTo - (24 * 60 * 60);
SampleProvider provider = getProvider(db, device); SampleProvider<? extends ActivitySample> provider = getProvider(db, device);
return db.getAllActivitySamples(tsFrom, tsTo, provider); return provider.getAllActivitySamples(tsFrom, tsTo);
} }
protected void configureChartDefaults(Chart<?> chart) { protected void configureChartDefaults(Chart<?> chart) {
@ -398,7 +399,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
*/ */
protected abstract void renderCharts(); protected abstract void renderCharts();
protected DefaultChartsData refresh(GBDevice gbDevice, List<ActivitySample> samples) { protected DefaultChartsData refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
Calendar cal = GregorianCalendar.getInstance(); Calendar cal = GregorianCalendar.getInstance();
cal.clear(); cal.clear();
Date date; Date date;
@ -541,7 +542,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
* @param tsTo * @param tsTo
* @return * @return
*/ */
protected abstract List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo); protected abstract List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo);
protected abstract void setupLegend(Chart chart); protected abstract void setupLegend(Chart chart);
@ -689,7 +690,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment {
} }
} }
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device) { protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device) {
return getSamples(db, device, getTSStart(), getTSEnd()); return getSamples(db, device, getTSStart(), getTSEnd());
} }

View File

@ -8,7 +8,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public class ActivityAnalysis { public class ActivityAnalysis {
public ActivityAmounts calculateActivityAmounts(List<ActivitySample> samples) { public ActivityAmounts calculateActivityAmounts(List<? extends ActivitySample> samples) {
ActivityAmount deepSleep = new ActivityAmount(ActivityKind.TYPE_DEEP_SLEEP); ActivityAmount deepSleep = new ActivityAmount(ActivityKind.TYPE_DEEP_SLEEP);
ActivityAmount lightSleep = new ActivityAmount(ActivityKind.TYPE_LIGHT_SLEEP); ActivityAmount lightSleep = new ActivityAmount(ActivityKind.TYPE_LIGHT_SLEEP);
ActivityAmount notWorn = new ActivityAmount(ActivityKind.TYPE_NOT_WORN); ActivityAmount notWorn = new ActivityAmount(ActivityKind.TYPE_NOT_WORN);
@ -66,7 +66,7 @@ public class ActivityAnalysis {
return result; return result;
} }
public int calculateTotalSteps(List<ActivitySample> samples) { public int calculateTotalSteps(List<? extends ActivitySample> samples) {
int totalSteps = 0; int totalSteps = 0;
for (ActivitySample sample : samples) { for (ActivitySample sample : samples) {
totalSteps += sample.getSteps(); totalSteps += sample.getSteps();

View File

@ -108,7 +108,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
@Override @Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
List<ActivitySample> samples = getSamples(db, device); List<? extends ActivitySample> samples = getSamples(db, device);
return refresh(device, samples); return refresh(device, samples);
} }
@ -142,7 +142,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment {
} }
@Override @Override
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
return getAllSamples(db, device, tsFrom, tsTo); return getAllSamples(db, device, tsFrom, tsTo);
} }
} }

View File

@ -50,7 +50,7 @@ public class SleepChartFragment extends AbstractChartFragment {
@Override @Override
protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) {
List<ActivitySample> samples = getSamples(db, device); List<? extends ActivitySample> samples = getSamples(db, device);
MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples); MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples);
DefaultChartsData chartsData = refresh(device, samples); DefaultChartsData chartsData = refresh(device, samples);
@ -58,7 +58,7 @@ public class SleepChartFragment extends AbstractChartFragment {
return new MyChartsData(mySleepChartsData, chartsData); return new MyChartsData(mySleepChartsData, chartsData);
} }
private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List<ActivitySample> samples) { private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List<? extends ActivitySample> samples) {
ActivityAnalysis analysis = new ActivityAnalysis(); ActivityAnalysis analysis = new ActivityAnalysis();
ActivityAmounts amounts = analysis.calculateActivityAmounts(samples); ActivityAmounts amounts = analysis.calculateActivityAmounts(samples);
PieData data = new PieData(); PieData data = new PieData();
@ -195,7 +195,7 @@ public class SleepChartFragment extends AbstractChartFragment {
} }
@Override @Override
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
// temporary fix for totally wrong sleep amounts // temporary fix for totally wrong sleep amounts
// return super.getSleepSamples(db, device, tsFrom, tsTo); // return super.getSleepSamples(db, device, tsFrom, tsTo);
return super.getAllSamples(db, device, tsFrom, tsTo); return super.getAllSamples(db, device, tsFrom, tsTo);

View File

@ -214,7 +214,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
// chart.getLegend().setTextColor(LEGEND_TEXT_COLOR); // chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
} }
private List<ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, GBDevice device) { private List<? extends ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, GBDevice device) {
int startTs; int startTs;
int endTs; int endTs;
@ -233,7 +233,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment {
} }
@Override @Override
protected List<ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { protected List<? extends ActivitySample> getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) {
return super.getAllSamples(db, device, tsFrom, tsTo); return super.getAllSamples(db, device, tsFrom, tsTo);
} }

View File

@ -33,6 +33,8 @@ import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TIME
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TYPE; import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GBACTIVITYSAMPLES; import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GBACTIVITYSAMPLES;
// TODO: can be removed entirely
// TODO: port schema migration to greendao first
public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandler { public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandler {
private static final Logger LOG = LoggerFactory.getLogger(ActivityDatabaseHandler.class); private static final Logger LOG = LoggerFactory.getLogger(ActivityDatabaseHandler.class);
@ -87,7 +89,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
* @param kind the raw activity kind of the sample * @param kind the raw activity kind of the sample
* @param customShortValue * @param customShortValue
*/ */
@Override
public void addGBActivitySample(AbstractActivitySample sample) { public void addGBActivitySample(AbstractActivitySample sample) {
float intensity = sample.getIntensity(); float intensity = sample.getIntensity();
int steps = sample.getSteps(); int steps = sample.getSteps();
@ -122,7 +123,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
} }
} }
@Override
public void addGBActivitySamples(AbstractActivitySample[] activitySamples) { public void addGBActivitySamples(AbstractActivitySample[] activitySamples) {
try (SQLiteDatabase db = this.getWritableDatabase()) { try (SQLiteDatabase db = this.getWritableDatabase()) {
@ -231,7 +231,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
return builder.toString(); return builder.toString();
} }
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider) { public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider) {
try (SQLiteDatabase db = this.getReadableDatabase()) { try (SQLiteDatabase db = this.getReadableDatabase()) {
String sql = "UPDATE " + TABLE_GBACTIVITYSAMPLES + " SET " + KEY_TYPE + "= ? WHERE " String sql = "UPDATE " + TABLE_GBACTIVITYSAMPLES + " SET " + KEY_TYPE + "= ? WHERE "
@ -247,7 +246,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
} }
} }
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind, SampleProvider provider) { public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind, SampleProvider provider) {
try (SQLiteDatabase db = this.getReadableDatabase()) { try (SQLiteDatabase db = this.getReadableDatabase()) {
String sql = "UPDATE " + TABLE_GBACTIVITYSAMPLES + " SET " + KEY_TYPE + "= ? WHERE " String sql = "UPDATE " + TABLE_GBACTIVITYSAMPLES + " SET " + KEY_TYPE + "= ? WHERE "
@ -265,7 +263,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
} }
} }
@Override
public int fetchLatestTimestamp(SampleProvider provider) { public int fetchLatestTimestamp(SampleProvider provider) {
try (SQLiteDatabase db = this.getReadableDatabase()) { 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")) { try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, new String[]{KEY_TIMESTAMP}, KEY_PROVIDER + "=" + String.valueOf(provider.getID()), null, null, null, KEY_TIMESTAMP + " DESC", "1")) {

View File

@ -3,13 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.database;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public interface DBHandler extends AutoCloseable { public interface DBHandler extends AutoCloseable {
/** /**
@ -25,23 +20,7 @@ public interface DBHandler extends AutoCloseable {
*/ */
void close() throws Exception; void close() throws Exception;
List<ActivitySample> getAllActivitySamples(int tsFrom, int tsTo, SampleProvider provider);
List<ActivitySample> getActivitySamples(int tsFrom, int tsTo, SampleProvider provider);
List<ActivitySample> getSleepSamples(int tsFrom, int tsTo, SampleProvider provider);
void addGBActivitySample(AbstractActivitySample sample);
void addGBActivitySamples(AbstractActivitySample[] activitySamples);
SQLiteDatabase getWritableDatabase(); SQLiteDatabase getWritableDatabase();
void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider);
void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind, SampleProvider provider);
int fetchLatestTimestamp(SampleProvider provider);
DaoSession getDaoSession(); DaoSession getDaoSession();
} }

View File

@ -1,22 +1,16 @@
package nodomain.freeyourgadget.gadgetbridge.devices; package nodomain.freeyourgadget.gadgetbridge.devices;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Arrays;
import java.util.List; import java.util.List;
import de.greenrobot.dao.AbstractDao; import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.dao.query.WhereCondition; import de.greenrobot.dao.query.WhereCondition;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public abstract class AbstractSampleProvider<T extends ActivitySample> implements SampleProvider { public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0]; private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
private final DaoSession mSession; private final DaoSession mSession;
@ -56,13 +50,13 @@ public abstract class AbstractSampleProvider<T extends ActivitySample> implement
} }
@Override @Override
public void addGBActivitySample(AbstractActivitySample activitySample) { public void addGBActivitySample(T activitySample) {
getSampleDao().insert((T) activitySample); getSampleDao().insert(activitySample);
} }
@Override @Override
public void addGBActivitySamples(AbstractActivitySample[] activitySamples) { public void addGBActivitySamples(T[] activitySamples) {
getSampleDao().insertInTx((T[]) activitySamples); getSampleDao().insertInTx(activitySamples);
} }
// @Override // @Override
@ -102,30 +96,16 @@ public abstract class AbstractSampleProvider<T extends ActivitySample> implement
// return null; // return null;
// } // }
// //
// @Override
// public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider) { public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
// // TODO: implement
// } }
//
// @Override public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) {
// public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind, SampleProvider provider) { // TODO: implement
// }
// }
//
// @Override
// public int fetchLatestTimestamp(SampleProvider provider) {
// return 0;
// }
//
//// SQLiteDatabase getWritableDatabase(); //// SQLiteDatabase getWritableDatabase();
//
// public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
// // TODO: implement
// }
//
// public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) {
// // TODO: implement
// }
protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) { protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
QueryBuilder<T> qb = getSampleDao().queryBuilder(); QueryBuilder<T> qb = getSampleDao().queryBuilder();
@ -133,7 +113,7 @@ public abstract class AbstractSampleProvider<T extends ActivitySample> implement
.where(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to), getClauseForActivityType(qb, activityType)); .where(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to), getClauseForActivityType(qb, activityType));
List<T> samples = qb.build().list(); List<T> samples = qb.build().list();
for (T sample : samples) { for (T sample : samples) {
((AbstractActivitySample) sample).setProvider(this); sample.setProvider(this);
} }
return samples; return samples;
} }

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.net.Uri; import android.net.Uri;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
@ -83,7 +84,7 @@ public interface DeviceCoordinator {
* *
* @return * @return
*/ */
SampleProvider getSampleProvider(DBHandler db); SampleProvider<AbstractActivitySample> getSampleProvider(DBHandler db);
/** /**
* Finds an install handler for the given uri that can install the given * Finds an install handler for the given uri that can install the given

View File

@ -3,9 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public interface SampleProvider<T extends ActivitySample> { public interface SampleProvider<T extends AbstractActivitySample> {
// TODO: these constants can all be removed
int PROVIDER_MIBAND = 0; int PROVIDER_MIBAND = 0;
int PROVIDER_PEBBLE_MORPHEUZ = 1; int PROVIDER_PEBBLE_MORPHEUZ = 1;
int PROVIDER_PEBBLE_GADGETBRIDGE = 2; int PROVIDER_PEBBLE_GADGETBRIDGE = 2;
@ -13,6 +13,8 @@ public interface SampleProvider<T extends ActivitySample> {
int PROVIDER_PEBBLE_HEALTH = 4; int PROVIDER_PEBBLE_HEALTH = 4;
int PROVIDER_UNKNOWN = 100; int PROVIDER_UNKNOWN = 100;
// TODO: can also be removed
int getID();
int normalizeType(int rawType); int normalizeType(int rawType);
@ -26,11 +28,13 @@ public interface SampleProvider<T extends ActivitySample> {
List<T> getSleepSamples(int timestamp_from, int timestamp_to); List<T> getSleepSamples(int timestamp_from, int timestamp_to);
void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind);
void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind);
int fetchLatestTimestamp(); int fetchLatestTimestamp();
void addGBActivitySample(AbstractActivitySample activitySample); void addGBActivitySample(T activitySample);
void addGBActivitySamples(AbstractActivitySample[] activitySamples); void addGBActivitySamples(T[] activitySamples);
int getID();
} }

View File

@ -15,6 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
private final UnknownSampleProvider sampleProvider;
private static final class UnknownSampleProvider implements SampleProvider { private static final class UnknownSampleProvider implements SampleProvider {
@Override @Override
@ -47,6 +48,14 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
return null; return null;
} }
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) {
}
@Override
public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) {
}
@Override @Override
public int fetchLatestTimestamp() { public int fetchLatestTimestamp() {
return 0; return 0;
@ -67,6 +76,7 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator {
} }
public UnknownDeviceCoordinator() { public UnknownDeviceCoordinator() {
sampleProvider = new UnknownSampleProvider();
} }
@Override @Override

View File

@ -2,7 +2,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast; import android.widget.Toast;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -19,20 +18,11 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.devices.AbstractSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandDateConverter;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao;
import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder; import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction;
import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport; import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.MiBandSupport;
@ -319,13 +309,13 @@ public class FetchActivityOperation extends AbstractMiBandOperation {
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId(); Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
int minutes = 0; int minutes = 0;
try (SQLiteDatabase db = dbHandler.getWritableDatabase()) { // explicitly keep the db open while looping over the samples try {
int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000); int timestampInSeconds = (int) (activityStruct.activityDataTimestampProgress.getTimeInMillis() / 1000);
if ((activityStruct.activityDataHolderProgress % bpm) != 0) { if ((activityStruct.activityDataHolderProgress % bpm) != 0) {
throw new IllegalStateException("Unexpected data, progress should be mutiple of " + bpm + ": " + activityStruct.activityDataHolderProgress); throw new IllegalStateException("Unexpected data, progress should be mutiple of " + bpm + ": " + activityStruct.activityDataHolderProgress);
} }
int numSamples = activityStruct.activityDataHolderProgress / bpm; int numSamples = activityStruct.activityDataHolderProgress / bpm;
AbstractActivitySample[] samples = new AbstractActivitySample[numSamples]; MiBandActivitySample[] samples = new MiBandActivitySample[numSamples];
for (int i = 0; i < activityStruct.activityDataHolderProgress; i += bpm) { for (int i = 0; i < activityStruct.activityDataHolderProgress; i += bpm) {
category = activityStruct.activityDataHolder[i]; category = activityStruct.activityDataHolder[i];

View File

@ -22,6 +22,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleGadgetBridgeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleGadgetBridgeSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.Device; import nodomain.freeyourgadget.gadgetbridge.entities.Device;
import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample;
import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.entities.User;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
@ -58,7 +59,7 @@ public class AppMessageHandlerGBPebble extends AppMessageHandler {
User user = DBHelper.getUser(db.getDaoSession()); User user = DBHelper.getUser(db.getDaoSession());
Device device = DBHelper.getDevice(getDevice(), db.getDaoSession()); Device device = DBHelper.getDevice(getDevice(), db.getDaoSession());
PebbleGadgetBridgeSampleProvider sampleProvider = new PebbleGadgetBridgeSampleProvider(db.getDaoSession()); PebbleGadgetBridgeSampleProvider sampleProvider = new PebbleGadgetBridgeSampleProvider(db.getDaoSession());
AbstractActivitySample[] activitySamples = new AbstractActivitySample[samples_remaining]; PebbleActivitySample[] activitySamples = new PebbleActivitySample[samples_remaining];
int i = 0; int i = 0;
while (samples_remaining-- > 0) { while (samples_remaining-- > 0) {
short sample = samplesBuffer.getShort(); short sample = samplesBuffer.getShort();

View File

@ -71,7 +71,7 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
LOG.info("got data from " + startDate + " to " + endDate); LOG.info("got data from " + startDate + " to " + endDate);
int totalSteps = 0; int totalSteps = 0;
AbstractActivitySample[] activitySamples = new AbstractActivitySample[samples]; PebbleActivitySample[] activitySamples = new PebbleActivitySample[samples];
try (DBHandler db = GBApplication.acquireDB()) { try (DBHandler db = GBApplication.acquireDB()) {
Long userId = DBHelper.getUser(db.getDaoSession()).getId(); Long userId = DBHelper.getUser(db.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId();

View File

@ -68,14 +68,14 @@ class DatalogSessionHealthSleep extends DatalogSession {
private boolean store84(SleepRecord84[] sleepRecords) { private boolean store84(SleepRecord84[] sleepRecords) {
try (DBHandler dbHandler = GBApplication.acquireDB()) { try (DBHandler dbHandler = GBApplication.acquireDB()) {
SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
int latestTimestamp = dbHandler.fetchLatestTimestamp(sampleProvider); int latestTimestamp = sampleProvider.fetchLatestTimestamp();
for (SleepRecord84 sleepRecord : sleepRecords) { for (SleepRecord84 sleepRecord : sleepRecords) {
if (latestTimestamp < (sleepRecord.timestampStart + sleepRecord.durationSeconds)) if (latestTimestamp < (sleepRecord.timestampStart + sleepRecord.durationSeconds))
return false; return false;
if (sleepRecord.type == 2) { if (sleepRecord.type == 2) {
dbHandler.changeStoredSamplesType(sleepRecord.timestampStart, (sleepRecord.timestampStart + sleepRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP), sampleProvider); sampleProvider.changeStoredSamplesType(sleepRecord.timestampStart, (sleepRecord.timestampStart + sleepRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_DEEP_SLEEP));
} else { } else {
dbHandler.changeStoredSamplesType(sleepRecord.timestampStart, (sleepRecord.timestampStart + sleepRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP), sampleProvider); sampleProvider.changeStoredSamplesType(sleepRecord.timestampStart, (sleepRecord.timestampStart + sleepRecord.durationSeconds), sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP));
} }
} }
@ -116,11 +116,11 @@ class DatalogSessionHealthSleep extends DatalogSession {
try (DBHandler dbHandler = GBApplication.acquireDB()) { try (DBHandler dbHandler = GBApplication.acquireDB()) {
SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
GB.toast("Deep sleep is supported only from firmware 3.11 onwards.", Toast.LENGTH_LONG, GB.INFO); GB.toast("Deep sleep is supported only from firmware 3.11 onwards.", Toast.LENGTH_LONG, GB.INFO);
int latestTimestamp = dbHandler.fetchLatestTimestamp(sampleProvider); int latestTimestamp = sampleProvider.fetchLatestTimestamp();
for (SleepRecord83 sleepRecord : sleepRecords) { for (SleepRecord83 sleepRecord : sleepRecords) {
if (latestTimestamp < sleepRecord.bedTimeEnd) if (latestTimestamp < sleepRecord.bedTimeEnd)
return false; return false;
dbHandler.changeStoredSamplesType(sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd, sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP), sampleProvider); sampleProvider.changeStoredSamplesType(sleepRecord.bedTimeStart, sleepRecord.bedTimeEnd, sampleProvider.toRawActivityKind(ActivityKind.TYPE_ACTIVITY), sampleProvider.toRawActivityKind(ActivityKind.TYPE_LIGHT_SLEEP));
} }
} catch (Exception ex) { } catch (Exception ex) {
LOG.debug(ex.getMessage()); LOG.debug(ex.getMessage());

View File

@ -78,7 +78,7 @@ public class DatalogSessionHealthSteps extends DatalogSession {
try (DBHandler dbHandler = GBApplication.acquireDB()) { try (DBHandler dbHandler = GBApplication.acquireDB()) {
HealthSampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); HealthSampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession());
AbstractActivitySample[] samples = new AbstractActivitySample[stepsRecords.length]; PebbleActivitySample[] samples = new PebbleActivitySample[stepsRecords.length];
// TODO: user and device // TODO: user and device
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId(); Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
Long deviceId = DBHelper.getDevice(device, dbHandler.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(device, dbHandler.getDaoSession()).getId();