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, keep) values(?, 1)", new Object[]{acct_name} );
72 publishProgress(++count);
74 last_account_name = acct_name;
79 Matcher tr_m = tr_re.matcher(line);
81 Log.d("account-parser", "<tr> - another account expected");
82 last_account_name = null;
86 if (last_account_name != null) {
87 m = value_re.matcher(line);
88 boolean match_found = false;
91 String value = m.group(1);
92 String currency = m.group(2);
93 if (currency == null) currency = "";
94 value = value.replace(',', '.');
95 Log.d("db", "curr=" + currency + ", value=" + value);
96 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
97 new Object[]{last_account_name, currency, Float.valueOf(value)});
100 if (match_found) continue;
103 m = descriptions_line_re.matcher(line);
105 db.execSQL("update description_history set keep=0;");
106 m = description_items_re.matcher(line);
108 String description = m.group(1);
109 if (description.isEmpty()) continue;
111 Log.d("db", String.format("Stored description: %s", description));
112 db.execSQL("insert or replace into description_history(description, keep) values(?, 1);", new Object[]{description});
117 db.execSQL("delete from account_values where keep=0;");
118 db.execSQL("delete from accounts where keep=0;");
119 // db.execSQL("delete from description_history where keep=0;");
120 db.setTransactionSuccessful();
131 } catch (MalformedURLException e) {
132 error = R.string.err_bad_backend_url;
135 catch (FileNotFoundException e) {
136 error = R.string.err_bad_auth;
139 catch (IOException e) {
140 error = R.string.err_net_io_error;
143 catch (Exception e) {
144 error = R.string.err_net_error;
151 abstract protected void onProgressUpdate(Integer... values);
153 abstract protected void onPostExecute(Void result);