Skip to content

Commit 00d262a

Browse files
committed
Merge branch 'null_refactoring' into hotfix/patches
2 parents 772e15f + afcac8e commit 00d262a

8 files changed

Lines changed: 116 additions & 117 deletions

File tree

app/src/main/java/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,11 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Account account
173173
mReplaceStatement.clearBindings();
174174
mReplaceStatement.bindString(1, account.getUID());
175175
mReplaceStatement.bindString(2, account.getName());
176-
if (account.getDescription() != null)
177-
mReplaceStatement.bindString(3, account.getDescription());
176+
mReplaceStatement.bindString(3, account.getDescription());
178177
mReplaceStatement.bindString(4, account.getAccountType().name());
179178
mReplaceStatement.bindString(5, account.getCurrency().getCurrencyCode());
180-
if (account.getColorHexCode() != null) {
181-
mReplaceStatement.bindString(6, account.getColorHexCode());
179+
if (account.getColor() != Account.DEFAULT_COLOR) {
180+
mReplaceStatement.bindString(6, convertToRGBHexString(account.getColor()));
182181
}
183182
mReplaceStatement.bindLong(7, account.isFavorite() ? 1 : 0);
184183
mReplaceStatement.bindString(8, account.getFullName());
@@ -206,6 +205,10 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Account account
206205
return mReplaceStatement;
207206
}
208207

