]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
update accounts table visual upon refreshilg the account data from the backend
[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     public void setPref(SharedPreferences pref) {
23         this.pref = pref;
24     }
25     public RetrieveAccountsTask() {
26         error = 0;
27     }
28
29     protected Void doInBackground(Void... params) {
30         try {
31             HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
32             http.setAllowUserInteraction(false);
33             http.setRequestProperty("Accept-Charset", "UTF-8");
34             publishProgress(0);
35             InputStream resp = http.getInputStream();
36             try {
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));
40                 }
41                 else {
42                     if (db.inTransaction()) throw new AssertionError();
43
44                     db.beginTransaction();
45
46                     try {
47                         db.execSQL("update account_values set keep=0;");
48                         db.execSQL("update accounts set keep=0;");
49
50                         String line;
51                         String last_account_name = null;
52                         BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
53                         // %3A is '='
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>");
57                         int count = 0;
58                         while ((line = buf.readLine()) != null) {
59
60                             Matcher m = re.matcher(line);
61                             if (m.find()) {
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);
66
67                                 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
68                                 publishProgress(++count);
69
70                                 last_account_name = acct_name;
71
72                                 continue;
73                             }
74
75                             Matcher tr_m = tr_re.matcher(line);
76                             if (tr_m.find()) {
77                                 last_account_name = null;
78                                 continue;
79                             }
80
81                             if (last_account_name == null) continue;
82
83                             m = value_re.matcher(line);
84                             while (m.find()) {
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)});
92                             }
93                         }
94
95                         db.execSQL("delete from account_values where keep=0;");
96                         db.execSQL("delete from accounts where keep=0;");
97                         db.setTransactionSuccessful();
98                     }
99                     finally {
100                         db.endTransaction();
101                     }
102
103                 }
104             }
105             finally {
106                 resp.close();
107             }
108         } catch (MalformedURLException e) {
109             error = R.string.err_bad_backend_url;
110             e.printStackTrace();
111         } catch (IOException e) {
112             error = R.string.err_net_io_error;
113             e.printStackTrace();
114         }
115         catch (Exception e) {
116             error = R.string.err_net_error;
117             e.printStackTrace();
118         }
119
120         return null;
121     }
122
123     abstract protected void onProgressUpdate(Integer... values);
124
125     abstract protected void onPostExecute(Void result);
126 }