1616
1717package org .gnucash .android .db ;
1818
19+ import android .app .ProgressDialog ;
1920import android .content .ContentValues ;
2021import android .content .Context ;
2122import android .database .Cursor ;
2223import android .database .sqlite .SQLiteDatabase ;
2324import android .database .sqlite .SQLiteOpenHelper ;
2425import android .util .Log ;
26+ import android .widget .Toast ;
2527import org .gnucash .android .export .ExportFormat ;
2628import org .gnucash .android .model .Account ;
2729import org .gnucash .android .model .AccountType ;
@@ -102,12 +104,18 @@ public class DatabaseHelper extends SQLiteOpenHelper {
102104 + "UNIQUE (" + SplitEntry .COLUMN_UID + ") "
103105 + ");" ;
104106
107+ /**
108+ * Context passed in for database upgrade. Keep reference so as to be able to display UI dialogs
109+ */
110+ private Context mContext ;
111+
105112 /**
106113 * Constructor
107114 * @param context Application context
108115 */
109116 public DatabaseHelper (Context context ){
110117 super (context , DATABASE_NAME , null , DatabaseSchema .DATABASE_VERSION );
118+ mContext = context ;
111119 }
112120
113121 @ Override
@@ -119,7 +127,9 @@ public void onCreate(SQLiteDatabase db) {
119127 public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
120128 Log .i (LOG_TAG , "Upgrading database from version "
121129 + oldVersion + " to " + newVersion );
122-
130+
131+ ProgressDialog progressDialog = ProgressDialog .show (mContext , "Upgrading database" , "Processing..." , true );
132+
123133 if (oldVersion < newVersion ){
124134 //introducing double entry accounting
125135 Log .i (LOG_TAG , "Upgrading database to version " + newVersion );
@@ -185,6 +195,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
185195
186196 if (oldVersion == 5 && newVersion >= 6 ){
187197 Log .i (LOG_TAG , "Upgrading database to version 6" );
198+ progressDialog .setMessage ("Upgrading database to version 6" );
199+
188200 String addFullAccountNameQuery = " ALTER TABLE " + AccountEntry .TABLE_NAME
189201 + " ADD COLUMN " + AccountEntry .COLUMN_FULL_NAME + " varchar(255) " ;
190202 db .execSQL (addFullAccountNameQuery );
@@ -216,6 +228,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
216228
217229 if (oldVersion == 6 && newVersion >= DatabaseSchema .SPLITS_DB_VERSION ){
218230 Log .i (LOG_TAG , "Upgrading database to version 7" );
231+ progressDialog .setMessage ("Upgrading to version " + SPLITS_DB_VERSION );
219232
220233 //for users who do not have double-entry activated, we create imbalance accounts for their splits
221234 //TODO: Enable when we can hide imbalance accounts from user
@@ -225,17 +238,27 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
225238// accountsDbAdapter.getOrCreateImbalanceAccountUID(currency);
226239// }
227240
228- String filepath = MigrationHelper .exportDatabase (db , ExportFormat .GNC_XML );
241+ progressDialog .setMessage ("Backing up database" );
242+ try {
243+ String filepath = MigrationHelper .exportDatabase (db , ExportFormat .GNC_XML );
229244
230- dropAllDatabaseTables (db );
231- createDatabaseTables (db );
245+ progressDialog .setMessage ("Upgrading database schema" );
232246
233- MigrationHelper .importGnucashXML (db , filepath );
247+ dropAllDatabaseTables (db );
248+ createDatabaseTables (db );
234249
250+ progressDialog .setMessage ("Restoring database" );
251+
252+ MigrationHelper .importGnucashXML (db , filepath );
253+ } catch (Exception e ){
254+ Toast .makeText (mContext , "Error upgrading database.\n " + e .getMessage (), Toast .LENGTH_LONG ).show ();
255+ throw new RuntimeException (e );
256+ }
235257 oldVersion = DatabaseSchema .SPLITS_DB_VERSION ;
236258 }
237259 }
238260
261+ progressDialog .dismiss ();
239262 if (oldVersion != newVersion ) {
240263 Log .w (LOG_TAG , "Upgrade for the database failed. The Database is currently at version " + oldVersion );
241264 }
0 commit comments