@Override public void changeStoredSamplesType( int timestampFrom, int timestampTo, byte kind, SampleProvider provider) { try (SQLiteDatabase db = this.getReadableDatabase()) { String sql = "UPDATE " + TABLE_GBACTIVITYSAMPLES + " SET " + KEY_TYPE + "= ? WHERE " + KEY_PROVIDER + " = ? AND " + KEY_TIMESTAMP + " >= ? AND " + KEY_TIMESTAMP + " < ? ;"; // do not use BETWEEN because the range is inclusive in that case! SQLiteStatement statement = db.compileStatement(sql); statement.bindLong(1, kind); statement.bindLong(2, provider.getID()); statement.bindLong(3, timestampFrom); statement.bindLong(4, timestampTo); statement.execute(); } }
@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")) { if (cursor.moveToFirst()) { return cursor.getInt(0); } } } return -1; }
/** * Returns all available activity samples from between the two timestamps (inclusive), of the * given provided and type(s). * * @param timestamp_from * @param timestamp_to * @param activityTypes ORed combination of #TYPE_DEEP_SLEEP, #TYPE_LIGHT_SLEEP, #TYPE_ACTIVITY * @param provider the producer of the samples to be sought * @return */ private ArrayList<ActivitySample> getGBActivitySamples( int timestamp_from, int timestamp_to, int activityTypes, SampleProvider provider) { if (timestamp_to < 0) { throw new IllegalArgumentException("negative timestamp_to"); } if (timestamp_from < 0) { throw new IllegalArgumentException("negative timestamp_from"); } ArrayList<ActivitySample> samples = new ArrayList<>(); final String where = "(provider=" + provider.getID() + " and timestamp>=" + timestamp_from + " and timestamp<=" + timestamp_to + getWhereClauseFor(activityTypes, provider) + ")"; LOG.info("Activity query where: " + where); final String order = "timestamp"; try (SQLiteDatabase db = this.getReadableDatabase()) { try (Cursor cursor = db.query(TABLE_GBACTIVITYSAMPLES, null, where, null, null, null, order)) { LOG.info("Activity query result: " + cursor.getCount() + " samples"); if (cursor.moveToFirst()) { do { GBActivitySample sample = new GBActivitySample( provider, cursor.getInt(cursor.getColumnIndex(KEY_TIMESTAMP)), cursor.getShort(cursor.getColumnIndex(KEY_INTENSITY)), cursor.getShort(cursor.getColumnIndex(KEY_STEPS)), (byte) cursor.getShort(cursor.getColumnIndex(KEY_TYPE))); samples.add(sample); } while (cursor.moveToNext()); } } } return samples; }