From 876bdac918158a9ade572776f98c727d80b083b9 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Mon, 16 May 2016 23:36:54 +0200 Subject: [PATCH] More WIP: remove now unused code, better use of generics --- .../gadgetbridge/LockHandler.java | 39 --------------- .../charts/AbstractChartFragment.java | 33 ++++++------ .../activities/charts/ActivityAnalysis.java | 4 +- .../charts/ActivitySleepChartFragment.java | 4 +- .../activities/charts/SleepChartFragment.java | 6 +-- .../charts/WeekStepsChartFragment.java | 4 +- .../database/ActivityDatabaseHandler.java | 7 +-- .../gadgetbridge/database/DBHandler.java | 21 -------- .../devices/AbstractSampleProvider.java | 50 ++++++------------- .../devices/DeviceCoordinator.java | 3 +- .../gadgetbridge/devices/SampleProvider.java | 16 +++--- .../devices/UnknownDeviceCoordinator.java | 10 ++++ .../operations/FetchActivityOperation.java | 14 +----- .../pebble/AppMessageHandlerGBPebble.java | 3 +- .../pebble/AppMessageHandlerMisfit.java | 2 +- .../pebble/DatalogSessionHealthSleep.java | 10 ++-- .../pebble/DatalogSessionHealthSteps.java | 2 +- 17 files changed, 76 insertions(+), 152 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java index 3116b69a6..982a35a67 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/LockHandler.java @@ -39,52 +39,13 @@ public class LockHandler implements DBHandler { return null; } - @Override - public List getAllActivitySamples(int tsFrom, int tsTo, SampleProvider provider) { - return provider.getAllActivitySamples(tsFrom, tsTo); - } - - @Override - public List getActivitySamples(int tsFrom, int tsTo, SampleProvider provider) { - return provider.getActivitySamples(tsFrom, tsTo); - } - - @Override - public List getSleepSamples(int tsFrom, int tsTo, SampleProvider provider) { - return provider.getSleepSamples(tsFrom, tsTo); - } - - @Override - public int fetchLatestTimestamp(SampleProvider provider) { - return provider.fetchLatestTimestamp(); - } - @Override public DaoSession getDaoSession() { return session; } - @Override - public void addGBActivitySample(AbstractActivitySample sample) { - } - - @Override - public void addGBActivitySamples(AbstractActivitySample[] activitySamples) { - - } - @Override public SQLiteDatabase getWritableDatabase() { 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) { - - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java index 394dab8f8..079bb9f23 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractChartFragment.java @@ -45,6 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBAccess; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; +import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; @@ -293,7 +294,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { return akActivity.color; } - protected SampleProvider getProvider(DBHandler db, GBDevice device) { + protected SampleProvider getProvider(DBHandler db, GBDevice device) { DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device); return coordinator.getSampleProvider(db); } @@ -306,27 +307,27 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { * @param tsFrom * @param tsTo */ - protected List getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { - SampleProvider provider = getProvider(db, device); - return db.getAllActivitySamples(tsFrom, tsTo, provider); + protected List getAllSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + SampleProvider provider = getProvider(db, device); + return provider.getAllActivitySamples(tsFrom, tsTo); } private int getTSLast24Hours(int tsTo) { return (tsTo) - (24 * 60 * 60); // -24 hours } - protected List getActivitySamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { - SampleProvider provider = getProvider(db, device); - return db.getActivitySamples(tsFrom, tsTo, provider); + protected List getActivitySamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + SampleProvider provider = getProvider(db, device); + return provider.getActivitySamples(tsFrom, tsTo); } - protected List getSleepSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { - SampleProvider provider = getProvider(db, device); - return db.getSleepSamples(tsFrom, tsTo, provider); + protected List getSleepSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + SampleProvider provider = getProvider(db, device); + return provider.getSleepSamples(tsFrom, tsTo); } - protected List getTestSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + protected List getTestSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(2015, Calendar.JUNE, 10, 6, 40); @@ -334,8 +335,8 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { tsTo = (int) ((cal.getTimeInMillis() / 1000)); tsFrom = tsTo - (24 * 60 * 60); - SampleProvider provider = getProvider(db, device); - return db.getAllActivitySamples(tsFrom, tsTo, provider); + SampleProvider provider = getProvider(db, device); + return provider.getAllActivitySamples(tsFrom, tsTo); } protected void configureChartDefaults(Chart chart) { @@ -398,7 +399,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { */ protected abstract void renderCharts(); - protected DefaultChartsData refresh(GBDevice gbDevice, List samples) { + protected DefaultChartsData refresh(GBDevice gbDevice, List samples) { Calendar cal = GregorianCalendar.getInstance(); cal.clear(); Date date; @@ -541,7 +542,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { * @param tsTo * @return */ - protected abstract List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo); + protected abstract List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo); protected abstract void setupLegend(Chart chart); @@ -689,7 +690,7 @@ public abstract class AbstractChartFragment extends AbstractGBFragment { } } - protected List getSamples(DBHandler db, GBDevice device) { + protected List getSamples(DBHandler db, GBDevice device) { return getSamples(db, device, getTSStart(), getTSEnd()); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityAnalysis.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityAnalysis.java index f656b0587..d73d963d8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityAnalysis.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityAnalysis.java @@ -8,7 +8,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; public class ActivityAnalysis { - public ActivityAmounts calculateActivityAmounts(List samples) { + public ActivityAmounts calculateActivityAmounts(List samples) { ActivityAmount deepSleep = new ActivityAmount(ActivityKind.TYPE_DEEP_SLEEP); ActivityAmount lightSleep = new ActivityAmount(ActivityKind.TYPE_LIGHT_SLEEP); ActivityAmount notWorn = new ActivityAmount(ActivityKind.TYPE_NOT_WORN); @@ -66,7 +66,7 @@ public class ActivityAnalysis { return result; } - public int calculateTotalSteps(List samples) { + public int calculateTotalSteps(List samples) { int totalSteps = 0; for (ActivitySample sample : samples) { totalSteps += sample.getSteps(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java index 7260a205e..3825c153a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivitySleepChartFragment.java @@ -108,7 +108,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment { @Override protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { - List samples = getSamples(db, device); + List samples = getSamples(db, device); return refresh(device, samples); } @@ -142,7 +142,7 @@ public class ActivitySleepChartFragment extends AbstractChartFragment { } @Override - protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { return getAllSamples(db, device, tsFrom, tsTo); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java index 48ff18f84..bf0731210 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/SleepChartFragment.java @@ -50,7 +50,7 @@ public class SleepChartFragment extends AbstractChartFragment { @Override protected ChartsData refreshInBackground(ChartsHost chartsHost, DBHandler db, GBDevice device) { - List samples = getSamples(db, device); + List samples = getSamples(db, device); MySleepChartsData mySleepChartsData = refreshSleepAmounts(device, samples); DefaultChartsData chartsData = refresh(device, samples); @@ -58,7 +58,7 @@ public class SleepChartFragment extends AbstractChartFragment { return new MyChartsData(mySleepChartsData, chartsData); } - private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List samples) { + private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List samples) { ActivityAnalysis analysis = new ActivityAnalysis(); ActivityAmounts amounts = analysis.calculateActivityAmounts(samples); PieData data = new PieData(); @@ -195,7 +195,7 @@ public class SleepChartFragment extends AbstractChartFragment { } @Override - protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { // temporary fix for totally wrong sleep amounts // return super.getSleepSamples(db, device, tsFrom, tsTo); return super.getAllSamples(db, device, tsFrom, tsTo); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java index 2e271f286..5baede4eb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/WeekStepsChartFragment.java @@ -214,7 +214,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment { // chart.getLegend().setTextColor(LEGEND_TEXT_COLOR); } - private List getSamplesOfDay(DBHandler db, Calendar day, GBDevice device) { + private List getSamplesOfDay(DBHandler db, Calendar day, GBDevice device) { int startTs; int endTs; @@ -233,7 +233,7 @@ public class WeekStepsChartFragment extends AbstractChartFragment { } @Override - protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { + protected List getSamples(DBHandler db, GBDevice device, int tsFrom, int tsTo) { return super.getAllSamples(db, device, tsFrom, tsTo); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java index 197e94117..8f92dd5d6 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/ActivityDatabaseHandler.java @@ -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.TABLE_GBACTIVITYSAMPLES; +// TODO: can be removed entirely +// TODO: port schema migration to greendao first public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandler { 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 customShortValue */ - @Override public void addGBActivitySample(AbstractActivitySample sample) { float intensity = sample.getIntensity(); int steps = sample.getSteps(); @@ -122,7 +123,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl } } - @Override public void addGBActivitySamples(AbstractActivitySample[] activitySamples) { try (SQLiteDatabase db = this.getWritableDatabase()) { @@ -231,7 +231,6 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl return builder.toString(); } - @Override public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind, SampleProvider provider) { try (SQLiteDatabase db = this.getReadableDatabase()) { 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) { try (SQLiteDatabase db = this.getReadableDatabase()) { 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) { 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")) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHandler.java index 114ea0de5..d9f1ab858 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/database/DBHandler.java @@ -3,13 +3,8 @@ package nodomain.freeyourgadget.gadgetbridge.database; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import java.util.List; - 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.model.ActivitySample; public interface DBHandler extends AutoCloseable { /** @@ -25,23 +20,7 @@ public interface DBHandler extends AutoCloseable { */ void close() throws Exception; - List getAllActivitySamples(int tsFrom, int tsTo, SampleProvider provider); - - List getActivitySamples(int tsFrom, int tsTo, SampleProvider provider); - - List getSleepSamples(int tsFrom, int tsTo, SampleProvider provider); - - void addGBActivitySample(AbstractActivitySample sample); - - void addGBActivitySamples(AbstractActivitySample[] activitySamples); - 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(); } 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 451e20aec..4df87cc42 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java @@ -1,22 +1,16 @@ package nodomain.freeyourgadget.gadgetbridge.devices; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; - -import java.util.Arrays; import java.util.List; import de.greenrobot.dao.AbstractDao; import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.WhereCondition; -import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.MiBandActivitySampleDao; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; -import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; -public abstract class AbstractSampleProvider implements SampleProvider { +public abstract class AbstractSampleProvider implements SampleProvider { private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0]; private final DaoSession mSession; @@ -56,13 +50,13 @@ public abstract class AbstractSampleProvider implement } @Override - public void addGBActivitySample(AbstractActivitySample activitySample) { - getSampleDao().insert((T) activitySample); + public void addGBActivitySample(T activitySample) { + getSampleDao().insert(activitySample); } @Override - public void addGBActivitySamples(AbstractActivitySample[] activitySamples) { - getSampleDao().insertInTx((T[]) activitySamples); + public void addGBActivitySamples(T[] activitySamples) { + getSampleDao().insertInTx(activitySamples); } // @Override @@ -102,30 +96,16 @@ public abstract class AbstractSampleProvider implement // 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) { -// -// } -// -// @Override -// public int fetchLatestTimestamp(SampleProvider provider) { -// return 0; -// } -// + + public void changeStoredSamplesType(int timestampFrom, int timestampTo, int kind) { + // TODO: implement + } + + public void changeStoredSamplesType(int timestampFrom, int timestampTo, int fromKind, int toKind) { + // TODO: implement + } + //// 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 getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) { QueryBuilder qb = getSampleDao().queryBuilder(); @@ -133,7 +113,7 @@ public abstract class AbstractSampleProvider implement .where(MiBandActivitySampleDao.Properties.Timestamp.le(timestamp_to), getClauseForActivityType(qb, activityType)); List samples = qb.build().list(); for (T sample : samples) { - ((AbstractActivitySample) sample).setProvider(this); + sample.setProvider(this); } return samples; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java index 09f39d7b8..5e544a585 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/DeviceCoordinator.java @@ -5,6 +5,7 @@ import android.content.Context; import android.net.Uri; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; +import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; @@ -83,7 +84,7 @@ public interface DeviceCoordinator { * * @return */ - SampleProvider getSampleProvider(DBHandler db); + SampleProvider getSampleProvider(DBHandler db); /** * Finds an install handler for the given uri that can install the given diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java index 16f7e108a..888951599 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/SampleProvider.java @@ -3,9 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; -import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; -public interface SampleProvider { +public interface SampleProvider { + // TODO: these constants can all be removed int PROVIDER_MIBAND = 0; int PROVIDER_PEBBLE_MORPHEUZ = 1; int PROVIDER_PEBBLE_GADGETBRIDGE = 2; @@ -13,6 +13,8 @@ public interface SampleProvider { int PROVIDER_PEBBLE_HEALTH = 4; int PROVIDER_UNKNOWN = 100; + // TODO: can also be removed + int getID(); int normalizeType(int rawType); @@ -26,11 +28,13 @@ public interface SampleProvider { List 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(); - void addGBActivitySample(AbstractActivitySample activitySample); + void addGBActivitySample(T activitySample); - void addGBActivitySamples(AbstractActivitySample[] activitySamples); - - int getID(); + void addGBActivitySamples(T[] activitySamples); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java index dc58b9067..c75c983d7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/UnknownDeviceCoordinator.java @@ -15,6 +15,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { + private final UnknownSampleProvider sampleProvider; private static final class UnknownSampleProvider implements SampleProvider { @Override @@ -47,6 +48,14 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { 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 public int fetchLatestTimestamp() { return 0; @@ -67,6 +76,7 @@ public class UnknownDeviceCoordinator extends AbstractDeviceCoordinator { } public UnknownDeviceCoordinator() { + sampleProvider = new UnknownSampleProvider(); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java index 098d14a0f..1a550d61b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java @@ -2,7 +2,6 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; -import android.database.sqlite.SQLiteDatabase; import android.widget.Toast; import org.slf4j.Logger; @@ -19,20 +18,11 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; 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.MiBandDateConverter; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandSampleProvider; 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.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.actions.SetDeviceBusyAction; 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 deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId(); 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); if ((activityStruct.activityDataHolderProgress % bpm) != 0) { throw new IllegalStateException("Unexpected data, progress should be mutiple of " + bpm + ": " + activityStruct.activityDataHolderProgress); } int numSamples = activityStruct.activityDataHolderProgress / bpm; - AbstractActivitySample[] samples = new AbstractActivitySample[numSamples]; + MiBandActivitySample[] samples = new MiBandActivitySample[numSamples]; for (int i = 0; i < activityStruct.activityDataHolderProgress; i += bpm) { category = activityStruct.activityDataHolder[i]; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerGBPebble.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerGBPebble.java index a3b8605ca..9ddb48e9f 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerGBPebble.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerGBPebble.java @@ -22,6 +22,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; import nodomain.freeyourgadget.gadgetbridge.devices.pebble.PebbleGadgetBridgeSampleProvider; import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.PebbleActivitySample; import nodomain.freeyourgadget.gadgetbridge.entities.User; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample; @@ -58,7 +59,7 @@ public class AppMessageHandlerGBPebble extends AppMessageHandler { User user = DBHelper.getUser(db.getDaoSession()); Device device = DBHelper.getDevice(getDevice(), db.getDaoSession()); PebbleGadgetBridgeSampleProvider sampleProvider = new PebbleGadgetBridgeSampleProvider(db.getDaoSession()); - AbstractActivitySample[] activitySamples = new AbstractActivitySample[samples_remaining]; + PebbleActivitySample[] activitySamples = new PebbleActivitySample[samples_remaining]; int i = 0; while (samples_remaining-- > 0) { short sample = samplesBuffer.getShort(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java index 3991e2c75..43bd4f450 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/AppMessageHandlerMisfit.java @@ -71,7 +71,7 @@ public class AppMessageHandlerMisfit extends AppMessageHandler { LOG.info("got data from " + startDate + " to " + endDate); int totalSteps = 0; - AbstractActivitySample[] activitySamples = new AbstractActivitySample[samples]; + PebbleActivitySample[] activitySamples = new PebbleActivitySample[samples]; try (DBHandler db = GBApplication.acquireDB()) { Long userId = DBHelper.getUser(db.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(getDevice(), db.getDaoSession()).getId(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java index 8eb702ffa..9d15d31f4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSleep.java @@ -68,14 +68,14 @@ class DatalogSessionHealthSleep extends DatalogSession { private boolean store84(SleepRecord84[] sleepRecords) { try (DBHandler dbHandler = GBApplication.acquireDB()) { SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); - int latestTimestamp = dbHandler.fetchLatestTimestamp(sampleProvider); + int latestTimestamp = sampleProvider.fetchLatestTimestamp(); for (SleepRecord84 sleepRecord : sleepRecords) { if (latestTimestamp < (sleepRecord.timestampStart + sleepRecord.durationSeconds)) return false; 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 { - 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()) { SampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); 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) { if (latestTimestamp < sleepRecord.bedTimeEnd) 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) { LOG.debug(ex.getMessage()); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSteps.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSteps.java index 3f9ae04e2..a314a674b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSteps.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/DatalogSessionHealthSteps.java @@ -78,7 +78,7 @@ public class DatalogSessionHealthSteps extends DatalogSession { try (DBHandler dbHandler = GBApplication.acquireDB()) { HealthSampleProvider sampleProvider = new HealthSampleProvider(dbHandler.getDaoSession()); - AbstractActivitySample[] samples = new AbstractActivitySample[stepsRecords.length]; + PebbleActivitySample[] samples = new PebbleActivitySample[stepsRecords.length]; // TODO: user and device Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId(); Long deviceId = DBHelper.getDevice(device, dbHandler.getDaoSession()).getId();