diff --git a/CHANGELOG.md b/CHANGELOG.md index 44d39ce21..97d30e73f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,11 @@ Change Log =============================================================================== -Version 2.4.0 *(2018-06-15)* +Version 2.4.1 *(2019-11-30)* ---------------------------- * Fixes #809: Crash when exporting CSV * Fixes #811: Add button in schedule action disappears after rotation * Fixes #790: Missing transaction description margin +* Fixes crash if during database upgrade triggered by scheduled action * Improve performance of CSV export * Improved: Add book name to accounts listing view * Improved: Update translations diff --git a/app/build.gradle b/app/build.gradle index 433fd721b..ee6d05082 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ apply plugin: 'io.fabric' def versionMajor = 2 def versionMinor = 4 def versionPatch = 1 -def versionBuild = 1 +def versionBuild = 2 static def buildTime() { def df = new SimpleDateFormat("yyyyMMdd HH:mm 'UTC'") diff --git a/app/src/main/java/org/gnucash/android/db/DatabaseHelper.java b/app/src/main/java/org/gnucash/android/db/DatabaseHelper.java index b8ef6c41f..9d4762ef3 100644 --- a/app/src/main/java/org/gnucash/android/db/DatabaseHelper.java +++ b/app/src/main/java/org/gnucash/android/db/DatabaseHelper.java @@ -20,11 +20,9 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; -import android.widget.Toast; import com.crashlytics.android.Crashlytics; -import org.gnucash.android.app.GnuCashApplication; import org.gnucash.android.model.Commodity; import org.xml.sax.SAXException; @@ -264,7 +262,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ Log.i(LOG_TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); - Toast.makeText(GnuCashApplication.getAppContext(), "Upgrading GnuCash database", Toast.LENGTH_SHORT).show(); + //TODO: Find way to show a progress dialog for long running db upgrades /* * 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. @@ -354,7 +352,7 @@ private void createDatabaseTables(SQLiteDatabase db) { db.execSQL(createBudgetAmountUidIndex); try { - MigrationHelper.importCommodities(db); + MigrationHelper.importCommodities(db, true); } catch (SAXException | ParserConfigurationException | IOException e) { Log.e(LOG_TAG, "Error loading currencies into the database"); e.printStackTrace(); diff --git a/app/src/main/java/org/gnucash/android/db/MigrationHelper.java b/app/src/main/java/org/gnucash/android/db/MigrationHelper.java index a5717671b..dbad1acb9 100644 --- a/app/src/main/java/org/gnucash/android/db/MigrationHelper.java +++ b/app/src/main/java/org/gnucash/android/db/MigrationHelper.java @@ -235,7 +235,7 @@ public void run() { /** * Imports commodities into the database from XML resource file */ - static void importCommodities(SQLiteDatabase db) throws SAXException, ParserConfigurationException, IOException { + static void importCommodities(SQLiteDatabase db, boolean deleteExisting) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); @@ -246,7 +246,7 @@ static void importCommodities(SQLiteDatabase db) throws SAXException, ParserConf /** Create handler to handle XML Tags ( extends DefaultHandler ) */ - CommoditiesXmlHandler handler = new CommoditiesXmlHandler(db); + CommoditiesXmlHandler handler = new CommoditiesXmlHandler(db, deleteExisting); xr.setContentHandler(handler); xr.parse(new InputSource(bos)); @@ -883,7 +883,7 @@ static int upgradeDbToVersion9(SQLiteDatabase db){ + "' ON " + CommodityEntry.TABLE_NAME + "(" + CommodityEntry.COLUMN_UID + ")"); try { - importCommodities(db); + importCommodities(db, true); } catch (SAXException | ParserConfigurationException | IOException e) { Log.e(LOG_TAG, "Error loading currencies into the database", e); Crashlytics.logException(e); @@ -1633,7 +1633,7 @@ static int upgradeDbToVersion15(SQLiteDatabase db) { * Upgrades the database to version 16. *

This migration makes the following changes to the database: *

*

* @param db SQLite database to be upgraded @@ -1644,7 +1644,7 @@ static int upgradeDbToVersion16(SQLiteDatabase db) { int dbVersion = 15; try { - importCommodities(db); + importCommodities(db, false); dbVersion = 16; } catch (SAXException | ParserConfigurationException | IOException e) { Log.e(LOG_TAG, "Error loading currencies into the database", e); diff --git a/app/src/main/java/org/gnucash/android/importer/CommoditiesXmlHandler.java b/app/src/main/java/org/gnucash/android/importer/CommoditiesXmlHandler.java index afefcc343..0cc9b84b4 100644 --- a/app/src/main/java/org/gnucash/android/importer/CommoditiesXmlHandler.java +++ b/app/src/main/java/org/gnucash/android/importer/CommoditiesXmlHandler.java @@ -15,9 +15,11 @@ */ package org.gnucash.android.importer; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import org.gnucash.android.app.GnuCashApplication; +import org.gnucash.android.db.DatabaseSchema; import org.gnucash.android.db.adapter.CommoditiesDbAdapter; import org.gnucash.android.db.adapter.DatabaseAdapter; import org.gnucash.android.model.Commodity; @@ -44,17 +46,23 @@ public class CommoditiesXmlHandler extends DefaultHandler { * List of commodities parsed from the XML file. * They will be all added to db at once at the end of the document */ - private List mCommodities; + private List mCommodities = new ArrayList<>(); + + private boolean deleteExisting; private CommoditiesDbAdapter mCommoditiesDbAdapter; - public CommoditiesXmlHandler(SQLiteDatabase db){ + public CommoditiesXmlHandler(SQLiteDatabase db, boolean deleteExisting){ + initAdapter(db); + this.deleteExisting = deleteExisting; + } + + private void initAdapter(SQLiteDatabase db) { if (db == null){ mCommoditiesDbAdapter = GnuCashApplication.getCommoditiesDbAdapter(); } else { mCommoditiesDbAdapter = new CommoditiesDbAdapter(db); } - mCommodities = new ArrayList<>(); } @Override @@ -82,7 +90,25 @@ public void startElement(String uri, String localName, String qName, Attributes @Override public void endDocument() throws SAXException { - mCommoditiesDbAdapter.deleteAllRecords(); - mCommoditiesDbAdapter.bulkAddRecords(mCommodities, DatabaseAdapter.UpdateMethod.insert); + if (this.deleteExisting){ + mCommoditiesDbAdapter.deleteAllRecords(); + mCommoditiesDbAdapter.bulkAddRecords(mCommodities, DatabaseAdapter.UpdateMethod.insert); + } else { + List existingCurrencyCodes = new ArrayList<>(); + + try(Cursor cursor = mCommoditiesDbAdapter.fetchAllRecords()) { + while (cursor.moveToNext()) { + String code = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseSchema.CommodityEntry.COLUMN_MNEMONIC)); + existingCurrencyCodes.add(code); + } + } + for (Commodity commodity : mCommodities) { + if (existingCurrencyCodes.contains(commodity.getCurrencyCode())){ + mCommoditiesDbAdapter.addRecord(commodity, DatabaseAdapter.UpdateMethod.update); + } else { + mCommoditiesDbAdapter.addRecord(commodity, DatabaseAdapter.UpdateMethod.insert); + } + } + } } } diff --git a/app/src/main/res/xml/fragment_about_preferences.xml b/app/src/main/res/xml/fragment_about_preferences.xml index 47e375b21..19e4bbb63 100644 --- a/app/src/main/res/xml/fragment_about_preferences.xml +++ b/app/src/main/res/xml/fragment_about_preferences.xml @@ -18,11 +18,6 @@ - - -