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;
22 public void setPref(SharedPreferences pref) {
25 public RetrieveAccountsTask() {
29 protected Void doInBackground(Void... params) {
31 HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
32 http.setAllowUserInteraction(false);
33 http.setRequestProperty("Accept-Charset", "UTF-8");
35 InputStream resp = http.getInputStream();
37 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
38 if (http.getResponseCode() != 200) {
39 throw new IOException(String.valueOf(R.string.err_http_error));
42 if (db.inTransaction()) throw new AssertionError();
44 db.beginTransaction();
47 db.execSQL("update account_values set keep=0;");
48 db.execSQL("update accounts set keep=0;");
51 String last_account_name = null;
52 BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
54 Pattern re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
55 Pattern value_re = Pattern.compile("<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
56 Pattern tr_re = Pattern.compile("</tr>");
58 while ((line = buf.readLine()) != null) {
60 Matcher m = re.matcher(line);
62 String acct_encoded = m.group(1);
63 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
64 acct_name = acct_name.replace("\"", "");
65 Log.d("account-parser", acct_name);
67 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
68 publishProgress(++count);
70 last_account_name = acct_name;
75 Matcher tr_m = tr_re.matcher(line);
77 last_account_name = null;
81 if (last_account_name == null) continue;
83 m = value_re.matcher(line);
85 String value = m.group(1);
86 String currency = m.group(2);
87 if(currency == null) currency="";
88 value = value.replace(',', '.');
89 Log.d("db", "curr="+currency+", value="+value);
90 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
91 new Object[]{last_account_name, currency, Float.valueOf(value)});
95 db.execSQL("delete from account_values where keep=0;");
96 db.execSQL("delete from accounts where keep=0;");
97 db.setTransactionSuccessful();
108 } catch (MalformedURLException e) {
109 error = R.string.err_bad_backend_url;
111 } catch (IOException e) {
112 error = R.string.err_net_io_error;
115 catch (Exception e) {
116 error = R.string.err_net_error;
123 abstract protected void onProgressUpdate(Integer... values);
125 abstract protected void onPostExecute(Void result);