1 package net.ktnx.mobileledger;
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.database.sqlite.SQLiteDatabase;
6 import android.util.Log;
8 import java.io.BufferedReader;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 import java.net.HttpURLConnection;
14 import java.net.MalformedURLException;
15 import java.net.URLDecoder;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
19 abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
22 private SharedPreferences pref;
23 private final Context mContext;
25 RetrieveAccountsTask(Context context) {
30 void setPref(SharedPreferences pref) {
34 protected Void doInBackground(Void... params) {
36 HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
37 http.setAllowUserInteraction(false);
38 http.setRequestProperty("Accept-Charset", "UTF-8");
40 try(MobileLedgerDatabase dbh = new MobileLedgerDatabase(mContext)) {
41 try(SQLiteDatabase db = dbh.getWritableDatabase()) {
42 try (InputStream resp = http.getInputStream()) {
43 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
44 if (http.getResponseCode() != 200) {
45 throw new IOException(
46 String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
49 if (db.inTransaction()) throw new AssertionError();
51 db.beginTransaction();
54 db.execSQL("update account_values set keep=0;");
55 db.execSQL("update accounts set keep=0;");
58 String last_account_name = null;
60 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
62 Pattern account_name_re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
63 Pattern value_re = Pattern.compile(
64 "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
65 Pattern tr_re = Pattern.compile("</tr>");
66 Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
67 Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
69 while ((line = buf.readLine()) != null) {
70 Matcher m = account_name_re.matcher(line);
72 String acct_encoded = m.group(1);
73 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
74 acct_name = acct_name.replace("\"", "");
75 Log.d("account-parser", acct_name);
78 "insert or replace into accounts(name, name_upper, "
79 + "keep) values(?, ?, 1)",
80 new Object[]{acct_name, acct_name.toUpperCase()});
81 publishProgress(++count);
83 last_account_name = acct_name;
88 Matcher tr_m = tr_re.matcher(line);
90 Log.d("account-parser", "<tr> - another account expected");
91 last_account_name = null;
95 if (last_account_name != null) {
96 m = value_re.matcher(line);
97 boolean match_found = false;
100 String value = m.group(1);
101 String currency = m.group(2);
102 if (currency == null) currency = "";
103 value = value.replace(',', '.');
104 Log.d("db", "curr=" + currency + ", value=" + value);
106 "insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
107 new Object[]{last_account_name, currency, Float.valueOf(value)
111 if (match_found) continue;
114 m = descriptions_line_re.matcher(line);
116 db.execSQL("update description_history set keep=0;");
117 m = description_items_re.matcher(line);
119 String description = m.group(1);
120 if (description.isEmpty()) continue;
122 Log.d("db", String.format("Stored description: %s",
124 db.execSQL("insert or replace into description_history"
125 + "(description, description_upper, keep) " + "values(?, ?, 1);",
126 new Object[]{description, description.toUpperCase()
132 db.execSQL("delete from account_values where keep=0;");
133 db.execSQL("delete from accounts where keep=0;");
134 // db.execSQL("delete from description_history where keep=0;");
135 db.setTransactionSuccessful();
145 } catch (MalformedURLException e) {
146 error = R.string.err_bad_backend_url;
149 catch (FileNotFoundException e) {
150 error = R.string.err_bad_auth;
153 catch (IOException e) {
154 error = R.string.err_net_io_error;
157 catch (Exception e) {
158 error = R.string.err_net_error;
165 abstract protected void onProgressUpdate(Integer... values);
167 abstract protected void onPostExecute(Void result);