Skip to content

Commit 01a2301

Browse files
committed
Fixed: crash when devices report locale as es_LG which is unsupported
Fixed: crash when files in backup folder have no timestamp
1 parent baa094c commit 01a2301

7 files changed

Lines changed: 31 additions & 6 deletions

File tree

app/src/androidTest/java/org/gnucash/android/test/ui/TransactionsActivityTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,26 @@ public void testAddTransactionShouldRequireAmount(){
182182
.perform(typeText("Lunch"));
183183

184184
onView(withId(R.id.menu_save)).perform(click());
185-
185+
sleep(500);
186186
assertToastDisplayed(R.string.toast_transanction_amount_required);
187187

188188
int afterCount = mTransactionsDbAdapter.getTransactionsCount(DUMMY_ACCOUNT_UID);
189189
assertThat(afterCount).isEqualTo(beforeCount);
190190

191191
}
192192

193+
/**
194+
* Sleep the thread for a specified period
195+
* @param millis Duration to sleep in milliseconds
196+
*/
197+
private void sleep(long millis) {
198+
try {
199+
Thread.sleep(millis);
200+
} catch (InterruptedException e) {
201+
e.printStackTrace();
202+
}
203+
}
204+
193205
/**
194206
* Checks that a specific toast message is displayed
195207
* @param toastString

app/src/main/java/org/gnucash/android/app/GnuCashApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,18 @@ public static boolean shouldSaveOpeningBalances(boolean defaultValue){
158158
*
159159
* @return Default currency code string for the application
160160
*/
161-
public static String getDefaultCurrency(){
161+
public static String getDefaultCurrencyCode(){
162162
Locale locale = Locale.getDefault();
163163
//sometimes the locale en_UK is returned which causes a crash with Currency
164164
if (locale.getCountry().equals("UK")) {
165165
locale = new Locale(locale.getLanguage(), "GB");
166166
}
167167

168+
//for unsupported locale es_LG
169+
if (locale.getCountry().equals("LG")){
170+
locale = new Locale(locale.getLanguage(), "ES");
171+
}
172+
168173
String currencyCode = "USD"; //start with USD as the default
169174
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
170175
try { //there are some strange locales out there

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ private Money computeBalance(String accountUID, long startTimestamp, long endTim
813813
* @return the absolute balance of account list
814814
*/
815815
public Money getAccountsBalance(List<String> accountUIDList, long startTimestamp, long endTimestamp) {
816-
String currencyCode = GnuCashApplication.getDefaultCurrency();
816+
String currencyCode = GnuCashApplication.getDefaultCurrencyCode();
817817
Money balance = Money.createZeroInstance(currencyCode);
818818

819819
SplitsDbAdapter splitsDbAdapter = SplitsDbAdapter.getInstance();

app/src/main/java/org/gnucash/android/export/Exporter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public static String buildExportFilename(ExportFormat format) {
118118
public static long getExportTime(String filename){
119119
String[] tokens = filename.split("_");
120120
long timeMillis = 0;
121+
if (tokens.length < 2){
122+
return timeMillis;
123+
}
121124
try {
122125
Date date = EXPORT_FILENAME_DATE_FORMAT.parse(tokens[0] + "_" + tokens[1]);
123126
timeMillis = date.getTime();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.crashlytics.android.Crashlytics;
2323

24+
import org.gnucash.android.app.GnuCashApplication;
25+
2426
import java.math.BigDecimal;
2527
import java.math.MathContext;
2628
import java.math.RoundingMode;
@@ -90,7 +92,7 @@ public final class Money implements Comparable<Money>{
9092
* A zero instance with the currency of the default locale.
9193
* This can be used anywhere where a starting amount is required without having to create a new object
9294
*/
93-
private static final Money sDefaultZero = Money.createZeroInstance(Currency.getInstance(Locale.getDefault()).getCurrencyCode());
95+
private static final Money sDefaultZero = Money.createZeroInstance(GnuCashApplication.getDefaultCurrencyCode());
9496

9597
/**
9698
* Returns a Money instance initialized to the local currency and value 0

app/src/main/java/org/gnucash/android/ui/account/AccountsActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void setTab(int index){
309309
private void init() {
310310
PreferenceManager.setDefaultValues(this, R.xml.fragment_transaction_preferences, false);
311311

312-
Money.DEFAULT_CURRENCY_CODE = GnuCashApplication.getDefaultCurrency();
312+
Money.DEFAULT_CURRENCY_CODE = GnuCashApplication.getDefaultCurrencyCode();
313313

314314
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
315315
boolean firstRun = prefs.getBoolean(getString(R.string.key_first_run), true);

app/src/main/java/org/gnucash/android/ui/settings/SettingsActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ public void restoreBackup() {
538538
final DateFormat dateFormatter = SimpleDateFormat.getDateTimeInstance();
539539
for (File backupFile : sortedBackupFiles) {
540540
long time = Exporter.getExportTime(backupFile.getName());
541-
arrayAdapter.add(dateFormatter.format(new Date(time)));
541+
if (time > 0)
542+
arrayAdapter.add(dateFormatter.format(new Date(time)));
543+
else //if no timestamp was found in the filename, just use the name
544+
arrayAdapter.add(backupFile.getName());
542545
}
543546

544547
AlertDialog.Builder restoreDialogBuilder = new AlertDialog.Builder(this);

0 commit comments

Comments
 (0)