Skip to content

Commit

Permalink
Fixed parsing of recurrence events into ScheduledEvents
Browse files Browse the repository at this point in the history
Updated strings.xml and deleted pom files (which were restored after a rebase with develop)
Updated README to explain new build system

TODO: Create service for running the events
  • Loading branch information
codinguser committed Feb 14, 2015
1 parent f534b82 commit 0e8777d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 333 deletions.
36 changes: 14 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,22 @@ There are different ways to get the Gnucash app for Android; through the app sto

### With Gradle

Run `gradlew build installDebug` from the within the project folder.
This project requires the [Android SDK](http://developer.android.com/sdk/index.html)
to be installed in your development environment. In addition you'll need to set
the `ANDROID_HOME` environment variable to the location of your SDK. For example:

export ANDROID_HOME=/home/<user>/tools/android-sdk

After satisfying those requirements, the build is pretty simple:

* Run `gradlew build installDebug` from the within the project folder.
It will build the project for you and install it to the connected Android device or running emulator.

You might find that your device doesn't let you install your build if you
already have the version from the Android Market installed. This is standard
Android security as it it won't let you directly replace an app that's been
signed with a different key. Manually uninstall GnuCash from your device and
you will then be able to install your own built version.

### With Android Studio
The easiest way to build is to install [Android Studio](https://developer.android.com/sdk/index.html) v1.+
Expand All @@ -38,27 +51,6 @@ Once installed, then you can import the project into Android Studio:

Then, Gradle will do everything for you.

### With Maven
The build requires [Maven](http://maven.apache.org/download.html)
v3.1.1+ and the [Android SDK](http://developer.android.com/sdk/index.html)
to be installed in your development environment. In addition you'll need to set
the `ANDROID_HOME` environment variable to the location of your SDK:

export ANDROID_HOME=/home/<user>/tools/android-sdk

After satisfying those requirements, the build is pretty simple:

* Run `mvn clean package` from the `app` directory to build the APK only
* Run `mvn clean install` from the root directory to build the app and also run
the integration tests, this requires a connected Android device or running
emulator. (see this [blog post](http://goo.gl/TprMw) for details)

You might find that your device doesn't let you install your build if you
already have the version from the Android Market installed. This is standard
Android security as it it won't let you directly replace an app that's been
signed with a different key. Manually uninstall GnuCash from your device and
you will then be able to install your own built version.

## Contributing

There are several ways you could contribute to the development.
Expand Down
83 changes: 0 additions & 83 deletions app/pom.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public enum EventType {TRANSACTION};
public ScheduledEvent(EventType eventType){
mUID = UUID.randomUUID().toString().replaceAll("-", "");
mEventType = eventType;
mStartDate = System.currentTimeMillis();
}

public String getUID(){
Expand Down Expand Up @@ -123,7 +124,12 @@ public void setEndTime(long endDate) {
@Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
return mEventType.name() + " recurring every " + mPeriod/ RecurrenceParser.DAY_MILLIS + " days starting on "
String eventString = mEventType.name() + " recurring every " + mPeriod/ RecurrenceParser.DAY_MILLIS + " days starting on "
+ dateFormat.format(new Date(mStartDate));
if (mEndDate > 0){
eventString += " until " + dateFormat.format(mEndDate);
}

return eventString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,10 @@ private void scheduleRecurringTransaction() {
ScheduledEventDbAdapter scheduledEventDbAdapter = GnuCashApplication.getScheduledEventDbAdapter();
Toast.makeText(getActivity(), "Found " + events.size() + " events", Toast.LENGTH_LONG).show();
for (ScheduledEvent event : events) {
scheduledEventDbAdapter.addScheduledEvent(event);
event.setEventUID(recurringTransaction.getUID());
scheduledEventDbAdapter.addScheduledEvent(event);

Log.d("TransactionFormFragment", event.toString());
Log.i("TransactionFormFragment", event.toString());
}
}

Expand Down
107 changes: 79 additions & 28 deletions app/src/main/java/org/gnucash/android/ui/util/RecurrenceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RecurrenceParser {
public static final long MINUTE_MILLIS = 60*SECOND_MILLIS;
public static final long DAY_MILLIS = 24*60*MINUTE_MILLIS;
public static final long WEEK_MILLIS = 7*DAY_MILLIS;
public static final long MONTH_MILLIS = 4*WEEK_MILLIS;
public static final long MONTH_MILLIS = 30*DAY_MILLIS;
public static final long YEAR_MILLIS = 12*MONTH_MILLIS;


Expand All @@ -51,67 +51,90 @@ public static List<ScheduledEvent> parse(EventRecurrence eventRecurrence, Schedu
long period = 0;
List<ScheduledEvent> scheduledEventList = new ArrayList<ScheduledEvent>();
switch(eventRecurrence.freq){
case EventRecurrence.DAILY:
period = DAY_MILLIS;
case EventRecurrence.DAILY: {
if (eventRecurrence.interval == 0) //I assume this is a bug from the picker library
period = DAY_MILLIS;
else
period = eventRecurrence.interval * DAY_MILLIS;

ScheduledEvent scheduledEvent = new ScheduledEvent(eventType);
scheduledEvent.setPeriod(period);
parseEndTime(eventRecurrence, scheduledEvent);
scheduledEventList.add(scheduledEvent);
}
break;

case EventRecurrence.WEEKLY: {
period = WEEK_MILLIS;
if (eventRecurrence.interval == 0)
period = WEEK_MILLIS;
else
period = eventRecurrence.interval * WEEK_MILLIS;
for (int day : eventRecurrence.byday) {
ScheduledEvent scheduledEvent = new ScheduledEvent(eventType);
scheduledEvent.setPeriod(period);

scheduledEvent.setStartTime(nextDayOfWeek(day).getTimeInMillis());
if (eventRecurrence.until != null && eventRecurrence.until.length() > 0) {
Time endTime = new Time();
endTime.parse(eventRecurrence.until);
scheduledEvent.setEndTime(endTime.toMillis(false));
} else if (eventRecurrence.count > 0){
scheduledEvent.setEndTime(scheduledEvent.getStartTime() + (scheduledEvent.getPeriod() * eventRecurrence.count));
}
scheduledEvent.setStartTime(nextDayOfWeek(day2CalendarDay(day)).getTimeInMillis());
parseEndTime(eventRecurrence, scheduledEvent);
scheduledEventList.add(scheduledEvent);
}
}
break;

case EventRecurrence.MONTHLY: {
if (eventRecurrence.interval == 0)
period = MONTH_MILLIS;
else
period = eventRecurrence.interval * MONTH_MILLIS;
ScheduledEvent event = new ScheduledEvent(eventType);
event.setPeriod(MONTH_MILLIS);
event.setPeriod(period);
Calendar now = Calendar.getInstance();
now.add(Calendar.MONTH, 1);
event.setStartTime(now.getTimeInMillis());
if (eventRecurrence.until != null && eventRecurrence.until.length() > 0) {
Time endTime = new Time();
endTime.parse(eventRecurrence.until);
event.setEndTime(endTime.toMillis(false));
} else if (eventRecurrence.count > 0){
event.setEndTime(event.getStartTime() + (event.getPeriod()*eventRecurrence.count));
}
parseEndTime(eventRecurrence, event);

scheduledEventList.add(event);
}
break;

case EventRecurrence.YEARLY: {
if (eventRecurrence.interval == 0)
period = YEAR_MILLIS;
else
period = eventRecurrence.interval * YEAR_MILLIS;
ScheduledEvent event = new ScheduledEvent(eventType);
event.setPeriod(YEAR_MILLIS);
event.setPeriod(period);
Calendar now = Calendar.getInstance();
now.add(Calendar.YEAR, 1);
event.setStartTime(now.getTimeInMillis());
if (eventRecurrence.until != null && eventRecurrence.until.length() > 0) {
Time endTime = new Time();
endTime.parse(eventRecurrence.until);
event.setEndTime(endTime.toMillis(false));
} else if (eventRecurrence.count > 0){
event.setEndTime(event.getStartTime() + (event.getPeriod()*eventRecurrence.count));
}
parseEndTime(eventRecurrence, event);
scheduledEventList.add(event);
}
break;
}
return scheduledEventList;
}

/**
* Parses the end time from an EventRecurrence object and sets it to the <code>scheduledEvent</code>.
* The end time is specified in the dialog either by number of occurences or a date.
* @param eventRecurrence Event recurrence pattern obtained from dialog
* @param scheduledEvent ScheduledEvent to be to updated
*/
private static void parseEndTime(EventRecurrence eventRecurrence, ScheduledEvent scheduledEvent) {
if (eventRecurrence.until != null && eventRecurrence.until.length() > 0) {
Time endTime = new Time();
endTime.parse(eventRecurrence.until);
scheduledEvent.setEndTime(endTime.toMillis(false));
} else if (eventRecurrence.count > 0){
scheduledEvent.setEndTime(scheduledEvent.getStartTime() + (scheduledEvent.getPeriod() * eventRecurrence.count));
}
}

/**
* Returns the date for the next day of the week
* @param dow Day of the week (Calendar constants)
* @return Calendar instance with the next day of the week
*/
private static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
Expand All @@ -121,4 +144,32 @@ private static Calendar nextDayOfWeek(int dow) {
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}

/**
* Converts one of the SU, MO, etc. constants to the Calendar.SUNDAY
* constants. btw, I think we should switch to those here too, to
* get rid of this function, if possible.
*/
public static int day2CalendarDay(int day)
{
switch (day)
{
case EventRecurrence.SU:
return Calendar.SUNDAY;
case EventRecurrence.MO:
return Calendar.MONDAY;
case EventRecurrence.TU:
return Calendar.TUESDAY;
case EventRecurrence.WE:
return Calendar.WEDNESDAY;
case EventRecurrence.TH:
return Calendar.THURSDAY;
case EventRecurrence.FR:
return Calendar.FRIDAY;
case EventRecurrence.SA:
return Calendar.SATURDAY;
default:
throw new RuntimeException("bad day of week: " + day);
}
}
}
1 change: 0 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">GnuCash</string>
<string name="app_version_name">1.5.4</string>
<string name="title_add_account">Create Account</string>
<string name="title_edit_account">Edit Account</string>
<string name="info_details">Info</string>
Expand Down
Loading

0 comments on commit 0e8777d

Please sign in to comment.