1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

add method for batch inserts in ActivityDatabaseHandler. Closes #150

This commit is contained in:
Andreas Shimokawa 2015-10-23 22:11:21 +09:00
parent 0cd9b0419c
commit e809c490dc
3 changed files with 48 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.widget.Toast;
import org.slf4j.Logger;
@ -138,6 +139,30 @@ public class ActivityDatabaseHandler extends SQLiteOpenHelper implements DBHandl
}
}
@Override
public void addGBActivitySamples(GBActivitySample[] activitySamples) {
try (SQLiteDatabase db = this.getWritableDatabase()) {
String sql = "INSERT INTO " + TABLE_GBACTIVITYSAMPLES + " (" + KEY_TIMESTAMP + "," +
KEY_PROVIDER + "," + KEY_INTENSITY + "," + KEY_STEPS + "," + KEY_TYPE + ")" +
" VALUES (?,?,?,?,?);";
SQLiteStatement statement = db.compileStatement(sql);
db.beginTransaction();
for (GBActivitySample activitySample : activitySamples) {
statement.clearBindings();
statement.bindLong(1, activitySample.getTimestamp());
statement.bindLong(2, activitySample.getProvider().getID());
statement.bindLong(3, activitySample.getRawIntensity());
statement.bindLong(4, activitySample.getSteps());
statement.bindLong(5, activitySample.getRawKind());
statement.execute();
}
db.setTransactionSuccessful();
db.endTransaction();
}
}
public ArrayList<ActivitySample> getSleepSamples(int timestamp_from, int timestamp_to, SampleProvider provider) {
return getGBActivitySamples(timestamp_from, timestamp_to, ActivityKind.TYPE_SLEEP, provider);
}

View File

@ -7,10 +7,11 @@ import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySample;
public interface DBHandler {
public SQLiteOpenHelper getHelper();
SQLiteOpenHelper getHelper();
/**
* Releases the DB handler. No access may be performed after calling this method.
@ -26,5 +27,7 @@ public interface DBHandler {
void addGBActivitySample(int timestamp, byte provider, short intensity, short steps, byte kind);
void addGBActivitySamples(GBActivitySample[] activitySamples);
SQLiteDatabase getWritableDatabase();
}

View File

@ -17,7 +17,8 @@ import nodomain.freeyourgadget.gadgetbridge.GBException;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEvent;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSendBytes;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.devices.pebble.MisfitSampleProvider;
import nodomain.freeyourgadget.gadgetbridge.impl.GBActivitySample;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
public class AppMessageHandlerMisfit extends AppMessageHandler {
@ -38,6 +39,8 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
super(uuid, pebbleProtocol);
}
private final MisfitSampleProvider sampleProvider = new MisfitSampleProvider();
@Override
public GBDeviceEvent[] handleMessage(ArrayList<Pair<Integer, Object>> pairs) {
for (Pair<Integer, Object> pair : pairs) {
@ -49,14 +52,6 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
LOG.info("incoming data end");
break;
case KEY_INCOMING_DATA:
DBHandler db = null;
try {
db = GBApplication.acquireDB();
} catch (GBException e) {
LOG.error("Error acquiring database", e);
return null;
}
byte[] data = (byte[]) pair.second;
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.LITTLE_ENDIAN);
@ -76,11 +71,13 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
int steps = 0;
int totalSteps = 0;
GBActivitySample[] activitySamples = new GBActivitySample[samples];
for (int i = 0; i < samples; i++) {
short sample = buf.getShort();
if ((sample & 0x0001) == 0 || (sample & 0xff00) == 0) { // 16-??? steps encoded in bits 1-7
steps = (sample & 0x00fe);
} else if ((sample & 0xfc71) == 0xfc71) { // 0-14 steps encoded in bits 1-3, bits 8-9 unknown, all other seem to be all 1 in this case
} else if ((sample & 0x0001) == 0x0001) { // 0-14 steps encoded in bits 1-3, most of the time fc71 bits are set in that case
// 0040 also set always?
steps = (sample & 0x000e);
} else {
steps = 0;
@ -91,12 +88,21 @@ public class AppMessageHandlerMisfit extends AppMessageHandler {
if (steps > 0) {
activityKind = ActivityKind.TYPE_ACTIVITY;
}
db.addGBActivitySample(timestamp + i * 60, SampleProvider.PROVIDER_PEBBLE_MISFIT, (short) steps, (short) steps, activityKind);
activitySamples[i] = new GBActivitySample(sampleProvider, timestamp + i * 60, (short) steps, (short) steps, activityKind);
}
LOG.info("total steps for above period: " + totalSteps);
if (db != null) {
db.release();
DBHandler db = null;
try {
db = GBApplication.acquireDB();
db.addGBActivitySamples(activitySamples);
} catch (GBException e) {
LOG.error("Error acquiring database", e);
return null;
} finally {
if (db != null) {
db.release();
}
}
break;
default: