1 package net.ktnx.mobileledger;
3 import android.content.SharedPreferences;
4 import android.util.Log;
6 import java.io.BufferedReader;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.net.HttpURLConnection;
12 import java.net.MalformedURLException;
13 import java.net.URLDecoder;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
17 import static net.ktnx.mobileledger.MobileLedgerDB.db;
19 abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
22 private SharedPreferences pref;
24 RetrieveAccountsTask() {
28 void setPref(SharedPreferences pref) {
32 protected Void doInBackground(Void... params) {
34 HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
35 http.setAllowUserInteraction(false);
36 http.setRequestProperty("Accept-Charset", "UTF-8");
38 InputStream resp = http.getInputStream();
40 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
41 if (http.getResponseCode() != 200) {
42 throw new IOException(String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
45 if (db.inTransaction()) throw new AssertionError();
47 db.beginTransaction();
50 db.execSQL("update account_values set keep=0;");
51 db.execSQL("update accounts set keep=0;");
54 String last_account_name = null;
55 BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
57 Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
58 Pattern value_re = Pattern.compile("<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
59 Pattern tr_re = Pattern.compile("</tr>");
60 Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
61 Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
63 while ((line = buf.readLine()) != null) {
64 Matcher m = account_name_re.matcher(line);
66 String acct_encoded = m.group(1);
67 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
68 acct_name = acct_name.replace("\"", "");
69 Log.d("account-parser", acct_name);
71 db.execSQL("insert or replace into accounts(name, name_upper, " +
72 "keep) values(?, ?, 1)",
73 new Object[]{acct_name, acct_name.toUpperCase()});
74 publishProgress(++count);
76 last_account_name = acct_name;
81 Matcher tr_m = tr_re.matcher(line);
83 Log.d("account-parser", "<tr> - another account expected");
84 last_account_name = null;
88 if (last_account_name != null) {
89 m = value_re.matcher(line);
90 boolean match_found = false;
93 String value = m.group(1);
94 String currency = m.group(2);
95 if (currency == null) currency = "";
96 value = value.replace(',', '.');
97 Log.d("db", "curr=" + currency + ", value=" + value);
98 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
99 new Object[]{last_account_name, currency, Float.valueOf(value)});
102 if (match_found) continue;
105 m = descriptions_line_re.matcher(line);
107 db.execSQL("update description_history set keep=0;");
108 m = description_items_re.matcher(line);
110 String description = m.group(1);
111 if (description.isEmpty()) continue;
113 Log.d("db", String.format("Stored description: %s", description));
114 db.execSQL("insert or replace into description_history" +
115 "(description, description_upper, keep) " +
117 new Object[]{description, description.toUpperCase()});
122 db.execSQL("delete from account_values where keep=0;");
123 db.execSQL("delete from accounts where keep=0;");
124 // db.execSQL("delete from description_history where keep=0;");
125 db.setTransactionSuccessful();
136 } catch (MalformedURLException e) {
137 error = R.string.err_bad_backend_url;
140 catch (FileNotFoundException e) {
141 error = R.string.err_bad_auth;
144 catch (IOException e) {
145 error = R.string.err_net_io_error;
148 catch (Exception e) {
149 error = R.string.err_net_error;
156 abstract protected void onProgressUpdate(Integer... values);
158 abstract protected void onPostExecute(Void result);