@@ -127,7 +127,7 @@ public static void processScheduledActions(List<ScheduledAction> scheduledAction
127127 */
128128 private static void executeScheduledEvent (ScheduledAction scheduledAction , SQLiteDatabase db ){
129129 Log .i (LOG_TAG , "Executing scheduled action: " + scheduledAction .toString ());
130- int executionCount = scheduledAction . getExecutionCount () ;
130+ int executionCount = 0 ;
131131
132132 switch (scheduledAction .getActionType ()){
133133 case TRANSACTION :
@@ -139,20 +139,21 @@ private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLit
139139 break ;
140140 }
141141
142- //the last run time is computed instead of just using "now" so that if the more than
143- // one period has been skipped, all intermediate transactions can be created
144-
145- scheduledAction .setLastRun (System .currentTimeMillis ());
146- //set the execution count in the object because it will be checked for the next iteration in the calling loop
147- scheduledAction .setExecutionCount (executionCount ); //this call is important, do not remove!!
148- //update the last run time and execution count
149- ContentValues contentValues = new ContentValues ();
150- contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_LAST_RUN ,
151- scheduledAction .getLastRunTime ());
152- contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_EXECUTION_COUNT ,
153- scheduledAction .getExecutionCount ());
154- db .update (DatabaseSchema .ScheduledActionEntry .TABLE_NAME , contentValues ,
155- DatabaseSchema .ScheduledActionEntry .COLUMN_UID + "=?" , new String []{scheduledAction .getUID ()});
142+ if (executionCount > 0 ) {
143+ scheduledAction .setLastRun (System .currentTimeMillis ());
144+ // Set the execution count in the object because it will be checked
145+ // for the next iteration in the calling loop.
146+ // This call is important, do not remove!!
147+ scheduledAction .setExecutionCount (scheduledAction .getExecutionCount () + executionCount );
148+ // Update the last run time and execution count
149+ ContentValues contentValues = new ContentValues ();
150+ contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_LAST_RUN ,
151+ scheduledAction .getLastRunTime ());
152+ contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_EXECUTION_COUNT ,
153+ scheduledAction .getExecutionCount ());
154+ db .update (DatabaseSchema .ScheduledActionEntry .TABLE_NAME , contentValues ,
155+ DatabaseSchema .ScheduledActionEntry .COLUMN_UID + "=?" , new String []{scheduledAction .getUID ()});
156+ }
156157 }
157158
158159 /**
@@ -219,6 +220,7 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
219220 int totalPlannedExecutions = scheduledAction .getTotalPlannedExecutionCount ();
220221 List <Transaction > transactions = new ArrayList <>();
221222
223+ int previousExecutionCount = scheduledAction .getExecutionCount (); // We'll modify it
222224 //we may be executing scheduled action significantly after scheduled time (depending on when Android fires the alarm)
223225 //so compute the actual transaction time from pre-known values
224226 long transactionTime = scheduledAction .computeNextCountBasedScheduledExecutionTime ();
@@ -235,6 +237,8 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
235237 }
236238
237239 transactionsDbAdapter .bulkAddRecords (transactions , DatabaseAdapter .UpdateMethod .insert );
240+ // Be nice and restore the parameter's original state to avoid confusing the callers
241+ scheduledAction .setExecutionCount (previousExecutionCount );
238242 return executionCount ;
239243 }
240244}
0 commit comments