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;
20 import android.content.SharedPreferences;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.util.Log;
24 import java.io.BufferedReader;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.lang.ref.WeakReference;
30 import java.net.HttpURLConnection;
31 import java.net.MalformedURLException;
32 import java.net.URLDecoder;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
39 private SharedPreferences pref;
40 WeakReference<AccountSummary> mContext;
42 RetrieveAccountsTask(WeakReference<AccountSummary> context) {
47 void setPref(SharedPreferences pref) {
51 protected Void doInBackground(Void... params) {
53 HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
55 try(MobileLedgerDatabase dbh = new MobileLedgerDatabase(mContext.get())) {
56 try(SQLiteDatabase db = dbh.getWritableDatabase()) {
57 try (InputStream resp = http.getInputStream()) {
58 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
59 if (http.getResponseCode() != 200) {
60 throw new IOException(
61 String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
64 if (db.inTransaction()) throw new AssertionError();
66 db.beginTransaction();
69 db.execSQL("update account_values set keep=0;");
70 db.execSQL("update accounts set keep=0;");
73 String last_account_name = null;
75 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
77 Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
78 Pattern value_re = Pattern.compile(
79 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
80 Pattern tr_re = Pattern.compile("</tr>");
81 Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
82 Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
84 while ((line = buf.readLine()) != null) {
85 Matcher m = account_name_re.matcher(line);
87 String acct_encoded = m.group(1);
88 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
89 acct_name = acct_name.replace("\"", "");
90 Log.d("account-parser", acct_name);
92 addAccount(db, acct_name);
93 publishProgress(++count);
95 last_account_name = acct_name;
100 Matcher tr_m = tr_re.matcher(line);
102 Log.d("account-parser", "<tr> - another account expected");
103 last_account_name = null;
107 if (last_account_name != null) {
108 m = value_re.matcher(line);
109 boolean match_found = false;
112 String value = m.group(1);
113 String currency = m.group(2);
114 if (currency == null) currency = "";
115 value = value.replace(',', '.');
116 Log.d("db", "curr=" + currency + ", value=" + value);
118 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
119 new Object[]{last_account_name, currency, Float.valueOf(value)
123 if (match_found) continue;
126 m = descriptions_line_re.matcher(line);
128 db.execSQL("update description_history set keep=0;");
129 m = description_items_re.matcher(line);
131 String description = m.group(1);
132 if (description.isEmpty()) continue;
134 Log.d("db", String.format("Stored description: %s",
136 db.execSQL("insert or replace into description_history"
137 + "(description, description_upper, keep) " + "values(?, ?, 1);",
138 new Object[]{description, description.toUpperCase()
144 db.execSQL("delete from account_values where keep=0;");
145 db.execSQL("delete from accounts where keep=0;");
146 // db.execSQL("delete from description_history where keep=0;");
147 db.setTransactionSuccessful();
157 } catch (MalformedURLException e) {
158 error = R.string.err_bad_backend_url;
161 catch (FileNotFoundException e) {
162 error = R.string.err_bad_auth;
165 catch (IOException e) {
166 error = R.string.err_net_io_error;
169 catch (Exception e) {
170 error = R.string.err_net_error;
177 private void addAccount(SQLiteDatabase db, String name) {
179 LedgerAccount acc = new LedgerAccount(name);
181 "update accounts set level = ?, keep = 1 where name = ?",
182 new Object[]{acc.getLevel(), name});
183 db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?,"
184 + "?,? " + "where (select changes() = 0)",
185 new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
186 name = acc.getParentName();
187 } while (name != null);
190 protected void onPostExecute(Void result) {
191 AccountSummary ctx = mContext.get();
192 if (ctx == null) return;
193 ctx.onAccountRefreshDone(this.error);