1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-14 05:59:26 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/AbstractSampleProvider.java

237 lines
8.9 KiB
Java
Raw Normal View History

2021-01-10 23:37:09 +01:00
/* Copyright (C) 2016-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
2019-02-13 20:43:30 +01:00
Gobbetti
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
along with this program. If not, see <http://www.gnu.org/licenses/>. */
2016-05-05 10:19:01 +02:00
package nodomain.freeyourgadget.gadgetbridge.devices;
import java.util.ArrayList;
import java.util.Collections;
2016-05-05 10:19:01 +02:00
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2016-05-05 10:19:01 +02:00
import de.greenrobot.dao.AbstractDao;
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;
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;
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.
* @param <T> the sample type
2016-06-14 20:13:08 +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;
private final GBDevice mDevice;
2016-05-08 23:14:58 +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() {
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
@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);
}
@Override
2016-05-08 22:58:50 +02:00
public List<T> getActivitySamples(int timestamp_from, int timestamp_to) {
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
}
@Override
2016-05-08 22:58:50 +02:00
public List<T> getSleepSamples(int timestamp_from, int timestamp_to) {
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;
}
if (getRawKindSampleProperty() != null) {
2022-08-24 12:21:59 +02:00
return getGBActivitySamples(timestamp_from, timestamp_to, sleepActivityType);
} else {
2022-08-24 12:21:59 +02:00
return getActivitySamplesByActivityFilter(timestamp_from, timestamp_to, sleepActivityType);
}
2016-05-05 10:19:01 +02:00
}
2016-05-13 23:47:47 +02:00
@Override
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
public void addGBActivitySamples(T[] activitySamples) {
2016-06-14 20:13:08 +02:00
getSampleDao().insertOrReplaceInTx(activitySamples);
2016-05-13 23:47:47 +02: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;
}
T sample = samples.get(0);
sample.setProvider(this);
return sample;
}
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) {
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();
Property timestampProperty = getTimestampSampleProperty();
2016-09-04 22:39:35 +02:00
Device dbDevice = DBHelper.findDevice(getDevice(), getSession());
if (dbDevice == null) {
// no device, no samples
return Collections.emptyList();
}
Property deviceProperty = getDeviceIdentifierSampleProperty();
qb.where(deviceProperty.eq(dbDevice.getId()), timestampProperty.ge(timestamp_from))
.where(timestampProperty.le(timestamp_to), getClauseForActivityType(qb, activityType));
List<T> samples = qb.build().list();
for (T sample : samples) {
sample.setProvider(this);
}
detachFromSession();
return samples;
2016-05-05 10:19:01 +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;
}
Property rawKindProperty = getRawKindSampleProperty();
if (rawKindProperty == null) {
return null;
}
2016-05-08 22:58:50 +02:00
if (dbActivityTypes.length == 1) {
return rawKindProperty.eq(dbActivityTypes[0]);
2016-05-08 22:58:50 +02:00
}
if (dbActivityTypes.length == 2) {
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++) {
trailingConditions[i] = rawKindProperty.eq(dbActivityTypes[i + offset]);
2016-05-05 10:19:01 +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
}
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();
@Nullable
protected abstract Property getRawKindSampleProperty();
@NonNull
protected abstract Property getTimestampSampleProperty();
@NonNull
protected abstract Property getDeviceIdentifierSampleProperty();
2016-05-05 10:19:01 +02:00
}