1 package net.ktnx.mobileledger;
3 import android.content.SharedPreferences;
4 import android.util.Log;
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.net.HttpURLConnection;
11 import java.net.MalformedURLException;
12 import java.net.URLDecoder;
13 import java.util.regex.Matcher;
14 import java.util.regex.Pattern;
16 import static net.ktnx.mobileledger.MobileLedgerDB.db;
18 abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
21 private SharedPreferences pref;
23 RetrieveAccountsTask() {
27 void setPref(SharedPreferences pref) {
31 protected Void doInBackground(Void... params) {
33 HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
34 http.setAllowUserInteraction(false);
35 http.setRequestProperty("Accept-Charset", "UTF-8");
37 InputStream resp = http.getInputStream();
39 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
40 if (http.getResponseCode() != 200) {
41 throw new IOException(String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
44 if (db.inTransaction()) throw new AssertionError();
46 db.beginTransaction();
49 db.execSQL("update account_values set keep=0;");
50 db.execSQL("update accounts set keep=0;");
53 String last_account_name = null;
54 BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
56 Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
57 Pattern value_re = Pattern.compile("<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
58 Pattern tr_re = Pattern.compile("</tr>");
59 Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\b");
60 Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
62 while ((line = buf.readLine()) != null) {
63 Matcher m = account_name_re.matcher(line);
65 String acct_encoded = m.group(1);
66 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
67 acct_name = acct_name.replace("\"", "");
68 Log.d("account-parser", acct_name);
70 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
71 publishProgress(++count);
73 last_account_name = acct_name;
78 Matcher tr_m = tr_re.matcher(line);
80 Log.d("account-parser", "<tr> - another account expected");
81 last_account_name = null;
85 if (last_account_name != null) {
86 m = value_re.matcher(line);
87 boolean match_found = false;
90 String value = m.group(1);
91 String currency = m.group(2);
92 if (currency == null) currency = "";
93 value = value.replace(',', '.');
94 Log.d("db", "curr=" + currency + ", value=" + value);
95 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
96 new Object[]{last_account_name, currency, Float.valueOf(value)});
99 if (match_found) continue;
102 m = descriptions_line_re.matcher(line);
104 db.execSQL("update description_history set keep=0;");
105 m = description_items_re.matcher(line);
107 String description = m.group(1);
108 if (description.isEmpty()) continue;
110 Log.d("db", String.format("Stored description: %s", description));
111 db.execSQL("insert or replace into description_history(description, keep) values(?, 1);", new Object[]{description});
116 db.execSQL("delete from account_values where keep=0;");
117 db.execSQL("delete from accounts where keep=0;");
118 db.execSQL("delete from description_history where keep=0;");
119 db.setTransactionSuccessful();
130 } catch (MalformedURLException e) {
131 error = R.string.err_bad_backend_url;
133 } catch (IOException e) {
134 error = R.string.err_net_io_error;
137 catch (Exception e) {
138 error = R.string.err_net_error;
145 abstract protected void onProgressUpdate(Integer... values);
147 abstract protected void onPostExecute(Void result);