]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
auto-completion for the transaction description
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / RetrieveAccountsTask.java
1 package net.ktnx.mobileledger;
2
3 import android.content.SharedPreferences;
4 import android.util.Log;
5
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;
15
16 import static net.ktnx.mobileledger.MobileLedgerDB.db;
17
18 abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
19     int error;
20
21     private SharedPreferences pref;
22
23     RetrieveAccountsTask() {
24         error = 0;
25     }
26
27     void setPref(SharedPreferences pref) {
28         this.pref = pref;
29     }
30
31     protected Void doInBackground(Void... params) {
32         try {
33             HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
34             http.setAllowUserInteraction(false);
35             http.setRequestProperty("Accept-Charset", "UTF-8");
36             publishProgress(0);
37             InputStream resp = http.getInputStream();
38             try {
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()));
42                 }
43                 else {
44                     if (db.inTransaction()) throw new AssertionError();
45
46                     db.beginTransaction();
47
48                     try {
49                         db.execSQL("update account_values set keep=0;");
50                         db.execSQL("update accounts set keep=0;");
51
52                         String line;
53                         String last_account_name = null;
54                         BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
55                         // %3A is '='
56                         Pattern account_name_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>");
59                         Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b");
60                         Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\"");
61                         int count = 0;
62                         while ((line = buf.readLine()) != null) {
63                             Matcher m = account_name_re.matcher(line);
64                             if (m.find()) {
65                                 String acct_encoded = m.group(1);
66                                 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
67                                 acct_name = acct_name.replace("\"", "");
68                                 Log.d("account-parser", acct_name);
69
70                                 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
71                                 publishProgress(++count);
72
73                                 last_account_name = acct_name;
74
75                                 continue;
76                             }
77
78                             Matcher tr_m = tr_re.matcher(line);
79                             if (tr_m.find()) {
80                                 Log.d("account-parser", "<tr> - another account expected");
81                                 last_account_name = null;
82                                 continue;
83                             }
84
85                             if (last_account_name != null) {
86                                 m = value_re.matcher(line);
87                                 boolean match_found = false;
88                                 while (m.find()) {
89                                     match_found = true;
90                                     String value = m.group(1);
91                                     String currency = m.group(2);
92                                     if (currency == null) currency = "";
93                                     value = value.replace(',', '.');
94                                     Log.d("db", "curr=" + currency + ", value=" + value);
95                                     db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
96                                             new Object[]{last_account_name, currency, Float.valueOf(value)});
97                                 }
98
99                                 if (match_found) continue;
100                             }
101
102                             m = descriptions_line_re.matcher(line);
103                             if (m.find()) {
104                                 db.execSQL("update description_history set keep=0;");
105                                 m = description_items_re.matcher(line);
106                                 while(m.find()) {
107                                     String description = m.group(1);
108                                     if (description.isEmpty()) continue;
109
110                                     Log.d("db", String.format("Stored description: %s", description));
111                                     db.execSQL("insert or replace into description_history(description, keep) values(?, 1);", new Object[]{description});
112                                 }
113                             }
114                         }
115
116                         db.execSQL("delete from account_values where keep=0;");
117                         db.execSQL("delete from accounts where keep=0;");
118 //                        db.execSQL("delete from description_history where keep=0;");
119                         db.setTransactionSuccessful();
120                     }
121                     finally {
122                         db.endTransaction();
123                     }
124
125                 }
126             }
127             finally {
128                 resp.close();
129             }
130         } catch (MalformedURLException e) {
131             error = R.string.err_bad_backend_url;
132             e.printStackTrace();
133         } catch (IOException e) {
134             error = R.string.err_net_io_error;
135             e.printStackTrace();
136         }
137         catch (Exception e) {
138             error = R.string.err_net_error;
139             e.printStackTrace();
140         }
141
142         return null;
143     }
144
145     abstract protected void onProgressUpdate(Integer... values);
146
147     abstract protected void onPostExecute(Void result);
148 }