2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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.
9 * Mobile-Ledger 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.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23 import android.util.Log;
25 import net.ktnx.mobileledger.model.Data;
26 import net.ktnx.mobileledger.model.LedgerAccount;
27 import net.ktnx.mobileledger.utils.MLDB;
29 import java.util.ArrayList;
31 public class UpdateAccountsTask extends AsyncTask<Boolean, Void, ArrayList<LedgerAccount>> {
32 protected ArrayList<LedgerAccount> doInBackground(Boolean[] onlyStarred) {
33 Data.backgroundTaskCount.incrementAndGet();
34 String profileUUID = Data.profile.get().getUuid();
36 ArrayList<LedgerAccount> newList = new ArrayList<>();
38 String sql = "SELECT name, hidden FROM accounts WHERE profile = ?";
39 if (onlyStarred[0]) sql += " AND hidden = 0";
40 sql += " ORDER BY name";
42 SQLiteDatabase db = MLDB.getReadableDatabase();
43 try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
44 while (cursor.moveToNext()) {
45 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
46 acc.setHidden(cursor.getInt(1) == 1);
47 try (Cursor c2 = db.rawQuery(
48 "SELECT value, currency FROM account_values WHERE profile = ? " +
49 "AND account = ?", new String[]{profileUUID, acc.getName()}))
51 while (c2.moveToNext()) {
52 acc.addAmount(c2.getFloat(0), c2.getString(1));
62 Log.d("UAT", "decrementing background task count");
63 Data.backgroundTaskCount.decrementAndGet();