+ // should not be set in the DB transaction, because of a possible deadlock
+ // with the main and DbOpQueueRunner threads
+ if (listFilledOK) Data.accounts.setList(accountList);
+
+ return true;
+ }
+ private boolean retrieveTransactionList(MobileLedgerProfile profile)
+ throws IOException, ParseException, HTTPException {
+ Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
+
+ HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
+ http.setAllowUserInteraction(false);
+ publishProgress(progress);
+ switch (http.getResponseCode()) {
+ case 200:
+ break;
+ case 404:
+ return false;
+ default:
+ throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+ }
+ SQLiteDatabase db = MLDB.getDatabase();
+ try (InputStream resp = http.getInputStream()) {
+ if (http.getResponseCode() != 200)
+ throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
+ throwIfCancelled();
+ db.beginTransaction();
+ try {
+ profile.markTransactionsAsNotPresent(db);
+
+ int matchedTransactionsCount = 0;
+ TransactionListParser parser = new TransactionListParser(resp);
+
+ int processedTransactionCount = 0;
+
+ while (true) {
+ throwIfCancelled();
+ ParsedLedgerTransaction parsedTransaction = parser.nextTransaction();
+ throwIfCancelled();
+ if (parsedTransaction == null) break;
+ LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
+ if (transaction.existsInDb(db)) {
+ profile.markTransactionAsPresent(db, transaction);
+ matchedTransactionsCount++;
+
+ if (matchedTransactionsCount == MATCHING_TRANSACTIONS_LIMIT) {
+ profile.markTransactionsBeforeTransactionAsPresent(db, transaction);
+ progress.setTotal(progress.getProgress());
+ publishProgress(progress);
+ db.setTransactionSuccessful();
+ profile.setLastUpdateStamp();
+ return true;
+ }
+ }
+ else {
+ profile.storeTransaction(db, transaction);
+ matchedTransactionsCount = 0;
+ progress.setTotal(maxTransactionId);
+ }
+
+ if ((progress.getTotal() == Progress.INDETERMINATE) ||
+ (progress.getTotal() < transaction.getId()))
+ progress.setTotal(transaction.getId());
+
+ progress.setProgress(++processedTransactionCount);
+ publishProgress(progress);
+ }
+
+ throwIfCancelled();
+ profile.deleteNotPresentTransactions(db);
+ throwIfCancelled();
+ db.setTransactionSuccessful();
+ profile.setLastUpdateStamp();
+ }
+ finally {
+ db.endTransaction();
+ }
+ }
+
+ return true;
+ }
+ @SuppressLint("DefaultLocale")
+ @Override
+ protected String doInBackground(Void... params) {
+ MobileLedgerProfile profile = Data.profile.get();
+ Data.backgroundTaskCount.incrementAndGet();
+ try {
+ if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
+ return retrieveTransactionListLegacy(profile);
+ return null;
+ }