2 * Copyright © 2018 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.content.SharedPreferences;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
24 import net.ktnx.mobileledger.AccountSummary;
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.model.LedgerAccount;
27 import net.ktnx.mobileledger.utils.MLDB;
28 import net.ktnx.mobileledger.utils.NetworkUtil;
30 import java.io.BufferedReader;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.lang.ref.WeakReference;
36 import java.net.HttpURLConnection;
37 import java.net.MalformedURLException;
38 import java.net.URLDecoder;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
42 public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
44 WeakReference<AccountSummary> mContext;
45 private SharedPreferences pref;
47 public RetrieveAccountsTask(WeakReference<AccountSummary> context) {
52 public void setPref(SharedPreferences pref) {
56 protected Void doInBackground(Void... params) {
58 HttpURLConnection http = NetworkUtil.prepare_connection(pref, "add");
60 try (SQLiteDatabase db = MLDB.getWritableDatabase(mContext.get())) {
61 try (InputStream resp = http.getInputStream()) {
62 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
63 if (http.getResponseCode() != 200) {
64 throw new IOException(
65 String.format("HTTP error: %d %s", http.getResponseCode(),
66 http.getResponseMessage()));
69 if (db.inTransaction()) throw new AssertionError();
71 db.beginTransaction();
74 db.execSQL("update account_values set keep=0;");
75 db.execSQL("update accounts set keep=0;");
78 String last_account_name = null;
80 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
82 Pattern account_name_re =
83 Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
84 Pattern value_re = Pattern.compile(
85 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
86 Pattern tr_re = Pattern.compile("</tr>");
87 Pattern descriptions_line_re =
88 Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
89 Pattern description_items_re =
90 Pattern.compile("\"value\":\"([^\"]+)\"");
92 while ((line = buf.readLine()) != null) {
93 Matcher m = account_name_re.matcher(line);
95 String acct_encoded = m.group(1);
96 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
97 acct_name = acct_name.replace("\"", "");
98 Log.d("account-parser", acct_name);
100 addAccount(db, acct_name);
101 publishProgress(++count);
103 last_account_name = acct_name;
108 Matcher tr_m = tr_re.matcher(line);
110 Log.d("account-parser", "<tr> - another account expected");
111 last_account_name = null;
115 if (last_account_name != null) {
116 m = value_re.matcher(line);
117 boolean match_found = false;
120 String value = m.group(1);
121 String currency = m.group(2);
122 if (currency == null) currency = "";
123 value = value.replace(',', '.');
124 Log.d("db", "curr=" + currency + ", value=" + value);
126 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
127 new Object[]{last_account_name, currency,
132 if (match_found) continue;
135 m = descriptions_line_re.matcher(line);
137 db.execSQL("update description_history set keep=0;");
138 m = description_items_re.matcher(line);
140 String description = m.group(1);
141 if (description.isEmpty()) continue;
143 Log.d("db", String.format("Stored description: %s",
145 db.execSQL("insert or replace into description_history" +
146 "(description, description_upper, keep) " +
148 new Object[]{description, description.toUpperCase()
154 db.execSQL("delete from account_values where keep=0;");
155 db.execSQL("delete from accounts where keep=0;");
156 // db.execSQL("delete from description_history where keep=0;");
157 db.setTransactionSuccessful();
167 catch (MalformedURLException e) {
168 error = R.string.err_bad_backend_url;
171 catch (FileNotFoundException e) {
172 error = R.string.err_bad_auth;
175 catch (IOException e) {
176 error = R.string.err_net_io_error;
179 catch (Exception e) {
180 error = R.string.err_net_error;
187 private void addAccount(SQLiteDatabase db, String name) {
189 LedgerAccount acc = new LedgerAccount(name);
190 db.execSQL("update accounts set level = ?, keep = 1 where name = ?",
191 new Object[]{acc.getLevel(), name});
192 db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?," +
193 "?,? " + "where (select changes() = 0)",
194 new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
195 name = acc.getParentName();
196 } while (name != null);
199 protected void onPostExecute(Void result) {
200 AccountSummary ctx = mContext.get();
201 if (ctx == null) return;
202 ctx.onAccountRefreshDone(this.error);