77import android .database .sqlite .SQLiteOpenHelper ;
88import android .support .annotation .NonNull ;
99import android .support .annotation .Nullable ;
10-
1110import com .sukesan1984 .stepsensorlib .model .ChunkStepCount ;
1211import com .sukesan1984 .stepsensorlib .util .DateUtils ;
1312import com .sukesan1984 .stepsensorlib .util .Logger ;
14-
1513import java .util .ArrayList ;
1614import java .util .List ;
17- import java .util .concurrent .atomic .AtomicInteger ;
1815
1916class 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
0 commit comments