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 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>");
60 while ((line = buf.readLine()) != null) {
62 Matcher m = re.matcher(line);
64 String acct_encoded = m.group(1);
65 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
66 acct_name = acct_name.replace("\"", "");
67 Log.d("account-parser", acct_name);
69 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
70 publishProgress(++count);
72 last_account_name = acct_name;
77 Matcher tr_m = tr_re.matcher(line);
79 last_account_name = null;
83 if (last_account_name == null) continue;
85 m = value_re.matcher(line);
87 String value = m.group(1);
88 String currency = m.group(2);
89 if(currency == null) currency="";
90 value = value.replace(',', '.');
91 Log.d("db", "curr="+currency+", value="+value);
92 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
93 new Object[]{last_account_name, currency, Float.valueOf(value)});
97 db.execSQL("delete from account_values where keep=0;");
98 db.execSQL("delete from accounts where keep=0;");
99 db.setTransactionSuccessful();
110 } catch (MalformedURLException e) {
111 error = R.string.err_bad_backend_url;
113 } catch (IOException e) {
114 error = R.string.err_net_io_error;
117 catch (Exception e) {
118 error = R.string.err_net_error;
125 abstract protected void onProgressUpdate(Integer... values);
127 abstract protected void onPostExecute(Void result);