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.
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.
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/>.
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<Void, Void, ArrayList<LedgerAccount>> {
32 protected ArrayList<LedgerAccount> doInBackground(Void... params) {
33 Data.backgroundTaskCount.incrementAndGet();
34 String profileUUID = Data.profile.get().getUuid();
35 boolean onlyStarred = Data.optShowOnlyStarred.get();
37 ArrayList<LedgerAccount> newList = new ArrayList<>();
39 String sql = "SELECT name, hidden FROM accounts WHERE profile = ?";
40 if (onlyStarred) sql += " AND hidden = 0";
41 sql += " ORDER BY name";
43 SQLiteDatabase db = MLDB.getReadableDatabase();
44 try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
45 while (cursor.moveToNext()) {
46 LedgerAccount acc = new LedgerAccount(cursor.getString(0));
47 acc.setHidden(cursor.getInt(1) == 1);
48 try (Cursor c2 = db.rawQuery(
49 "SELECT value, currency FROM account_values WHERE profile = ? " +
50 "AND account = ?", new String[]{profileUUID, acc.getName()}))
52 while (c2.moveToNext()) {
53 acc.addAmount(c2.getFloat(0), c2.getString(1));
63 Log.d("UAT", "decrementing background task count");
64 Data.backgroundTaskCount.decrementAndGet();