]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/UpdateAccountsTask.java
wrap Log.d calls, skipping them on non-debug builds
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / UpdateAccountsTask.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.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23
24 import net.ktnx.mobileledger.model.Data;
25 import net.ktnx.mobileledger.model.LedgerAccount;
26 import net.ktnx.mobileledger.model.MobileLedgerProfile;
27 import net.ktnx.mobileledger.utils.MLDB;
28
29 import java.util.ArrayList;
30
31 import static net.ktnx.mobileledger.utils.Logger.debug;
32
33 public class UpdateAccountsTask extends AsyncTask<Void, Void, ArrayList<LedgerAccount>> {
34     protected ArrayList<LedgerAccount> doInBackground(Void... params) {
35         Data.backgroundTaskStarted();
36         try {
37             MobileLedgerProfile profile = Data.profile.get();
38             String profileUUID = profile.getUuid();
39             boolean onlyStarred = Data.optShowOnlyStarred.get();
40             ArrayList<LedgerAccount> newList = new ArrayList<>();
41
42             String sql = "SELECT a.name from accounts a WHERE a.profile = ?";
43             if (onlyStarred) sql += " AND a.hidden = 0";
44             sql += " ORDER BY a.name";
45
46             SQLiteDatabase db = MLDB.getDatabase();
47             try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) {
48                 while (cursor.moveToNext()) {
49                     final String accName = cursor.getString(0);
50 //                    debug("accounts",
51 //                            String.format("Read account '%s' from DB [%s]", accName, profileUUID));
52                     LedgerAccount acc = profile.loadAccount(db, accName);
53                     if (acc.isVisible(newList)) newList.add(acc);
54                 }
55             }
56
57             return newList;
58         }
59         finally {
60             debug("UAT", "decrementing background task count");
61             Data.backgroundTaskFinished();
62         }
63     }
64 }