a27b1c8cb5b27b5c2b0567726c1a139026cbaff8
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveAccountsTask.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.content.SharedPreferences;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.OperationCanceledException;
23 import android.util.Log;
24
25 import net.ktnx.mobileledger.R;
26 import net.ktnx.mobileledger.model.LedgerAccount;
27 import net.ktnx.mobileledger.ui.activity.MainActivity;
28 import net.ktnx.mobileledger.utils.MLDB;
29 import net.ktnx.mobileledger.utils.NetworkUtil;
30
31 import java.io.BufferedReader;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.lang.ref.WeakReference;
37 import java.net.HttpURLConnection;
38 import java.net.MalformedURLException;
39 import java.net.URLDecoder;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
44     int error;
45     WeakReference<MainActivity> mContext;
46     private SharedPreferences pref;
47
48     public RetrieveAccountsTask(WeakReference<MainActivity> context) {
49         mContext = context;
50         error = 0;
51     }
52
53     public void setPref(SharedPreferences pref) {
54         this.pref = pref;
55     }
56
57     protected Void doInBackground(Void... params) {
58         try {
59             HttpURLConnection http = NetworkUtil.prepare_connection(pref, "add");
60             publishProgress(0);
61             try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
62                 try (InputStream resp = http.getInputStream()) {
63                     Log.d("update_accounts", String.valueOf(http.getResponseCode()));
64                     if (http.getResponseCode() != 200) {
65                         throw new IOException(
66                                 String.format("HTTP error: %d %s", http.getResponseCode(),
67                                         http.getResponseMessage()));
68                     }
69                     else {
70                         if (db.inTransaction()) throw new AssertionError();
71
72                         db.beginTransaction();
73
74                         try {
75                             db.execSQL("update account_values set keep=0;");
76                             db.execSQL("update accounts set keep=0;");
77
78                             String line;
79                             String last_account_name = null;
80                             BufferedReader buf =
81                                     new BufferedReader(new InputStreamReader(resp, "UTF-8"));
82                             // %3A is '='
83                             Pattern account_name_re =
84                                     Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
85                             Pattern account_value_re = Pattern.compile(
86                                     "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
87                             Pattern tr_end_re = Pattern.compile("</tr>");
88                             Pattern descriptions_line_re =
89                                     Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
90                             Pattern description_items_re =
91                                     Pattern.compile("\"value\":\"([^\"]+)\"");
92                             int count = 0;
93                             while ((line = buf.readLine()) != null) {
94                                 throwIfCancelled();
95
96                                 Matcher m = account_name_re.matcher(line);
97                                 if (m.find()) {
98                                     String acct_encoded = m.group(1);
99                                     String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
100                                     acct_name = acct_name.replace("\"", "");
101                                     Log.d("account-parser", acct_name);
102
103                                     addAccount(db, acct_name);
104                                     publishProgress(++count);
105
106                                     last_account_name = acct_name;
107
108                                     continue;
109                                 }
110
111                                 Matcher tr_m = tr_end_re.matcher(line);
112                                 if (tr_m.find()) {
113                                     Log.d("account-parser", "<tr> - another account expected");
114                                     last_account_name = null;
115                                     continue;
116                                 }
117
118                                 if (last_account_name != null) {
119                                     m = account_value_re.matcher(line);
120                                     boolean match_found = false;
121                                     while (m.find()) {
122                                         throwIfCancelled();
123
124                                         match_found = true;
125                                         String value = m.group(1);
126                                         String currency = m.group(2);
127                                         if (currency == null) currency = "";
128                                         value = value.replace(',', '.');
129                                         Log.d("db", "curr=" + currency + ", value=" + value);
130                                         db.execSQL(
131                                                 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
132                                                 new Object[]{last_account_name, currency,
133                                                              Float.valueOf(value)
134                                                 });
135                                     }
136
137                                     if (match_found) continue;
138                                 }
139
140                                 m = descriptions_line_re.matcher(line);
141                                 if (m.find()) {
142                                     db.execSQL("update description_history set keep=0;");
143                                     m = description_items_re.matcher(line);
144                                     while (m.find()) {
145                                         throwIfCancelled();
146
147                                         String description = m.group(1);
148                                         if (description.isEmpty()) continue;
149
150                                         Log.d("db", String.format("Stored description: %s",
151                                                 description));
152                                         db.execSQL("insert or replace into description_history" +
153                                                    "(description, description_upper, keep) " +
154                                                    "values(?, ?, 1);",
155                                                 new Object[]{description, description.toUpperCase()
156                                                 });
157                                     }
158                                 }
159                             }
160
161                             db.execSQL("delete from account_values where keep=0;");
162                             db.execSQL("delete from accounts where keep=0;");
163                             db.setTransactionSuccessful();
164                         }
165                         catch (OperationCanceledException e) {
166                             Log.w("async", "Account retrieval cancelled");
167                         }
168                         finally {
169                             db.endTransaction();
170                         }
171                     }
172                 }
173             }
174         }
175         catch (MalformedURLException e) {
176             error = R.string.err_bad_backend_url;
177             e.printStackTrace();
178         }
179         catch (FileNotFoundException e) {
180             error = R.string.err_bad_auth;
181             e.printStackTrace();
182         }
183         catch (IOException e) {
184             error = R.string.err_net_io_error;
185             e.printStackTrace();
186         }
187         catch (Exception e) {
188             error = R.string.err_net_error;
189             e.printStackTrace();
190         }
191
192         return null;
193     }
194     private void throwIfCancelled() {
195         if (isCancelled()) throw new OperationCanceledException(null);
196     }
197
198     private void addAccount(SQLiteDatabase db, String name) {
199         do {
200             LedgerAccount acc = new LedgerAccount(name);
201             db.execSQL("update accounts set level = ?, keep = 1 where name = ?",
202                     new Object[]{acc.getLevel(), name});
203             db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?," +
204                        "?,? " + "where (select changes() = 0)",
205                     new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
206             name = acc.getParentName();
207         } while (name != null);
208     }
209     @Override
210     protected void onPostExecute(Void result) {
211         MainActivity ctx = mContext.get();
212         if (ctx == null) return;
213         ctx.onRetrieveDone(this.error == 0);
214     }
215
216 }