208+
private String convertToRGBHexString(int color) {
209+
return String.format("#%06X", (0xFFFFFF & color));
210+
}
211+
209212
/**
210213
* Marks all transactions for a given account as exported
211214
* @param accountUID Unique ID of the record to be marked as exported
@@ -407,14 +410,17 @@ private Account buildSimpleAccountInstance(Cursor c) {
407410
Account account = new Account(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_NAME)));
408411
populateBaseModelAttributes(c, account);
409412

410-
account.setDescription(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_DESCRIPTION)));
413+
String description = c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_DESCRIPTION));
414+
account.setDescription(description == null ? "" : description);
411415
account.setParentUID(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_PARENT_ACCOUNT_UID)));
412416
account.setAccountType(AccountType.valueOf(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_TYPE))));
413417
Currency currency = Currency.getInstance(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_CURRENCY)));
414418
account.setCommodity(CommoditiesDbAdapter.getInstance().getCommodity(currency.getCurrencyCode()));
415419
account.setPlaceHolderFlag(c.getInt(c.getColumnIndexOrThrow(AccountEntry.COLUMN_PLACEHOLDER)) == 1);
416420
account.setDefaultTransferAccountUID(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_DEFAULT_TRANSFER_ACCOUNT_UID)));
417-
account.setColorCode(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_COLOR_CODE)));
421+
String color = c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_COLOR_CODE));
422+
if (color != null)
423+
account.setColor(color);
418424
account.setFavorite(c.getInt(c.getColumnIndexOrThrow(AccountEntry.COLUMN_FAVORITE)) == 1);
419425
account.setFullName(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_FULL_NAME)));
420426
account.setHidden(c.getInt(c.getColumnIndexOrThrow(AccountEntry.COLUMN_HIDDEN)) == 1);
@@ -561,7 +567,7 @@ public String getOrCreateImbalanceAccountUID(Currency currency){
561567
account.setAccountType(AccountType.BANK);
562568
account.setParentUID(getOrCreateGnuCashRootAccountUID());
563569
account.setHidden(!GnuCashApplication.isDoubleEntryEnabled());
564-
account.setColorCode("#964B00");
570+
account.setColor("#964B00");
565571
addRecord(account);
566572
uid = account.getUID();
567573
}
@@ -595,7 +601,7 @@ public String createAccountHierarchy(String fullName, AccountType accountType) {
595601
String[] tokens = fullName.trim().split(ACCOUNT_NAME_SEPARATOR);
596602
String uid = getOrCreateGnuCashRootAccountUID();
597603
String parentName = "";
598-
ArrayList<Account> accountsList = new ArrayList<Account>();
604+
ArrayList<Account> accountsList = new ArrayList<>();
599605
for (String token : tokens) {
600606
parentName += token;
601607
String parentUID = findAccountUidByFullName(parentName);
@@ -835,8 +841,8 @@ public Money getAccountsBalance(@NonNull List<String> accountUIDList, long start
835841
public List<String> getDescendantAccountUIDs(String accountUID, String where, String[] whereArgs) {
836842
// accountsList will hold accountUID with all descendant accounts.
837843
// accountsListLevel will hold descendant accounts of the same level
838-
ArrayList<String> accountsList = new ArrayList<String>();
839-
ArrayList<String> accountsListLevel = new ArrayList<String>();
844+
ArrayList<String> accountsList = new ArrayList<>();
845+
ArrayList<String> accountsListLevel = new ArrayList<>();
840846
accountsListLevel.add(accountUID);
841847
for (;;) {
842848
Cursor cursor = mDb.query(AccountEntry.TABLE_NAME,
@@ -1099,14 +1105,14 @@ public boolean isFavoriteAccount(String accountUID){
10991105
*/
11001106
public List<Transaction> getAllOpeningBalanceTransactions(){
11011107
Cursor cursor = fetchAccounts(null, null, null);
1102-
List<Transaction> openingTransactions = new ArrayList<Transaction>();
1108+
List<Transaction> openingTransactions = new ArrayList<>();
11031109
try {
11041110
SplitsDbAdapter splitsDbAdapter = mTransactionsAdapter.getSplitDbAdapter();
11051111
while (cursor.moveToNext()) {
11061112
long id = cursor.getLong(cursor.getColumnIndexOrThrow(AccountEntry._ID));
11071113
String accountUID = getUID(id);
11081114
String currencyCode = getCurrencyCode(accountUID);
1109-
ArrayList<String> accountList = new ArrayList<String>();
1115+
ArrayList<String> accountList = new ArrayList<>();
11101116
accountList.add(accountUID);
11111117
Money balance = splitsDbAdapter.computeSplitBalance(accountList,
11121118
currencyCode, getAccountType(accountUID).hasDebitNormalBalance());

app/src/main/java/org/gnucash/android/importer/GncXmlHandler.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ public class GncXmlHandler extends DefaultHandler {
7575
*/
7676
private static final String LOG_TAG = "GnuCashAccountImporter";
7777

78+
/*
79+
^ anchor for start of string
80+
# the literal #
81+
( start of group
82+
?: indicate a non-capturing group that doesn't generate back-references
83+
[0-9a-fA-F] hexadecimal digit
84+
{3} three times
85+
) end of group
86+
{2} repeat twice
87+
$ anchor for end of string
88+
*/
89+
/**
90+
* Regular expression for validating color code strings.
91+
* Accepts #rgb and #rrggbb
92+
*/
93+
//TODO: Allow use of #aarrggbb format as well
94+
public static final String ACCOUNT_COLOR_HEX_REGEX = "^#(?:[0-9a-fA-F]{3}){2}$";
95+
7896
/**
7997
* Adapter for saving the imported accounts
8098
*/
@@ -463,11 +481,11 @@ public void endElement(String uri, String localName, String qualifiedName) throw
463481
//so we trim the last digit in each block, doesn't affect the color much
464482
if (!color.equals("Not Set")) {
465483
// avoid known exception, printStackTrace is very time consuming
466-
if (!Pattern.matches(Account.COLOR_HEX_REGEX, color))
484+
if (!Pattern.matches(ACCOUNT_COLOR_HEX_REGEX, color))
467485
color = "#" + color.replaceAll(".(.)?", "$1").replace("null", "");
468486
try {
469487
if (mAccount != null)
470-
mAccount.setColorCode(color);
488+
mAccount.setColor(color);
471489
} catch (IllegalArgumentException ex) {
472490
//sometimes the color entry in the account file is "Not set" instead of just blank. So catch!
473491
Log.e(LOG_TAG, "Invalid color code '" + color + "' for account " + mAccount.getName());

app/src/main/java/org/gnucash/android/model/Account.java

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
package org.gnucash.android.model;
1818

1919

20-
import android.preference.PreferenceManager;
20+
import android.graphics.Color;
21+
import android.support.annotation.NonNull;
2122

2223
import org.gnucash.android.BuildConfig;
23-
import org.gnucash.android.app.GnuCashApplication;
24-
import org.gnucash.android.export.Exporter;
2524
import org.gnucash.android.export.ofx.OfxHelper;
2625
import org.w3c.dom.Document;
2726
import org.w3c.dom.Element;
@@ -30,7 +29,6 @@
3029
import java.util.ArrayList;
3130
import java.util.Currency;
3231
import java.util.List;
33-
import java.util.regex.Pattern;
3432

3533
/**
3634
* An account represents a transaction account in with {@link Transaction}s may be recorded
@@ -51,25 +49,13 @@ public class Account extends BaseModel{
5149
*/
5250
public static final String MIME_TYPE = "vnd.android.cursor.item/vnd." + BuildConfig.APPLICATION_ID + ".account";
5351

54-
/*
55-
^ anchor for start of string
56-
# the literal #
57-
( start of group
58-
?: indicate a non-capturing group that doesn't generate back-references
59-
[0-9a-fA-F] hexadecimal digit
60-
{3} three times
61-
) end of group
62-
{1,2} repeat either once or twice
63-
$ anchor for end of string
64-
*/
65-
/**
66-
* Regular expression for validating color code strings.
67-
* Accepts #rgb and #rrggbb
68-
*/
69-
//TODO: Allow use of #aarrggbb format as well
70-
public static final String COLOR_HEX_REGEX = "^#(?:[0-9a-fA-F]{3}){1,2}$";
52+
/**
53+
* Default color, if not set explicitly through {@link #setColor(String)}.
54+
*/
55+
// TODO: get it from a theme value?
56+
public static final int DEFAULT_COLOR = Color.LTGRAY;
7157

72-
/**
58+
/**
7359
* Accounts types which are used by the OFX standard
7460
*/
7561
public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
@@ -88,7 +74,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
8874
/**
8975
* Account description
9076
*/
91-
private String mDescription;
77+
private String mDescription = "";
9278

9379
/**
9480
* Currency used by transactions in this account
@@ -106,7 +92,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
10692
* Defaults to {@link AccountType#CASH}
10793
*/
10894
private AccountType mAccountType = AccountType.CASH;
109-
95+
11096
/**
11197
* List of transactions in this account
11298
*/
@@ -132,7 +118,7 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
132118
/**
133119
* Account color field in hex format #rrggbb
134120
*/
135-
private String mColorCode;
121+
private int mColor = DEFAULT_COLOR;
136122

137123
/**
138124
* Flag which marks this account as a favorite account
@@ -148,13 +134,13 @@ public enum OfxAccountType {CHECKING, SAVINGS, MONEYMRKT, CREDITLINE }
148134
* An extra key for passing the currency code (according ISO 4217) in an intent
149135
*/
150136
public static final String EXTRA_CURRENCY_CODE = "org.gnucash.android.extra.currency_code";
151-
137+
152138
/**
153-
* Extra key for passing the unique ID of the parent account when creating a
139+
* Extra key for passing the unique ID of the parent account when creating a
154140
* new account using Intents
155141
*/
156142
public static final String EXTRA_PARENT_UID = "org.gnucash.android.extra.parent_uid";
157-
143+
158144
/**
159145
* Constructor
160146
* Creates a new account with the default currency and a generated unique ID
@@ -165,7 +151,7 @@ public Account(String name) {
165151
this.mFullName = mName;
166152
setCommodity(Commodity.DEFAULT_COMMODITY);
167153
}
168-
154+
169155
/**
170156
* Overloaded constructor
171157
* @param name Name of the account
@@ -211,18 +197,18 @@ public void setFullName(String fullName) {
211197
}
212198

213199
/**
214-
* Returns the account mDescription
215-
* @return String with mDescription
200+
* Returns the account description
201+
* @return String with description
216202
*/
217203
public String getDescription() {
218204
return mDescription;
219205
}
220206

221207
/**
222-
* Sets the account mDescription
223-
* @param description String mDescription
208+
* Sets the account description
209+
* @param description Account description
224210
*/
225-
public void setDescription(String description) {
211+
public void setDescription(@NonNull String description) {
226212
this.mDescription = description;
227213
}
228214

@@ -251,11 +237,11 @@ public void addTransaction(Transaction transaction){
251237
transaction.setCommodity(mCommodity);
252238
mTransactionsList.add(transaction);
253239
}
254-
240+
255241
/**
256242
* Sets a list of transactions for this account.
257243
* Overrides any previous transactions with those in the list.
258-
* The account UID and currency of the transactions will be set to the unique ID
244+
* The account UID and currency of the transactions will be set to the unique ID
259245
* and currency of the account respectively
260246
* @param transactionsList List of {@link Transaction}s to be set.
261247
*/
@@ -270,7 +256,7 @@ public void setTransactions(List<Transaction> transactionsList){
270256
public List<Transaction> getTransactions(){
271257
return mTransactionsList;
272258
}
273-
259+
274260
/**
275261
* Returns the number of transactions in this account
276262
* @return Number transactions in account
@@ -293,26 +279,34 @@ public Money getBalance(){
293279
}
294280

295281
/**
296-
* Returns the color code of the account in the format #rrggbb
297-
* @return Color code of the account
282+
* Returns the color of the account.
283+
* @return Color of the account as an int as returned by {@link Color}.
298284
*/
299-
public String getColorHexCode() {
300-
return mColorCode;
285+
public int getColor() {
286+
return mColor;
301287
}
302288

289+
/**
290+
* Sets the color of the account.
291+
* @param color Color as an int as returned by {@link Color}.
292+
* @throws java.lang.IllegalArgumentException if the color is transparent,
293+
* which is not supported.
294+
*/
295+
public void setColor(int color) {
296+
if (Color.alpha(color) < 255)
297+
throw new IllegalArgumentException("Transparent colors are not supported: " + color);
298+
mColor = color;
299+
}
300+
303301
/**
304-
* Sets the color code of the account.
305-
* @param colorCode Color code to be set in the format #rrggbb or #rgb
306-
* @throws java.lang.IllegalArgumentException if the color code is not properly formatted
302+
* Sets the color of the account.
303+
* @param colorCode Color code to be set in the format #rrggbb
304+
* @throws java.lang.IllegalArgumentException if the color code is not properly formatted or
305+
* the color is transparent.
307306
*/
308-
public void setColorCode(String colorCode) {
309-
if (colorCode == null)
310-
return;
311-
312-
if (!Pattern.matches(COLOR_HEX_REGEX, colorCode))
313-
throw new IllegalArgumentException("Invalid color hex code: " + colorCode);
314-
315-
this.mColorCode = colorCode;
307+
//TODO: Allow use of #aarrggbb format as well
308+
public void setColor(@NonNull String colorCode) {
309+
setColor(Color.parseColor(colorCode));
316310
}
317311

318312
/**
@@ -332,7 +326,7 @@ public void setFavorite(boolean isFavorite) {
332326
}
333327

334328
/**
335-
* @return the mCurrency
329+
* Returns the currency for this account.
336330
*/
337331
public Currency getCurrency() {
338332
return Currency.getInstance(mCurrencyCode);
@@ -348,7 +342,6 @@ public void setCurrencyCode(String currencyCode){
348342

349343
/**
350344
* Return the commodity for this account
351-
* @return
352345
*/
353346
public Commodity getCommodity(){
354347
return mCommodity;

0 commit comments

Comments
 (0)