2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2016-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniel
|
|
|
|
Dakhno, Daniele Gobbetti, José Rebelo, Petr Vaněk
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2016-05-05 10:19:01 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.devices;
|
|
|
|
|
2016-08-07 11:45:09 +02:00
|
|
|
import java.util.ArrayList;
|
2016-06-27 20:41:20 +02:00
|
|
|
import java.util.Collections;
|
2016-05-05 10:19:01 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2016-05-05 10:19:01 +02:00
|
|
|
import de.greenrobot.dao.AbstractDao;
|
2016-06-14 23:10:35 +02:00
|
|
|
import de.greenrobot.dao.Property;
|
2016-05-05 10:19:01 +02:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
|
|
|
import de.greenrobot.dao.query.WhereCondition;
|
2016-06-27 20:41:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2016-05-13 23:47:47 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
|
2016-05-08 23:14:58 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession;
|
2016-06-27 20:41:20 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2016-05-05 10:19:01 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
2022-08-24 12:21:59 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-06-14 20:13:08 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2016-06-14 23:10:35 +02:00
|
|
|
* @param <T> the sample type
|
2016-06-14 20:13:08 +02:00
|
|
|
*/
|
2016-05-16 23:36:54 +02:00
|
|
|
public abstract class AbstractSampleProvider<T extends AbstractActivitySample> implements SampleProvider<T> {
|
2016-05-08 22:58:50 +02:00
|
|
|
private static final WhereCondition[] NO_CONDITIONS = new WhereCondition[0];
|
2016-05-08 23:14:58 +02:00
|
|
|
private final DaoSession mSession;
|
2016-06-27 20:41:20 +02:00
|
|
|
private final GBDevice mDevice;
|
2016-05-08 23:14:58 +02:00
|
|
|
|
2016-06-27 20:41:20 +02:00
|
|
|
protected AbstractSampleProvider(GBDevice device, DaoSession session) {
|
|
|
|
mDevice = device;
|
2016-05-08 23:14:58 +02:00
|
|
|
mSession = session;
|
|
|
|
}
|
|
|
|
|
2016-09-04 22:39:35 +02:00
|
|
|
public GBDevice getDevice() {
|
2016-06-27 20:41:20 +02:00
|
|
|
return mDevice;
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:41:34 +02:00
|
|
|
public DaoSession getSession() {
|
2016-05-08 23:14:58 +02:00
|
|
|
return mSession;
|
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getAllActivitySamples(int timestamp_from, int timestamp_to) {
|
2016-05-05 10:19:01 +02:00
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ALL);
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getActivitySamples(int timestamp_from, int timestamp_to) {
|
2016-08-07 11:45:09 +02:00
|
|
|
if (getRawKindSampleProperty() != null) {
|
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
|
|
|
|
} else {
|
|
|
|
return getActivitySamplesByActivityFilter(timestamp_from, timestamp_to, ActivityKind.TYPE_ACTIVITY);
|
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-16 23:00:04 +02:00
|
|
|
@Override
|
2016-05-08 22:58:50 +02:00
|
|
|
public List<T> getSleepSamples(int timestamp_from, int timestamp_to) {
|
2023-09-27 23:11:02 +02:00
|
|
|
final DeviceCoordinator coordinator = getDevice().getDeviceCoordinator();
|
2022-08-24 12:21:59 +02:00
|
|
|
|
|
|
|
// If the device does not support REM sleep, we need to exclude its bit from the activity type
|
|
|
|
int sleepActivityType = ActivityKind.TYPE_SLEEP;
|
|
|
|
if (!coordinator.supportsRemSleep()) {
|
|
|
|
sleepActivityType &= ~ActivityKind.TYPE_REM_SLEEP;
|
|
|
|
}
|
|
|
|
|
2016-08-07 11:45:09 +02:00
|
|
|
if (getRawKindSampleProperty() != null) {
|
2022-08-24 12:21:59 +02:00
|
|
|
return getGBActivitySamples(timestamp_from, timestamp_to, sleepActivityType);
|
2016-08-07 11:45:09 +02:00
|
|
|
} else {
|
2022-08-24 12:21:59 +02:00
|
|
|
return getActivitySamplesByActivityFilter(timestamp_from, timestamp_to, sleepActivityType);
|
2016-08-07 11:45:09 +02:00
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
2016-05-16 23:36:54 +02:00
|
|
|
public void addGBActivitySample(T activitySample) {
|
2016-06-14 20:13:08 +02:00
|
|
|
getSampleDao().insertOrReplace(activitySample);
|
2016-05-13 23:47:47 +02:00
|
|
|
}
|
2016-05-05 10:19:01 +02:00
|
|
|
|
2016-05-13 23:47:47 +02:00
|
|
|
@Override
|
2016-05-16 23:36:54 +02:00
|
|
|
public void addGBActivitySamples(T[] activitySamples) {
|
2016-06-14 20:13:08 +02:00
|
|
|
getSampleDao().insertOrReplaceInTx(activitySamples);
|
2016-05-13 23:47:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 00:03:23 +01:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public T getLatestActivitySample() {
|
|
|
|
QueryBuilder<T> qb = getSampleDao().queryBuilder();
|
|
|
|
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
|
|
|
|
if (dbDevice == null) {
|
|
|
|
// no device, no sample
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Property deviceProperty = getDeviceIdentifierSampleProperty();
|
|
|
|
qb.where(deviceProperty.eq(dbDevice.getId())).orderDesc(getTimestampSampleProperty()).limit(1);
|
|
|
|
List<T> samples = qb.build().list();
|
|
|
|
if (samples.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-11-24 20:12:56 +01:00
|
|
|
T sample = samples.get(0);
|
|
|
|
sample.setProvider(this);
|
|
|
|
return sample;
|
2016-11-22 00:03:23 +01:00
|
|
|
}
|
|
|
|
|
2022-07-23 23:37:48 +02:00
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public T getFirstActivitySample() {
|
|
|
|
QueryBuilder<T> qb = getSampleDao().queryBuilder();
|
|
|
|
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
|
|
|
|
if (dbDevice == null) {
|
|
|
|
// no device, no sample
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Property deviceProperty = getDeviceIdentifierSampleProperty();
|
|
|
|
qb.where(deviceProperty.eq(dbDevice.getId())).orderAsc(getTimestampSampleProperty()).limit(1);
|
|
|
|
List<T> samples = qb.build().list();
|
|
|
|
if (samples.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
T sample = samples.get(0);
|
|
|
|
sample.setProvider(this);
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
protected List<T> getGBActivitySamples(int timestamp_from, int timestamp_to, int activityType) {
|
2016-08-07 01:47:15 +02:00
|
|
|
if (getRawKindSampleProperty() == null && activityType != ActivityKind.TYPE_ALL) {
|
|
|
|
// if we do not have a raw kind property we cannot query anything else then TYPE_ALL
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
2016-05-08 22:58:50 +02:00
|
|
|
QueryBuilder<T> qb = getSampleDao().queryBuilder();
|
2016-06-14 23:10:35 +02:00
|
|
|
Property timestampProperty = getTimestampSampleProperty();
|
2016-09-04 22:39:35 +02:00
|
|
|
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
|
2016-06-27 20:41:20 +02:00
|
|
|
if (dbDevice == null) {
|
|
|
|
// no device, no samples
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
Property deviceProperty = getDeviceIdentifierSampleProperty();
|
|
|
|
qb.where(deviceProperty.eq(dbDevice.getId()), timestampProperty.ge(timestamp_from))
|
2016-06-14 23:10:35 +02:00
|
|
|
.where(timestampProperty.le(timestamp_to), getClauseForActivityType(qb, activityType));
|
2016-05-16 23:00:04 +02:00
|
|
|
List<T> samples = qb.build().list();
|
|
|
|
for (T sample : samples) {
|
2016-05-16 23:36:54 +02:00
|
|
|
sample.setProvider(this);
|
2016-05-16 23:00:04 +02:00
|
|
|
}
|
2016-08-27 21:40:46 +02:00
|
|
|
detachFromSession();
|
2016-05-16 23:00:04 +02:00
|
|
|
return samples;
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-27 21:40:46 +02:00
|
|
|
/**
|
|
|
|
* Detaches all samples of this type from the session. Changes to them may not be
|
|
|
|
* written back to the database.
|
|
|
|
*
|
|
|
|
* Subclasses should call this method after performing custom queries.
|
|
|
|
*/
|
|
|
|
protected void detachFromSession() {
|
|
|
|
getSampleDao().detachAll();
|
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
private WhereCondition[] getClauseForActivityType(QueryBuilder qb, int activityTypes) {
|
2016-05-05 10:19:01 +02:00
|
|
|
if (activityTypes == ActivityKind.TYPE_ALL) {
|
2016-05-08 22:58:50 +02:00
|
|
|
return NO_CONDITIONS;
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int[] dbActivityTypes = ActivityKind.mapToDBActivityTypes(activityTypes, this);
|
2016-05-08 22:58:50 +02:00
|
|
|
WhereCondition activityTypeCondition = getActivityTypeConditions(qb, dbActivityTypes);
|
|
|
|
return new WhereCondition[] { activityTypeCondition };
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
private WhereCondition getActivityTypeConditions(QueryBuilder qb, int[] dbActivityTypes) {
|
|
|
|
// What a crappy QueryBuilder API ;-( QueryBuilder.or(WhereCondition[]) with a runtime array length
|
|
|
|
// check would have worked just fine.
|
|
|
|
if (dbActivityTypes.length == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-14 23:10:35 +02:00
|
|
|
Property rawKindProperty = getRawKindSampleProperty();
|
2016-08-27 15:36:42 +02:00
|
|
|
if (rawKindProperty == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-08 22:58:50 +02:00
|
|
|
if (dbActivityTypes.length == 1) {
|
2016-06-14 23:10:35 +02:00
|
|
|
return rawKindProperty.eq(dbActivityTypes[0]);
|
2016-05-08 22:58:50 +02:00
|
|
|
}
|
|
|
|
if (dbActivityTypes.length == 2) {
|
2016-06-14 23:10:35 +02:00
|
|
|
return qb.or(rawKindProperty.eq(dbActivityTypes[0]),
|
|
|
|
rawKindProperty.eq(dbActivityTypes[1]));
|
2016-05-08 22:58:50 +02:00
|
|
|
}
|
|
|
|
final int offset = 2;
|
|
|
|
int len = dbActivityTypes.length - offset;
|
|
|
|
WhereCondition[] trailingConditions = new WhereCondition[len];
|
|
|
|
for (int i = 0; i < len; i++) {
|
2016-06-14 23:10:35 +02:00
|
|
|
trailingConditions[i] = rawKindProperty.eq(dbActivityTypes[i + offset]);
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
2016-06-14 23:10:35 +02:00
|
|
|
return qb.or(rawKindProperty.eq(dbActivityTypes[0]),
|
|
|
|
rawKindProperty.eq(dbActivityTypes[1]),
|
2016-05-08 22:58:50 +02:00
|
|
|
trailingConditions);
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-07 11:45:09 +02:00
|
|
|
private List<T> getActivitySamplesByActivityFilter(int timestamp_from, int timestamp_to, int activityFilter) {
|
|
|
|
List<T> samples = getAllActivitySamples(timestamp_from, timestamp_to);
|
|
|
|
List<T> filteredSamples = new ArrayList<>();
|
|
|
|
|
|
|
|
for (T sample : samples) {
|
|
|
|
if ((sample.getKind() & activityFilter) != 0) {
|
|
|
|
filteredSamples.add(sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredSamples;
|
|
|
|
}
|
|
|
|
|
2016-06-16 21:54:53 +02:00
|
|
|
public abstract AbstractDao<T,?> getSampleDao();
|
2016-06-14 23:10:35 +02:00
|
|
|
|
2016-08-27 15:36:42 +02:00
|
|
|
@Nullable
|
2016-06-14 23:10:35 +02:00
|
|
|
protected abstract Property getRawKindSampleProperty();
|
2016-08-27 15:36:42 +02:00
|
|
|
|
|
|
|
@NonNull
|
2016-06-14 23:10:35 +02:00
|
|
|
protected abstract Property getTimestampSampleProperty();
|
2016-08-27 15:36:42 +02:00
|
|
|
|
|
|
|
@NonNull
|
2016-06-27 20:41:20 +02:00
|
|
|
protected abstract Property getDeviceIdentifierSampleProperty();
|
2016-05-05 10:19:01 +02:00
|
|
|
}
|