@@ -142,15 +142,17 @@ private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLit
142142 //the last run time is computed instead of just using "now" so that if the more than
143143 // one period has been skipped, all intermediate transactions can be created
144144
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!!
145148 //update the last run time and execution count
146149 ContentValues contentValues = new ContentValues ();
147- contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_LAST_RUN , System .currentTimeMillis ());
148- contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_EXECUTION_COUNT , executionCount );
150+ contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_LAST_RUN ,
151+ scheduledAction .getLastRunTime ());
152+ contentValues .put (DatabaseSchema .ScheduledActionEntry .COLUMN_EXECUTION_COUNT ,
153+ scheduledAction .getExecutionCount ());
149154 db .update (DatabaseSchema .ScheduledActionEntry .TABLE_NAME , contentValues ,
150155 DatabaseSchema .ScheduledActionEntry .COLUMN_UID + "=?" , new String []{scheduledAction .getUID ()});
151-
152- //set the execution count in the object because it will be checked for the next iteration in the calling loop
153- scheduledAction .setExecutionCount (executionCount ); //this call is important, do not remove!!
154156 }
155157
156158 /**
@@ -161,26 +163,31 @@ private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLit
161163 * @return Number of times backup is executed. This should either be 1 or 0
162164 */
163165 private static int executeBackup (ScheduledAction scheduledAction , SQLiteDatabase db ) {
164- int executionCount = 0 ;
165- long now = System .currentTimeMillis ();
166- long endTime = scheduledAction .getEndTime ();
167-
168- if (endTime > 0 && endTime < now )
169- return executionCount ;
170-
171- if (scheduledAction .computeNextScheduledExecutionTime () > now )
166+ if (!shouldExecuteScheduledBackup (scheduledAction ))
172167 return 0 ;
173168
174169 ExportParams params = ExportParams .parseCsv (scheduledAction .getTag ());
175170 try {
176171 //wait for async task to finish before we proceed (we are holding a wake lock)
177172 new ExportAsyncTask (GnuCashApplication .getAppContext (), db ).execute (params ).get ();
178- scheduledAction .setExecutionCount (++executionCount );
179173 } catch (InterruptedException | ExecutionException e ) {
180174 Crashlytics .logException (e );
181175 Log .e (LOG_TAG , e .getMessage ());
182176 }
183- return executionCount ;
177+ return 1 ;
178+ }
179+
180+ private static boolean shouldExecuteScheduledBackup (ScheduledAction scheduledAction ) {
181+ long now = System .currentTimeMillis ();
182+ long endTime = scheduledAction .getEndTime ();
183+
184+ if (endTime > 0 && endTime < now )
185+ return false ;
186+
187+ if (scheduledAction .computeNextTimeBasedScheduledExecutionTime () > now )
188+ return false ;
189+
190+ return true ;
184191 }
185192
186193 /**
@@ -214,7 +221,7 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
214221
215222 //we may be executing scheduled action significantly after scheduled time (depending on when Android fires the alarm)
216223 //so compute the actual transaction time from pre-known values
217- long transactionTime = scheduledAction .computeNextScheduledExecutionTime ();
224+ long transactionTime = scheduledAction .computeNextCountBasedScheduledExecutionTime ();
218225 while (transactionTime <= endTime ) {
219226 Transaction recurringTrxn = new Transaction (trxnTemplate , true );
220227 recurringTrxn .setTime (transactionTime );
@@ -224,7 +231,7 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa
224231
225232 if (totalPlannedExecutions > 0 && executionCount >= totalPlannedExecutions )
226233 break ; //if we hit the total planned executions set, then abort
227- transactionTime = scheduledAction .computeNextScheduledExecutionTime ();
234+ transactionTime = scheduledAction .computeNextCountBasedScheduledExecutionTime ();
228235 }
229236
230237 transactionsDbAdapter .bulkAddRecords (transactions , DatabaseAdapter .UpdateMethod .insert );
0 commit comments