diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java index 498925f6e..7c16f3142 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiBRCoordinator.java @@ -210,7 +210,7 @@ public abstract class HuaweiBRCoordinator extends AbstractBLClassicDeviceCoordin @Override public ActivitySummaryParser getActivitySummaryParser(final GBDevice device, final Context context) { - return new HuaweiWorkoutGbParser(device, context); + return new HuaweiWorkoutGbParser(device); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java index 8160bec10..f14815beb 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiLECoordinator.java @@ -219,7 +219,7 @@ public abstract class HuaweiLECoordinator extends AbstractBLEDeviceCoordinator i @Override public ActivitySummaryParser getActivitySummaryParser(final GBDevice device, final Context context) { - return new HuaweiWorkoutGbParser(device, context); + return new HuaweiWorkoutGbParser(device); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSettingsCustomizer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSettingsCustomizer.java index ed1ecf507..85d289d5e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSettingsCustomizer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiSettingsCustomizer.java @@ -70,7 +70,7 @@ public class HuaweiSettingsCustomizer implements DeviceSpecificSettingsCustomize if (preference.getKey().equals("huawei_reparse_workout_data")) { if (((SwitchPreferenceCompat) preference).isChecked()) { GB.toast("Starting workout reparse", Toast.LENGTH_SHORT, 0); - new HuaweiWorkoutGbParser(handler.getDevice(), handler.getContext()).parseAllWorkouts(); + new HuaweiWorkoutGbParser(handler.getDevice()).parseAllWorkouts(); GB.toast("Workout reparse is complete", Toast.LENGTH_SHORT, 0); ((SwitchPreferenceCompat) preference).setChecked(false); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivitySummaryParser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivitySummaryParser.java index 4c9fb8001..b007dfb36 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivitySummaryParser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/ActivitySummaryParser.java @@ -45,17 +45,23 @@ public interface ActivitySummaryParser { final GBDevice gbDevice, final int timestampSeconds) { final Device device = DBHelper.getDevice(gbDevice, session); + return findOrCreateBaseActivitySummary(session, device.getId(), timestampSeconds); + } + + static BaseActivitySummary findOrCreateBaseActivitySummary(final DaoSession session, + final long deviceId, + final int timestampSeconds) { final User user = DBHelper.getUser(session); final BaseActivitySummaryDao summaryDao = session.getBaseActivitySummaryDao(); final QueryBuilder qb = summaryDao.queryBuilder(); qb.where(BaseActivitySummaryDao.Properties.StartTime.eq(new Date(timestampSeconds * 1000L))); - qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(device.getId())); + qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(deviceId)); qb.where(BaseActivitySummaryDao.Properties.UserId.eq(user.getId())); final List summaries = qb.build().list(); if (summaries.isEmpty()) { final BaseActivitySummary summary = new BaseActivitySummary(); summary.setStartTime(new Date(timestampSeconds * 1000L)); - summary.setDevice(device); + summary.setDeviceId(deviceId); summary.setUser(user); // These will be set later, once we parse the summary diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWorkoutGbParser.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWorkoutGbParser.java index 3499bfe01..fa5ab5a19 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWorkoutGbParser.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiWorkoutGbParser.java @@ -16,7 +16,6 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei; -import android.content.Context; import android.widget.Toast; import org.slf4j.Logger; @@ -64,11 +63,9 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser { // TODO: Might be nicer to propagate the exceptions, so they can be handled upstream private final GBDevice gbDevice; - private final Context context; - public HuaweiWorkoutGbParser(final GBDevice gbDevice, final Context context) { + public HuaweiWorkoutGbParser(final GBDevice gbDevice) { this.gbDevice = gbDevice; - this.context = context; } @Override @@ -93,7 +90,7 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser { LOG.warn("Failed to find huawei summary for {}", summary.getStartTime()); return summary; } - updateBaseSummary(db, huaweiSummaries.get(0), summary); + updateBaseSummary(session, huaweiSummaries.get(0), summary); } catch (Exception e) { LOG.error("Failed to update summary"); } @@ -235,9 +232,10 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser { parseUnknownWorkoutData(); try (DBHandler db = GBApplication.acquireDB()) { - QueryBuilder qb = db.getDaoSession().getHuaweiWorkoutSummarySampleDao().queryBuilder(); + final DaoSession session = db.getDaoSession(); + QueryBuilder qb = session.getHuaweiWorkoutSummarySampleDao().queryBuilder(); for (HuaweiWorkoutSummarySample summary : qb.listLazy()) { - parseWorkout(summary.getWorkoutId()); + parseWorkout(session, summary.getWorkoutId(), summary.getDeviceId()); } } catch (Exception e) { GB.toast("Exception parsing workouts", Toast.LENGTH_SHORT, GB.ERROR, e); @@ -306,43 +304,49 @@ public class HuaweiWorkoutGbParser implements ActivitySummaryParser { } public void parseWorkout(Long workoutId) { - if (workoutId == null) - return; - try (DBHandler db = GBApplication.acquireDB()) { - QueryBuilder qbSummary = db.getDaoSession().getHuaweiWorkoutSummarySampleDao().queryBuilder().where( - HuaweiWorkoutSummarySampleDao.Properties.WorkoutId.eq(workoutId) - ); - List summarySamples = qbSummary.build().list(); - if (summarySamples.size() != 1) - return; - HuaweiWorkoutSummarySample summary = summarySamples.get(0); - - final BaseActivitySummary baseSummary = ActivitySummaryParser.findOrCreateBaseActivitySummary( - db.getDaoSession(), - gbDevice, - summary.getStartTimestamp() - ); - - updateBaseSummary(db, summary, baseSummary); - - db.getDaoSession().getBaseActivitySummaryDao().insertOrReplace(baseSummary); + final DaoSession session = db.getDaoSession(); + final Device device = DBHelper.getDevice(gbDevice, session); + parseWorkout(session, workoutId, device.getId()); } catch (Exception e) { GB.toast("Exception parsing workout data", Toast.LENGTH_SHORT, GB.ERROR, e); LOG.error("Exception parsing workout data", e); } } - public void updateBaseSummary(final DBHandler db, + public void parseWorkout(final DaoSession session, final Long workoutId, final long deviceId) { + if (workoutId == null) + return; + + QueryBuilder qbSummary = session.getHuaweiWorkoutSummarySampleDao().queryBuilder().where( + HuaweiWorkoutSummarySampleDao.Properties.WorkoutId.eq(workoutId) + ); + List summarySamples = qbSummary.build().list(); + if (summarySamples.size() != 1) + return; + HuaweiWorkoutSummarySample summary = summarySamples.get(0); + + final BaseActivitySummary baseSummary = ActivitySummaryParser.findOrCreateBaseActivitySummary( + session, + deviceId, + summary.getStartTimestamp() + ); + + updateBaseSummary(session, summary, baseSummary); + + session.getBaseActivitySummaryDao().insertOrReplace(baseSummary); + } + + public void updateBaseSummary(final DaoSession session, final HuaweiWorkoutSummarySample summary, final BaseActivitySummary baseSummary) { try { - QueryBuilder qbData = db.getDaoSession().getHuaweiWorkoutDataSampleDao().queryBuilder().where( + QueryBuilder qbData = session.getHuaweiWorkoutDataSampleDao().queryBuilder().where( HuaweiWorkoutDataSampleDao.Properties.WorkoutId.eq(summary.getWorkoutId()) ); List dataSamples = qbData.build().list(); - QueryBuilder qbPace = db.getDaoSession().getHuaweiWorkoutPaceSampleDao().queryBuilder().where( + QueryBuilder qbPace = session.getHuaweiWorkoutPaceSampleDao().queryBuilder().where( HuaweiWorkoutPaceSampleDao.Properties.WorkoutId.eq(summary.getWorkoutId()) ); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutDataRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutDataRequest.java index bf941d03f..b339e42e0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutDataRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutDataRequest.java @@ -112,7 +112,7 @@ public class GetWorkoutDataRequest extends Request { nextRequest.setFinalizeReq(this.finalizeReq); this.nextRequest(nextRequest); } else { - new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(this.databaseId); + new HuaweiWorkoutGbParser(getDevice()).parseWorkout(this.databaseId); if (!remainder.isEmpty()) { GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest( diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutPaceRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutPaceRequest.java index c01a64c20..3145dcba0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutPaceRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutPaceRequest.java @@ -89,7 +89,7 @@ public class GetWorkoutPaceRequest extends Request { nextRequest.setFinalizeReq(this.finalizeReq); this.nextRequest(nextRequest); } else { - new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(this.databaseId); + new HuaweiWorkoutGbParser(getDevice()).parseWorkout(this.databaseId); if (!remainder.isEmpty()) { GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest( diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutTotalsRequest.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutTotalsRequest.java index b9c13b60d..476873f10 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutTotalsRequest.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/requests/GetWorkoutTotalsRequest.java @@ -103,7 +103,7 @@ public class GetWorkoutTotalsRequest extends Request { nextRequest.setFinalizeReq(this.finalizeReq); this.nextRequest(nextRequest); } else { - new HuaweiWorkoutGbParser(getDevice(), getContext()).parseWorkout(databaseId); + new HuaweiWorkoutGbParser(getDevice()).parseWorkout(databaseId); if (!remainder.isEmpty()) { GetWorkoutTotalsRequest nextRequest = new GetWorkoutTotalsRequest(