]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
silence some debug
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.annotation.SuppressLint;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.os.OperationCanceledException;
24
25 import net.ktnx.mobileledger.err.HTTPException;
26 import net.ktnx.mobileledger.json.AccountListParser;
27 import net.ktnx.mobileledger.json.ParsedBalance;
28 import net.ktnx.mobileledger.json.ParsedLedgerAccount;
29 import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
30 import net.ktnx.mobileledger.json.TransactionListParser;
31 import net.ktnx.mobileledger.model.Data;
32 import net.ktnx.mobileledger.model.LedgerAccount;
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
35 import net.ktnx.mobileledger.model.MobileLedgerProfile;
36 import net.ktnx.mobileledger.ui.activity.MainActivity;
37 import net.ktnx.mobileledger.utils.MLDB;
38 import net.ktnx.mobileledger.utils.NetworkUtil;
39
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44 import java.lang.ref.WeakReference;
45 import java.net.HttpURLConnection;
46 import java.net.MalformedURLException;
47 import java.net.URLDecoder;
48 import java.nio.charset.StandardCharsets;
49 import java.text.ParseException;
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.Locale;
53 import java.util.Stack;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56
57 import static net.ktnx.mobileledger.utils.Logger.debug;
58
59
60 public class RetrieveTransactionsTask
61         extends AsyncTask<Void, RetrieveTransactionsTask.Progress, String> {
62     private static final int MATCHING_TRANSACTIONS_LIMIT = 150;
63     private static final Pattern reComment = Pattern.compile("^\\s*;");
64     private static final Pattern reTransactionStart = Pattern.compile("<tr class=\"title\" " +
65                                                                       "id=\"transaction-(\\d+)\"><td class=\"date\"[^\"]*>([\\d.-]+)</td>");
66     private static final Pattern reTransactionDescription =
67             Pattern.compile("<tr class=\"posting\" title=\"(\\S+)\\s(.+)");
68     private static final Pattern reTransactionDetails =
69             Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
70     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
71     private WeakReference<MainActivity> contextRef;
72     private int error;
73     // %3A is '='
74     private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
75     private Pattern reAccountValue = Pattern.compile(
76             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
77     public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef) {
78         this.contextRef = contextRef;
79     }
80     private static void L(String msg) {
81         //debug("transaction-parser", msg);
82     }
83     @Override
84     protected void onProgressUpdate(Progress... values) {
85         super.onProgressUpdate(values);
86         MainActivity context = getContext();
87         if (context == null) return;
88         context.onRetrieveProgress(values[0]);
89     }
90     @Override
91     protected void onPreExecute() {
92         super.onPreExecute();
93         MainActivity context = getContext();
94         if (context == null) return;
95         context.onRetrieveStart();
96     }
97     @Override
98     protected void onPostExecute(String error) {
99         super.onPostExecute(error);
100         MainActivity context = getContext();
101         if (context == null) return;
102         context.onRetrieveDone(error);
103     }
104     @Override
105     protected void onCancelled() {
106         super.onCancelled();
107         MainActivity context = getContext();
108         if (context == null) return;
109         context.onRetrieveDone(null);
110     }
111     private String retrieveTransactionListLegacy(MobileLedgerProfile profile)
112             throws IOException, ParseException, HTTPException {
113         Progress progress = new Progress();
114         int maxTransactionId = Progress.INDETERMINATE;
115         ArrayList<LedgerAccount> accountList = new ArrayList<>();
116         HashMap<String, Void> accountNames = new HashMap<>();
117         HashMap<String, LedgerAccount> syntheticAccounts = new HashMap<>();
118         LedgerAccount lastAccount = null, prevAccount = null;
119         boolean onlyStarred = Data.optShowOnlyStarred.get();
120
121         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
122         http.setAllowUserInteraction(false);
123         publishProgress(progress);
124         switch (http.getResponseCode()) {
125             case 200:
126                 break;
127             default:
128                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
129         }
130         try (SQLiteDatabase db = MLDB.getDatabase()) {
131             try (InputStream resp = http.getInputStream()) {
132                 if (http.getResponseCode() != 200)
133                     throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
134                 db.beginTransaction();
135                 try {
136                     prepareDbForRetrieval(db, profile);
137
138                     int matchedTransactionsCount = 0;
139
140
141                     ParserState state = ParserState.EXPECTING_ACCOUNT;
142                     String line;
143                     BufferedReader buf =
144                             new BufferedReader(new InputStreamReader(resp, StandardCharsets.UTF_8));
145
146                     int processedTransactionCount = 0;
147                     int transactionId = 0;
148                     LedgerTransaction transaction = null;
149                     LINES:
150                     while ((line = buf.readLine()) != null) {
151                         throwIfCancelled();
152                         Matcher m;
153                         m = reComment.matcher(line);
154                         if (m.find()) {
155                             // TODO: comments are ignored for now
156 //                            Log.v("transaction-parser", "Ignoring comment");
157                             continue;
158                         }
159                         //L(String.format("State is %d", updating));
160                         switch (state) {
161                             case EXPECTING_ACCOUNT:
162                                 if (line.equals("<h2>General Journal</h2>")) {
163                                     state = ParserState.EXPECTING_TRANSACTION;
164                                     L("→ expecting transaction");
165                                     // commit the current transaction and start a new one
166                                     // the account list in the UI should reflect the (committed)
167                                     // state of the database
168                                     db.setTransactionSuccessful();
169                                     db.endTransaction();
170                                     Data.accounts.setList(accountList);
171                                     db.beginTransaction();
172                                     continue;
173                                 }
174                                 m = reAccountName.matcher(line);
175                                 if (m.find()) {
176                                     String acct_encoded = m.group(1);
177                                     String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
178                                     acct_name = acct_name.replace("\"", "");
179                                     L(String.format("found account: %s", acct_name));
180
181                                     prevAccount = lastAccount;
182                                     lastAccount = profile.tryLoadAccount(db, acct_name);
183                                     if (lastAccount == null)
184                                         lastAccount = new LedgerAccount(acct_name);
185                                     else lastAccount.removeAmounts();
186                                     profile.storeAccount(db, lastAccount);
187
188                                     if (prevAccount != null) prevAccount
189                                             .setHasSubAccounts(prevAccount.isParentOf(lastAccount));
190                                     // make sure the parent account(s) are present,
191                                     // synthesising them if necessary
192                                     // this happens when the (missing-in-HTML) parent account has
193                                     // only one child so we create a synthetic parent account record,
194                                     // copying the amounts when child's amounts are parsed
195                                     String parentName = lastAccount.getParentName();
196                                     if (parentName != null) {
197                                         Stack<String> toAppend = new Stack<>();
198                                         while (parentName != null) {
199                                             if (accountNames.containsKey(parentName)) break;
200                                             toAppend.push(parentName);
201                                             parentName =
202                                                     new LedgerAccount(parentName).getParentName();
203                                         }
204                                         syntheticAccounts.clear();
205                                         while (!toAppend.isEmpty()) {
206                                             String aName = toAppend.pop();
207                                             LedgerAccount acc = profile.tryLoadAccount(db, aName);
208                                             if (acc == null) {
209                                                 acc = new LedgerAccount(aName);
210                                                 acc.setHiddenByStar(lastAccount.isHiddenByStar());
211                                                 acc.setExpanded(!lastAccount.hasSubAccounts() ||
212                                                                 lastAccount.isExpanded());
213                                             }
214                                             acc.setHasSubAccounts(true);
215                                             acc.removeAmounts();    // filled below when amounts are parsed
216                                             if ((!onlyStarred || !acc.isHiddenByStar()) &&
217                                                 acc.isVisible(accountList)) accountList.add(acc);
218                                             L(String.format("gap-filling with %s", aName));
219                                             accountNames.put(aName, null);
220                                             profile.storeAccount(db, acc);
221                                             syntheticAccounts.put(aName, acc);
222                                         }
223                                     }
224
225                                     if ((!onlyStarred || !lastAccount.isHiddenByStar()) &&
226                                         lastAccount.isVisible(accountList))
227                                         accountList.add(lastAccount);
228                                     accountNames.put(acct_name, null);
229
230                                     state = ParserState.EXPECTING_ACCOUNT_AMOUNT;
231                                     L("→ expecting account amount");
232                                 }
233                                 break;
234
235                             case EXPECTING_ACCOUNT_AMOUNT:
236                                 m = reAccountValue.matcher(line);
237                                 boolean match_found = false;
238                                 while (m.find()) {
239                                     throwIfCancelled();
240
241                                     match_found = true;
242                                     String value = m.group(1);
243                                     String currency = m.group(2);
244                                     if (currency == null) currency = "";
245                                     value = value.replace(',', '.');
246                                     L("curr=" + currency + ", value=" + value);
247                                     final float val = Float.parseFloat(value);
248                                     profile.storeAccountValue(db, lastAccount.getName(), currency,
249                                             val);
250                                     lastAccount.addAmount(val, currency);
251                                     for (LedgerAccount syn : syntheticAccounts.values()) {
252                                         syn.addAmount(val, currency);
253                                         profile.storeAccountValue(db, syn.getName(), currency, val);
254                                     }
255                                 }
256
257                                 if (match_found) {
258                                     state = ParserState.EXPECTING_ACCOUNT;
259                                     L("→ expecting account");
260                                 }
261
262                                 break;
263
264                             case EXPECTING_TRANSACTION:
265                                 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
266                                 m = reTransactionStart.matcher(line);
267                                 if (m.find()) {
268                                     transactionId = Integer.valueOf(m.group(1));
269                                     state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
270                                     L(String.format(Locale.ENGLISH,
271                                             "found transaction %d → expecting description",
272                                             transactionId));
273                                     progress.setProgress(++processedTransactionCount);
274                                     if (maxTransactionId < transactionId)
275                                         maxTransactionId = transactionId;
276                                     if ((progress.getTotal() == Progress.INDETERMINATE) ||
277                                         (progress.getTotal() < transactionId))
278                                         progress.setTotal(transactionId);
279                                     publishProgress(progress);
280                                 }
281                                 m = reEnd.matcher(line);
282                                 if (m.find()) {
283                                     L("--- transaction value complete ---");
284                                     break LINES;
285                                 }
286                                 break;
287
288                             case EXPECTING_TRANSACTION_DESCRIPTION:
289                                 if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
290                                 m = reTransactionDescription.matcher(line);
291                                 if (m.find()) {
292                                     if (transactionId == 0) throw new TransactionParserException(
293                                             "Transaction Id is 0 while expecting " + "description");
294
295                                     String date = m.group(1);
296                                     try {
297                                         int equalsIndex = date.indexOf('=');
298                                         if (equalsIndex >= 0)
299                                             date = date.substring(equalsIndex + 1);
300                                         transaction = new LedgerTransaction(transactionId, date,
301                                                 m.group(2));
302                                     }
303                                     catch (ParseException e) {
304                                         e.printStackTrace();
305                                         return String.format("Error parsing date '%s'", date);
306                                     }
307                                     state = ParserState.EXPECTING_TRANSACTION_DETAILS;
308                                     L(String.format(Locale.ENGLISH,
309                                             "transaction %d created for %s (%s) →" +
310                                             " expecting details", transactionId, date, m.group(2)));
311                                 }
312                                 break;
313
314                             case EXPECTING_TRANSACTION_DETAILS:
315                                 if (line.isEmpty()) {
316                                     // transaction data collected
317                                     if (transaction.existsInDb(db)) {
318                                         profile.markTransactionAsPresent(db, transaction);
319                                         matchedTransactionsCount++;
320
321                                         if (matchedTransactionsCount ==
322                                             MATCHING_TRANSACTIONS_LIMIT)
323                                         {
324                                             profile.markTransactionsBeforeTransactionAsPresent(db,
325                                                     transaction);
326                                             progress.setTotal(progress.getProgress());
327                                             publishProgress(progress);
328                                             break LINES;
329                                         }
330                                     }
331                                     else {
332                                         profile.storeTransaction(db, transaction);
333                                         matchedTransactionsCount = 0;
334                                         progress.setTotal(maxTransactionId);
335                                     }
336
337                                     state = ParserState.EXPECTING_TRANSACTION;
338                                     L(String.format("transaction %s saved → expecting transaction",
339                                             transaction.getId()));
340                                     transaction.finishLoading();
341
342 // sounds like a good idea, but transaction-1 may not be the first one chronologically
343 // for example, when you add the initial seeding transaction after entering some others
344 //                                            if (transactionId == 1) {
345 //                                                L("This was the initial transaction. Terminating " +
346 //                                                  "parser");
347 //                                                break LINES;
348 //                                            }
349                                 }
350                                 else {
351                                     m = reTransactionDetails.matcher(line);
352                                     if (m.find()) {
353                                         String acc_name = m.group(1);
354                                         String amount = m.group(2);
355                                         String currency = m.group(3);
356                                         if (currency == null) currency = "";
357                                         amount = amount.replace(',', '.');
358                                         transaction.addAccount(
359                                                 new LedgerTransactionAccount(acc_name,
360                                                         Float.valueOf(amount), currency));
361                                         L(String.format(Locale.ENGLISH, "%d: %s = %s",
362                                                 transaction.getId(), acc_name, amount));
363                                     }
364                                     else throw new IllegalStateException(String.format(
365                                             "Can't parse transaction %d " + "details: %s",
366                                             transactionId, line));
367                                 }
368                                 break;
369                             default:
370                                 throw new RuntimeException(
371                                         String.format("Unknown parser updating %s", state.name()));
372                         }
373                     }
374
375                     throwIfCancelled();
376
377                     profile.deleteNotPresentTransactions(db);
378                     db.setTransactionSuccessful();
379
380                     profile.setLastUpdateStamp();
381
382                     return null;
383                 }
384                 finally {
385                     db.endTransaction();
386                 }
387             }
388         }
389     }
390     private void prepareDbForRetrieval(SQLiteDatabase db, MobileLedgerProfile profile) {
391         db.execSQL("UPDATE transactions set keep=0 where profile=?",
392                 new String[]{profile.getUuid()});
393         db.execSQL("update account_values set keep=0 where profile=?;",
394                 new String[]{profile.getUuid()});
395         db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
396     }
397     private boolean retrieveAccountList(MobileLedgerProfile profile)
398             throws IOException, HTTPException {
399         Progress progress = new Progress();
400
401         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "accounts");
402         http.setAllowUserInteraction(false);
403         switch (http.getResponseCode()) {
404             case 200:
405                 break;
406             case 404:
407                 return false;
408             default:
409                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
410         }
411         publishProgress(progress);
412         SQLiteDatabase db = MLDB.getDatabase();
413         ArrayList<LedgerAccount> accountList = new ArrayList<>();
414         boolean listFilledOK = false;
415         try (InputStream resp = http.getInputStream()) {
416             if (http.getResponseCode() != 200)
417                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
418
419             db.beginTransaction();
420             try {
421                 profile.markAccountsAsNotPresent(db);
422
423                 AccountListParser parser = new AccountListParser(resp);
424
425                 LedgerAccount prevAccount = null;
426
427                 while (true) {
428                     throwIfCancelled();
429                     ParsedLedgerAccount parsedAccount = parser.nextAccount();
430                     if (parsedAccount == null) break;
431
432                     LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
433                     if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
434                     else acc.removeAmounts();
435
436                     profile.storeAccount(db, acc);
437                     String lastCurrency = null;
438                     float lastCurrencyAmount = 0;
439                     for (ParsedBalance b : parsedAccount.getAibalance()) {
440                         final String currency = b.getAcommodity();
441                         final float amount = b.getAquantity().asFloat();
442                         if (currency.equals(lastCurrency)) lastCurrencyAmount += amount;
443                         else {
444                             if (lastCurrency != null) {
445                                 profile.storeAccountValue(db, acc.getName(), lastCurrency,
446                                         lastCurrencyAmount);
447                                 acc.addAmount(lastCurrencyAmount, lastCurrency);
448                             }
449                             lastCurrency = currency;
450                             lastCurrencyAmount = amount;
451                         }
452                     }
453                     if (lastCurrency != null) {
454                         profile.storeAccountValue(db, acc.getName(), lastCurrency,
455                                 lastCurrencyAmount);
456                         acc.addAmount(lastCurrencyAmount, lastCurrency);
457                     }
458
459                     if (acc.isVisible(accountList)) accountList.add(acc);
460
461                     if (prevAccount != null) {
462                         prevAccount.setHasSubAccounts(
463                                 acc.getName().startsWith(prevAccount.getName() + ":"));
464                     }
465
466                     prevAccount = acc;
467                 }
468                 throwIfCancelled();
469
470                 profile.deleteNotPresentAccounts(db);
471                 throwIfCancelled();
472                 db.setTransactionSuccessful();
473                 listFilledOK = true;
474             }
475             finally {
476                 db.endTransaction();
477             }
478         }
479         // should not be set in the DB transaction, because of a possible deadlock
480         // with the main and DbOpQueueRunner threads
481         if (listFilledOK) Data.accounts.setList(accountList);
482
483         return true;
484     }
485     private boolean retrieveTransactionList(MobileLedgerProfile profile)
486             throws IOException, ParseException, HTTPException {
487         Progress progress = new Progress();
488         int maxTransactionId = Progress.INDETERMINATE;
489
490         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
491         http.setAllowUserInteraction(false);
492         publishProgress(progress);
493         switch (http.getResponseCode()) {
494             case 200:
495                 break;
496             case 404:
497                 return false;
498             default:
499                 throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
500         }
501         SQLiteDatabase db = MLDB.getDatabase();
502         try (InputStream resp = http.getInputStream()) {
503             if (http.getResponseCode() != 200)
504                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
505             throwIfCancelled();
506             db.beginTransaction();
507             try {
508                 profile.markTransactionsAsNotPresent(db);
509
510                 int matchedTransactionsCount = 0;
511                 TransactionListParser parser = new TransactionListParser(resp);
512
513                 int processedTransactionCount = 0;
514
515                 DetectedTransactionOrder transactionOrder = DetectedTransactionOrder.UNKNOWN;
516                 int orderAccumulator = 0;
517                 int lastTransactionId = 0;
518
519                 while (true) {
520                     throwIfCancelled();
521                     ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
522                     throwIfCancelled();
523                     if (parsedTransaction == null) break;
524
525                     LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
526                     if (transaction.getId() > lastTransactionId) orderAccumulator++;
527                     else orderAccumulator--;
528                     lastTransactionId = transaction.getId();
529                     if (transactionOrder == DetectedTransactionOrder.UNKNOWN) {
530                         if (orderAccumulator > 30) {
531                             transactionOrder = DetectedTransactionOrder.FILE;
532                             debug("rtt", String.format(Locale.ENGLISH,
533                                     "Detected native file order after %d transactions (factor %d)",
534                                     processedTransactionCount, orderAccumulator));
535                             progress.setTotal(Data.transactions.size());
536                         }
537                         else if (orderAccumulator < -30) {
538                             transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL;
539                             debug("rtt", String.format(Locale.ENGLISH,
540                                     "Detected reverse chronological order after %d transactions (factor %d)",
541                                     processedTransactionCount, orderAccumulator));
542                         }
543                     }
544
545                     if (transaction.existsInDb(db)) {
546                         profile.markTransactionAsPresent(db, transaction);
547                         matchedTransactionsCount++;
548
549                         if ((transactionOrder == DetectedTransactionOrder.REVERSE_CHRONOLOGICAL) &&
550                             (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT))
551                         {
552                             profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
553                             progress.setTotal(progress.getProgress());
554                             publishProgress(progress);
555                             db.setTransactionSuccessful();
556                             profile.setLastUpdateStamp();
557                             return true;
558                         }
559                     }
560                     else {
561                         profile.storeTransaction(db, transaction);
562                         matchedTransactionsCount = 0;
563                         progress.setTotal(maxTransactionId);
564                     }
565
566
567                     if ((transactionOrder != DetectedTransactionOrder.UNKNOWN) &&
568                         ((progress.getTotal() == Progress.INDETERMINATE) ||
569                          (progress.getTotal() < transaction.getId())))
570                         progress.setTotal(transaction.getId());
571
572                     progress.setProgress(++processedTransactionCount);
573                     publishProgress(progress);
574                 }
575
576                 throwIfCancelled();
577                 profile.deleteNotPresentTransactions(db);
578                 throwIfCancelled();
579                 db.setTransactionSuccessful();
580                 profile.setLastUpdateStamp();
581             }
582             finally {
583                 db.endTransaction();
584             }
585         }
586
587         return true;
588     }
589
590     @SuppressLint("DefaultLocale")
591     @Override
592     protected String doInBackground(Void... params) {
593         MobileLedgerProfile profile = Data.profile.get();
594         Data.backgroundTaskStarted();
595         try {
596             if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
597                 return retrieveTransactionListLegacy(profile);
598             return null;
599         }
600         catch (MalformedURLException e) {
601             e.printStackTrace();
602             return "Invalid server URL";
603         }
604         catch (HTTPException e) {
605             e.printStackTrace();
606             return String.format("HTTP error %d: %s", e.getResponseCode(), e.getResponseMessage());
607         }
608         catch (IOException e) {
609             e.printStackTrace();
610             return e.getLocalizedMessage();
611         }
612         catch (ParseException e) {
613             e.printStackTrace();
614             return "Network error";
615         }
616         catch (OperationCanceledException e) {
617             e.printStackTrace();
618             return "Operation cancelled";
619         }
620         finally {
621             Data.backgroundTaskFinished();
622         }
623     }
624     private MainActivity getContext() {
625         return contextRef.get();
626     }
627     private void throwIfCancelled() {
628         if (isCancelled()) throw new OperationCanceledException(null);
629     }
630     enum DetectedTransactionOrder {UNKNOWN, REVERSE_CHRONOLOGICAL, FILE}
631
632     private enum ParserState {
633         EXPECTING_ACCOUNT, EXPECTING_ACCOUNT_AMOUNT, EXPECTING_JOURNAL, EXPECTING_TRANSACTION,
634         EXPECTING_TRANSACTION_DESCRIPTION, EXPECTING_TRANSACTION_DETAILS
635     }
636
637     public class Progress {
638         public static final int INDETERMINATE = -1;
639         private int progress;
640         private int total;
641         Progress() {
642             this(INDETERMINATE, INDETERMINATE);
643         }
644         Progress(int progress, int total) {
645             this.progress = progress;
646             this.total = total;
647         }
648         public int getProgress() {
649             return progress;
650         }
651         protected void setProgress(int progress) {
652             this.progress = progress;
653         }
654         public int getTotal() {
655             return total;
656         }
657         protected void setTotal(int total) {
658             this.total = total;
659         }
660     }
661
662     private class TransactionParserException extends IllegalStateException {
663         TransactionParserException(String message) {
664             super(message);
665         }
666     }
667 }