Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
codinguser committed Nov 20, 2015
2 parents bbe3e2d + 7dcb254 commit dfd54e3
Show file tree
Hide file tree
Showing 80 changed files with 1,200 additions and 6,820 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
Change Log
===============================================================================
Version 2.0.2 *(2015-11-20)*
----------------------------
* Fixed: Exporting to external service does not work in some devices
* Fixed: Bar chart does not display negative amounts
* Fixed: Crash when saving transaction with invalid amount expression
* Fixed: Crash when displaying bar chart legend with accounts of same name
* Fixed: Crashes when importing some GnuCash XML files on some devices
* Improved: Remember last export destination
* Improved: Display current imbalance in split editor
* Improved: Set default commodity to the one used by imported file
* Improved: Add support for unlimited fractional digits in commodities
* Improved: Option to select date from which to export transactions

Version 2.0.1 *(2015-11-05)*
----------------------------
* Feature: Menu options for moving/duplicating transactions
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ apply plugin: 'io.fabric'

def versionMajor = 2
def versionMinor = 0
def versionPatch = 1
def versionBuild = 3
def versionPatch = 2
def versionBuild = 2

def buildTime() {
def df = new SimpleDateFormat("yyyyMMdd HH:mm 'UTC'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
Expand All @@ -69,6 +70,8 @@
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Expand Down Expand Up @@ -192,6 +195,11 @@ public void testExport(ExportFormat format){

DrawerActions.openDrawer(R.id.drawer_layout);
onView(withText(R.string.nav_menu_export)).perform(click());

onView(withId(R.id.spinner_export_destination)).perform(click());
String[] destinations = getActivity().getResources().getStringArray(R.array.export_destinations);

onView(withText(destinations[0])).perform(click());
onView(withText(format.name())).perform(click());

onView(withId(R.id.menu_save)).perform(click());
Expand Down Expand Up @@ -243,6 +251,26 @@ public void testShouldCreateExportSchedule(){
assertThat(action.getEndTime()).isEqualTo(0);
}

@Test
public void testCreateBackup(){
DrawerActions.openDrawer(R.id.drawer_layout);
onView(withText(R.string.title_settings)).perform(click());
onView(withText(R.string.header_backup_and_export_settings)).perform(click());

onView(withText(R.string.title_create_backup_pref)).perform(click());
assertToastDisplayed(R.string.toast_backup_successful);
}

/**
* Checks that a specific toast message is displayed
* @param toastString String that should be displayed
*/
private void assertToastDisplayed(int toastString) {
onView(withText(toastString))
.inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
}

//todo: add testing of export flag to unit test
//todo: add test of ignore exported transactions to unit tests
@Override
Expand Down
21 changes: 16 additions & 5 deletions app/src/main/java/org/gnucash/android/app/GnuCashApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
Expand All @@ -41,6 +42,7 @@
import org.gnucash.android.db.SplitsDbAdapter;
import org.gnucash.android.db.TransactionsDbAdapter;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.service.SchedulerService;

import java.util.Currency;
Expand Down Expand Up @@ -124,7 +126,7 @@ public void onCreate(){
mCommoditiesDbAdapter = new CommoditiesDbAdapter(mDb);
mPricesDbAdapter = new PricesDbAdapter(mDb);

Commodity.DEFAULT_COMMODITY = mCommoditiesDbAdapter.getCommodity(getDefaultCurrencyCode());
setDefaultCurrencyCode(getDefaultCurrencyCode());
}

public static AccountsDbAdapter getAccountsDbAdapter() {
Expand Down Expand Up @@ -216,12 +218,21 @@ public static String getDefaultCurrencyCode(){
}

/**
* Returns the default commodity
* @return Default commodity of application
* Sets the default currency for the application in all relevant places:
* <ul>
* <li>Shared preferences</li>
* <li>{@link Money#DEFAULT_CURRENCY_CODE}</li>
* <li>{@link Commodity#DEFAULT_COMMODITY}</li>
* </ul>
* @param currencyCode ISO 4217 currency code
* @see #getDefaultCurrencyCode()
*/
public static Commodity getDefaultCommodity(){
return Commodity.DEFAULT_COMMODITY;
public static void setDefaultCurrencyCode(@NonNull String currencyCode){
PreferenceManager.getDefaultSharedPreferences(getAppContext()).edit()
.putString(getAppContext().getString(R.string.key_default_currency), currencyCode)
.apply();
Money.DEFAULT_CURRENCY_CODE = currencyCode;
Commodity.DEFAULT_COMMODITY = mCommoditiesDbAdapter.getCommodity(currencyCode);
}

/**
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.gnucash.android.model.TransactionType;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Currency;
import java.util.HashMap;
Expand Down Expand Up @@ -503,12 +504,11 @@ public List<Account> getSimpleAccountList(String where, String[] whereArgs, Stri
}
/**
* Returns a list of accounts which have transactions that have not been exported yet
* @param lastExportTimeStamp Timestamp after which to any transactions created/modified should be exported
* @return List of {@link Account}s with unexported transactions
* @deprecated This uses the exported flag in the database which is no longer supported.
*/
@Deprecated
public List<Account> getExportableAccounts(){
LinkedList<Account> accountsList = new LinkedList<Account>();
public List<Account> getExportableAccounts(Timestamp lastExportTimeStamp){
LinkedList<Account> accountsList = new LinkedList<>();
Cursor cursor = mDb.query(
TransactionEntry.TABLE_NAME + " , " + SplitEntry.TABLE_NAME +
" ON " + TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_UID + " = " +
Expand All @@ -517,8 +517,8 @@ public List<Account> getExportableAccounts(){
AccountEntry.COLUMN_UID + " = " + SplitEntry.TABLE_NAME + "." +
SplitEntry.COLUMN_ACCOUNT_UID,
new String[]{AccountEntry.TABLE_NAME + ".*"},
TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_EXPORTED + " == 0",
null,
TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_MODIFIED_AT + " > ?",
new String[]{lastExportTimeStamp.toString()},
AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID,
null,
null
Expand Down Expand Up @@ -787,23 +787,24 @@ private Money computeBalance(String accountUID, long startTimestamp, long endTim
}

/**
* Returns the absolute balance of account list within the specified time range while taking sub-accounts
* into consideration. The default currency takes as base currency.
* Returns the balance of account list within the specified time range. The default currency
* takes as base currency.
* @param accountUIDList list of account UIDs
* @param startTimestamp the start timestamp of the time range
* @param endTimestamp the end timestamp of the time range
* @return the absolute balance of account list
* @return Money balance of account list
*/
public Money getAccountsBalance(List<String> accountUIDList, long startTimestamp, long endTimestamp) {
String currencyCode = GnuCashApplication.getDefaultCurrencyCode();
Money balance = Money.createZeroInstance(currencyCode);
boolean hasDebitNormalBalance = getAccountType(accountUIDList.get(0)).hasDebitNormalBalance();

SplitsDbAdapter splitsDbAdapter = SplitsDbAdapter.getInstance();
Money splitSum = (startTimestamp == -1 && endTimestamp == -1)
? splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, true)
: splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, true, startTimestamp, endTimestamp);
? splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance)
: splitsDbAdapter.computeSplitBalance(accountUIDList, currencyCode, hasDebitNormalBalance, startTimestamp, endTimestamp);

return balance.add(splitSum).absolute();
return balance.add(splitSum);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/gnucash/android/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
/*
* NOTE: In order to modify the database, create a new static method in the MigrationHelper class
* called upgradeDbToVersion<#>, e.g. int upgradeDbToVersion10(SQLiteDatabase) in order to upgrade to version 10.
* The upgrade method should return the current database version as the return value.
* Then all you need to do is incremend the DatabaseSchema.DATABASE_VERSION to the appropriate number.
* The upgrade method should return the upgraded database version as the return value.
* Then all you need to do is increment the DatabaseSchema.DATABASE_VERSION to the appropriate number.
*/
if (oldVersion > newVersion) {
throw new IllegalArgumentException("Database downgrades are not supported at the moment");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DatabaseSchema {
* Database version.
* With any change to the database schema, this number must increase
*/
public static final int DATABASE_VERSION = 9;
public static final int DATABASE_VERSION = 11;

/**
* Database version where Splits were introduced
Expand Down
112 changes: 112 additions & 0 deletions app/src/main/java/org/gnucash/android/db/MigrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.crashlytics.android.Crashlytics;

import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.export.ExportFormat;
import org.gnucash.android.export.ExportParams;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.importer.CommoditiesXmlHandler;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.BaseModel;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Transaction;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand All @@ -52,7 +56,9 @@
import java.nio.channels.FileChannel;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
Expand Down Expand Up @@ -1058,4 +1064,110 @@ static int upgradeDbToVersion9(SQLiteDatabase db){
}
return oldVersion;
}

/**
* Upgrades the database to version 10
* <p>This method converts all saved scheduled export parameters to the new format using the
* timestamp of last export</p>
* @param db SQLite database
* @return 10 if upgrade was successful, 9 otherwise
*/
static int upgradeDbToVersion10(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 9");
int oldVersion = 9;

db.beginTransaction();
try {
Cursor cursor = db.query(ScheduledActionEntry.TABLE_NAME,
new String[]{ScheduledActionEntry.COLUMN_UID, ScheduledActionEntry.COLUMN_TAG},
ScheduledActionEntry.COLUMN_TYPE + " = ?",
new String[]{ScheduledAction.ActionType.BACKUP.name()},
null, null, null);

ContentValues contentValues = new ContentValues();
while (cursor.moveToNext()){
String paramString = cursor.getString(cursor.getColumnIndexOrThrow(ScheduledActionEntry.COLUMN_TAG));
String[] tokens = paramString.split(";");
ExportParams params = new ExportParams(ExportFormat.valueOf(tokens[0]));
params.setExportTarget(ExportParams.ExportTarget.valueOf(tokens[1]));
params.setDeleteTransactionsAfterExport(Boolean.parseBoolean(tokens[3]));

boolean exportAll = Boolean.parseBoolean(tokens[2]);
if (exportAll){
params.setExportStartTime(Timestamp.valueOf(Exporter.TIMESTAMP_ZERO));
} else {
String lastExportTimeStamp = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext())
.getString(Exporter.PREF_LAST_EXPORT_TIME, Exporter.TIMESTAMP_ZERO);
Timestamp timestamp = Timestamp.valueOf(lastExportTimeStamp);
params.setExportStartTime(timestamp);
}

String uid = cursor.getString(cursor.getColumnIndexOrThrow(ScheduledActionEntry.COLUMN_UID));
contentValues.clear();
contentValues.put(ScheduledActionEntry.COLUMN_UID, uid);
contentValues.put(ScheduledActionEntry.COLUMN_TAG, params.toCsv());
db.insert(ScheduledActionEntry.TABLE_NAME, null, contentValues);
}

cursor.close();

db.setTransactionSuccessful();
oldVersion = 10;
} finally {
db.endTransaction();
}
return oldVersion;
}

/**
* Upgrade database to version 11
* <p>
* Migrate scheduled backups and update export parameters to the new format
* </p>
* @param db SQLite database
* @return 11 if upgrade was successful, 10 otherwise
*/
static int upgradeDbToVersion11(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 9");
int oldVersion = 10;

db.beginTransaction();
try {
Cursor cursor = db.query(ScheduledActionEntry.TABLE_NAME, null,
ScheduledActionEntry.COLUMN_TYPE + "= ?",
new String[]{ScheduledAction.ActionType.BACKUP.name()}, null, null, null);

Map<String, String> uidToTagMap = new HashMap<>();
while (cursor.moveToNext()) {
String uid = cursor.getString(cursor.getColumnIndexOrThrow(ScheduledActionEntry.COLUMN_UID));
String tag = cursor.getString(cursor.getColumnIndexOrThrow(ScheduledActionEntry.COLUMN_TAG));
String[] tokens = tag.split(";");
try {
Timestamp timestamp = Timestamp.valueOf(tokens[2]);
} catch (IllegalArgumentException ex) {
tokens[2] = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext())
.getString(Exporter.PREF_LAST_EXPORT_TIME, Exporter.TIMESTAMP_ZERO);
} finally {
tag = TextUtils.join(";", tokens);
}
uidToTagMap.put(uid, tag);
}

cursor.close();

ContentValues contentValues = new ContentValues();
for (Map.Entry<String, String> entry : uidToTagMap.entrySet()) {
contentValues.clear();
contentValues.put(ScheduledActionEntry.COLUMN_TAG, entry.getValue());
db.update(ScheduledActionEntry.TABLE_NAME, contentValues,
ScheduledActionEntry.COLUMN_UID + " = ?", new String[]{entry.getKey()});
}

db.setTransactionSuccessful();
oldVersion = 11;
} finally {
db.endTransaction();
}
return oldVersion;
}
}
Loading

0 comments on commit dfd54e3

Please sign in to comment.