Skip to content

Commit

Permalink
Code refactoring and cleanup
Browse files Browse the repository at this point in the history
Renamed SchedulerService to ScheduledActionService
  • Loading branch information
codinguser committed Aug 16, 2016
1 parent 893bd1a commit e83dc93
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
android:enabled="true"
android:exported="false"
android:label="Dropbox Sync" />
<service android:name=".service.SchedulerService"
<service android:name=".service.ScheduledActionService"
android:exported="false"
android:label="GnuCash Android Scheduler Execution Service"/>
<receiver android:name=".receivers.TransactionRecorder"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.service.SchedulerService;
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.ui.account.AccountsActivity;
import org.gnucash.android.ui.settings.PreferenceActivity;

Expand Down Expand Up @@ -127,9 +127,6 @@ public void onCreate(){
mBooksDbAdapter = new BooksDbAdapter(bookDbHelper.getWritableDatabase());

initDatabaseAdapters();

//TODO: migrate preferences from defaultShared to book

setDefaultCurrencyCode(getDefaultCurrencyCode());

if (BuildConfig.DEBUG && !isRoboUnitTest())
Expand Down Expand Up @@ -344,7 +341,7 @@ public static Locale getDefaultLocale() {
* @param context Application context
*/
public static void startScheduledActionExecutionService(Context context){
Intent alarmIntent = new Intent(context, SchedulerService.class);
Intent alarmIntent = new Intent(context, ScheduledActionService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE);

if (pendingIntent != null) //if service is already scheduled, just return
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/gnucash/android/db/MigrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.gnucash.android.model.Recurrence;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.service.SchedulerService;
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.util.PreferencesHelper;
import org.gnucash.android.util.TimestampHelper;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -1465,7 +1465,7 @@ static int upgradeDbToVersion13(SQLiteDatabase db){
.apply();

//cancel the existing pending intent so that the alarm can be rescheduled
Intent alarmIntent = new Intent(context, SchedulerService.class);
Intent alarmIntent = new Intent(context, ScheduledActionService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public long updateRecurrenceAttributes(ScheduledAction scheduledAction){
contentValues.put(ScheduledActionEntry.COLUMN_START_TIME, scheduledAction.getStartTime());
contentValues.put(ScheduledActionEntry.COLUMN_END_TIME, scheduledAction.getEndTime());
contentValues.put(ScheduledActionEntry.COLUMN_TAG, scheduledAction.getTag());
contentValues.put(ScheduledActionEntry.COLUMN_TOTAL_FREQUENCY, scheduledAction.getTotalFrequency());
contentValues.put(ScheduledActionEntry.COLUMN_TOTAL_FREQUENCY, scheduledAction.getTotalPlannedExecutionCount());

Log.d(LOG_TAG, "Updating scheduled event recurrence attributes");
String where = ScheduledActionEntry.COLUMN_UID + "=?";
Expand All @@ -138,7 +138,7 @@ public long updateRecurrenceAttributes(ScheduledAction scheduledAction){
stmt.bindNull(8);
else
stmt.bindString(8, schedxAction.getTag());
stmt.bindString(9, Integer.toString(schedxAction.getTotalFrequency()));
stmt.bindString(9, Integer.toString(schedxAction.getTotalPlannedExecutionCount()));
stmt.bindString(10, schedxAction.getRecurrence().getUID());
stmt.bindLong(11, schedxAction.shouldAutoCreate() ? 1 : 0);
stmt.bindLong(12, schedxAction.shouldAutoNotify() ? 1 : 0);
Expand Down Expand Up @@ -182,7 +182,7 @@ public ScheduledAction buildModelInstance(@NonNull final Cursor cursor){
event.setLastRun(lastRun);
event.setTag(tag);
event.setEnabled(enabled);
event.setTotalFrequency(numOccurrences);
event.setTotalPlannedExecutionCount(numOccurrences);
event.setExecutionCount(execCount);
event.setAutoCreate(autoCreate == 1);
event.setAutoNotify(autoNotify == 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,6 @@ private ArrayList<Uri> convertFilePathsToUris(List<String> paths) {
* @throws IOException if the file could not be moved.
*/
public void moveFile(String src, String dst) throws IOException {
//TODO: Make this asynchronous at some time, t in the future.
File srcFile = new File(src);
File dstFile = new File(dst);
FileChannel inChannel = new FileInputStream(srcFile).getChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ private void generateOfx(Document doc, Element parent){
}
}

// FIXME: Move code to generateExport()
/**
* Generate OFX export file from the transactions in the database
* @return String containing OFX export
* @throws ExporterException
*/
private String generateOfxExport() throws ExporterException {
mAccountsList = mAccountsDbAdapter.getExportableAccounts(mExportParams.getExportStartTime());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ public void endElement(String uri, String localName, String qualifiedName) throw
break;
//todo: export auto_notify, advance_create, advance_notify
case GncXmlHelper.TAG_SX_NUM_OCCUR:
mScheduledAction.setTotalFrequency(Integer.parseInt(characterString));
mScheduledAction.setTotalPlannedExecutionCount(Integer.parseInt(characterString));
break;
case GncXmlHelper.TAG_RX_MULT:
mRecurrenceMultiplier = Integer.parseInt(characterString);
Expand Down Expand Up @@ -1078,7 +1078,7 @@ private int generateMissedScheduledTransactions(ScheduledAction scheduledAction)
if (scheduledAction.getActionType() != ScheduledAction.ActionType.TRANSACTION
|| !scheduledAction.isEnabled() || !scheduledAction.shouldAutoCreate()
|| (scheduledAction.getEndTime() > 0 && scheduledAction.getEndTime() > System.currentTimeMillis())
|| (scheduledAction.getTotalFrequency() > 0 && scheduledAction.getExecutionCount() >= scheduledAction.getTotalFrequency())){
|| (scheduledAction.getTotalPlannedExecutionCount() > 0 && scheduledAction.getExecutionCount() >= scheduledAction.getTotalPlannedExecutionCount())){
return 0;
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/gnucash/android/model/ScheduledAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum ActionType {TRANSACTION, BACKUP}
private ActionType mActionType;

/**
* Number of times this event is to be executed
* Number of times this event is planned to be executed
*/
private int mTotalFrequency = 0;

Expand Down Expand Up @@ -297,16 +297,16 @@ public void setEnabled(boolean enabled){
* Returns the total number of planned occurrences of this scheduled action.
* @return Total number of planned occurrences of this action
*/
public int getTotalFrequency(){
public int getTotalPlannedExecutionCount(){
return mTotalFrequency;
}

/**
* Sets the number of occurences of this action
* @param occurencesCount Number of occurences
* @param plannedExecutions Number of occurences
*/
public void setTotalFrequency(int occurencesCount){
this.mTotalFrequency = occurencesCount;
public void setTotalPlannedExecutionCount(int plannedExecutions){
this.mTotalFrequency = plannedExecutions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
* Scheduled runs of the service should be achieved using an {@link android.app.AlarmManager}</p>
* @author Ngewi Fet <ngewif@gmail.com>
*/
public class SchedulerService extends IntentService {
public class ScheduledActionService extends IntentService {

public static final String LOG_TAG = "SchedulerService";
public static final String LOG_TAG = "ScheduledActionService";

public SchedulerService() {
public ScheduledActionService() {
super(LOG_TAG);
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public static void processScheduledActions(List<ScheduledAction> scheduledAction
for (ScheduledAction scheduledAction : scheduledActions) {

long now = System.currentTimeMillis();
int totalPlannedExecutions = scheduledAction.getTotalFrequency();
int totalPlannedExecutions = scheduledAction.getTotalPlannedExecutionCount();
int executionCount = scheduledAction.getExecutionCount();

if (scheduledAction.getStartTime() > now //if schedule begins in the future
Expand Down Expand Up @@ -133,7 +133,7 @@ public static void executeScheduledEvent(ScheduledAction scheduledAction, SQLite
//if the end time is in the future, we execute all schedules until now (current time)
//if there is no end time, we execute all schedules until now
long endTime = scheduledAction.getEndTime() > 0 ? Math.min(scheduledAction.getEndTime(), now) : now;
int totalPlannedExecutions = scheduledAction.getTotalFrequency();
int totalPlannedExecutions = scheduledAction.getTotalPlannedExecutionCount();
List<Transaction> transactions = new ArrayList<>();

//we may be executing scheduled action significantly after scheduled time (depending on when Android fires the alarm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testGenerateRepeatString(){
PeriodType periodType = PeriodType.MONTH;
periodType.setMultiplier(2);
scheduledAction.setRecurrence(new Recurrence(periodType));
scheduledAction.setTotalFrequency(4);
scheduledAction.setTotalPlannedExecutionCount(4);

String repeatString = "Every 2 months, for 4 times";
assertThat(scheduledAction.getRepeatString().trim()).isEqualTo(repeatString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Split;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.service.SchedulerService;
import org.gnucash.android.service.ScheduledActionService;
import org.gnucash.android.test.unit.testutil.GnucashTestRunner;
import org.gnucash.android.test.unit.testutil.ShadowCrashlytics;
import org.gnucash.android.test.unit.testutil.ShadowUserVoice;
Expand Down Expand Up @@ -134,7 +134,7 @@ public void disabledScheduledActions_shouldNotRun(){
TransactionsDbAdapter trxnAdapter = TransactionsDbAdapter.getInstance();

assertThat(trxnAdapter.getRecordsCount()).isZero();
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);
assertThat(trxnAdapter.getRecordsCount()).isZero();
}

Expand All @@ -152,7 +152,7 @@ public void futureScheduledActions_shouldNotRun(){
TransactionsDbAdapter trxnAdapter = TransactionsDbAdapter.getInstance();

assertThat(trxnAdapter.getRecordsCount()).isZero();
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);
assertThat(trxnAdapter.getRecordsCount()).isZero();
}

Expand All @@ -166,15 +166,15 @@ public void exceededExecutionCounts_shouldNotRun(){
scheduledAction.setStartTime(new DateTime(2015, 5, 31, 14, 0).getMillis());
scheduledAction.setEnabled(true);
scheduledAction.setRecurrence(new Recurrence(PeriodType.WEEK));
scheduledAction.setTotalFrequency(4);
scheduledAction.setTotalPlannedExecutionCount(4);
scheduledAction.setExecutionCount(4);

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledAction);

TransactionsDbAdapter trxnAdapter = TransactionsDbAdapter.getInstance();
assertThat(trxnAdapter.getRecordsCount()).isZero();
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);
assertThat(trxnAdapter.getRecordsCount()).isZero();
}

Expand All @@ -196,7 +196,7 @@ public void missedScheduledTransactions_shouldBeGenerated(){

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledAction);
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);

int weeks = Weeks.weeksBetween(startTime, new DateTime(2016, 8, 29, 10, 0)).getWeeks();
int expectedTransactionCount = weeks/2;
Expand All @@ -219,7 +219,7 @@ public void endTimeInTheFuture_shouldExecuteOnlyUntilPresent(){

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledAction);
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);

int weeks = Weeks.weeksBetween(startTime, new DateTime(2016, 8, 29, 10, 0)).getWeeks();
int expectedTransactionCount = weeks/2; //multiplier from the PeriodType
Expand All @@ -246,7 +246,7 @@ public void scheduledActionsWithEndTimeInPast_shouldBeExecuted(){

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledAction);
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);

int expectedCount = 5;
assertThat(scheduledAction.getExecutionCount()).isEqualTo(expectedCount);
Expand All @@ -268,7 +268,7 @@ public void recurringTransactions_shouldHaveScheduledActionUID(){

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledAction);
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);
}

//// FIXME: 16.08.2016 Cannot find the file after export. But the export task is called and run
Expand All @@ -288,7 +288,7 @@ public void scheduledBackups_shouldRunOnlyOnce(){

List<ScheduledAction> actions = new ArrayList<>();
actions.add(scheduledBackup);
SchedulerService.processScheduledActions(actions, mDb);
ScheduledActionService.processScheduledActions(actions, mDb);

File[] backupFiles = backupFolder.listFiles();
assertThat(backupFiles).hasSize(1);
Expand Down

0 comments on commit e83dc93

Please sign in to comment.