Skip to content

Commit fe7e8b6

Browse files
committed
Atomically mark as recorded to server on increasing step count
1 parent aa186cb commit fe7e8b6

2 files changed

Lines changed: 11 additions & 55 deletions

File tree

src/main/java/com/sukesan1984/stepsensorlib/Database.java

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
import android.database.sqlite.SQLiteOpenHelper;
88
import android.support.annotation.NonNull;
99
import android.support.annotation.Nullable;
10-
1110
import com.sukesan1984.stepsensorlib.model.ChunkStepCount;
1211
import com.sukesan1984.stepsensorlib.util.DateUtils;
1312
import com.sukesan1984.stepsensorlib.util.Logger;
14-
1513
import java.util.ArrayList;
1614
import java.util.List;
17-
import java.util.concurrent.atomic.AtomicInteger;
1815

1916
class Database extends SQLiteOpenHelper {
2017
private final static String TABLE_NAME = "steps";
@@ -85,7 +82,7 @@ public int addSteps(long targetDateAndHour, int stepsToAdd) {
8582
db.beginTransaction();
8683
int currentSteps = getStepsImpl(db, targetDateAndHour);
8784
int newSteps = currentSteps + stepsToAdd;
88-
insertOrReplaceStepRow(db, targetDateAndHour, newSteps, true);
85+
insertOrReplaceStepRow(db, targetDateAndHour, newSteps, false);
8986
db.setTransactionSuccessful();
9087
return newSteps;
9188
} catch (Exception e) {
@@ -98,13 +95,13 @@ public int addSteps(long targetDateAndHour, int stepsToAdd) {
9895
}
9996
}
10097

101-
private void insertOrReplaceStepRow(SQLiteDatabase db, long dateAndHour, int steps, boolean markNotRecorded) {
98+
private void insertOrReplaceStepRow(SQLiteDatabase db, long dateAndHour, int steps, @Nullable Boolean markAsRecorded) {
10299
ContentValues values = new ContentValues();
103100
values.put(COLUMN_DATE_AND_HOUR, dateAndHour);
104101
values.put(COLUMN_STEPS, steps);
105102
values.put(COLUMN_LAST_UPDATED, DateUtils.getCurrentTimeMllis());
106-
if (markNotRecorded) {
107-
values.put(COLUMN_IS_RECORDED_ON_SERVER, 0);
103+
if (markAsRecorded != null) {
104+
values.put(COLUMN_IS_RECORDED_ON_SERVER, markAsRecorded ? 1 : 0);
108105
}
109106
db.replaceOrThrow(TABLE_NAME, null, values);
110107
}
@@ -221,45 +218,13 @@ private List<ChunkStepCount> createChunkedStepCounts(Cursor c) {
221218
return lists;
222219
}
223220

224-
public void updateToRecorded(List<Long> dateAndHours) {
225-
SQLiteDatabase db = null;
226-
try {
227-
ContentValues values = new ContentValues();
228-
values.put(COLUMN_IS_RECORDED_ON_SERVER, "1");
229-
int length = dateAndHours.size();
230-
String args = "";
231-
String[] dateAndHoursString = new String[length];
232-
for (int i = 0; i < length; i++) {
233-
if (i == 0) {
234-
args = "?";
235-
} else {
236-
args += ", ?";
237-
}
238-
dateAndHoursString[i] = String.valueOf(dateAndHours.get(i));
239-
}
240-
241-
db = getWritableDatabase();
242-
db.beginTransaction();
243-
int rows = db.update(TABLE_NAME, values, String.format(COLUMN_DATE_AND_HOUR + " in (%s)", args), dateAndHoursString);
244-
Logger.log("updated number: " + rows);
245-
db.setTransactionSuccessful();
246-
Logger.log("update to Recorded: " + dateAndHoursString.toString());
247-
} catch (Exception e) {
248-
e.printStackTrace();
249-
} finally {
250-
if (db != null) {
251-
db.endTransaction();
252-
}
253-
}
254-
}
255-
256-
public void increaseByChunkStepCounts(List<ChunkStepCount> chunkStepCounts) {
221+
public void increaseByServerChunkStepCounts(List<ChunkStepCount> chunkStepCounts) {
257222
SQLiteDatabase db = null;
258223
try {
259224
db = getWritableDatabase();
260225
db.beginTransaction();
261226
for (ChunkStepCount chunkStepCount : chunkStepCounts) {
262-
increaseByChunkStepCount(db, chunkStepCount);
227+
increaseBySeverChunkStepCount(db, chunkStepCount);
263228
}
264229
db.setTransactionSuccessful();
265230
} finally {
@@ -269,10 +234,10 @@ public void increaseByChunkStepCounts(List<ChunkStepCount> chunkStepCounts) {
269234
}
270235
}
271236

272-
private void increaseByChunkStepCount(SQLiteDatabase db, ChunkStepCount chunkStepCount) {
237+
private void increaseBySeverChunkStepCount(SQLiteDatabase db, ChunkStepCount chunkStepCount) {
273238
int currentSteps = getStepsImpl(db, chunkStepCount.unixTimeMillis);
274-
if (chunkStepCount.steps > currentSteps) {
275-
insertOrReplaceStepRow(db, chunkStepCount.unixTimeMillis, chunkStepCount.steps, false);
239+
if (chunkStepCount.steps >= currentSteps) {
240+
insertOrReplaceStepRow(db, chunkStepCount.unixTimeMillis, chunkStepCount.steps, true);
276241
}
277242
}
278243

src/main/java/com/sukesan1984/stepsensorlib/StepSensorFacade.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static void clearAllData(Context context) {
4141
context.startService(SensorListener.createIntentForReset(context));
4242
}
4343

44-
public static void increaseByChunkStepCounts(Context context, List<ChunkStepCount> chunkStepCounts) {
44+
public static void increaseByServerChunkStepCounts(Context context, List<ChunkStepCount> chunkStepCounts) {
4545
saveNow(context);
46-
Database.getInstance(context).increaseByChunkStepCounts(chunkStepCounts);
46+
Database.getInstance(context).increaseByServerChunkStepCounts(chunkStepCounts);
4747
}
4848

4949
@NonNull
@@ -57,13 +57,4 @@ public static List<ChunkStepCount> getNotRecordedChunkStepCounts(Context context
5757
saveNow(context);
5858
return Database.getInstance(context).getNotRecordedChunkStepCounts();
5959
}
60-
61-
public static void markChunkStepCountsAsRecorded(Context context, List<ChunkStepCount> chunkStepCounts) {
62-
saveNow(context);
63-
List<Long> dateAndHours = new ArrayList<>(chunkStepCounts.size());
64-
for (ChunkStepCount chunkStepCount : chunkStepCounts) {
65-
dateAndHours.add(chunkStepCount.unixTimeMillis);
66-
}
67-
Database.getInstance(context).updateToRecorded(dateAndHours);
68-
}
6960
}

0 commit comments

Comments
 (0)