]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
pkg-private methods
[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 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                         int count = 0;
60                         while ((line = buf.readLine()) != null) {
61
62                             Matcher m = re.matcher(line);
63                             if (m.find()) {
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);
68
69                                 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
70                                 publishProgress(++count);
71
72                                 last_account_name = acct_name;
73
74                                 continue;
75                             }
76
77                             Matcher tr_m = tr_re.matcher(line);
78                             if (tr_m.find()) {
79                                 last_account_name = null;
80                                 continue;
81                             }
82
83                             if (last_account_name == null) continue;
84
85                             m = value_re.matcher(line);
86                             while (m.find()) {
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)});
94                             }
95                         }
96
97                         db.execSQL("delete from account_values where keep=0;");
98                         db.execSQL("delete from accounts where keep=0;");
99                         db.setTransactionSuccessful();
100                     }
101                     finally {
102                         db.endTransaction();
103                     }
104
105                 }
106             }
107             finally {
108                 resp.close();
109             }
110         } catch (MalformedURLException e) {
111             error = R.string.err_bad_backend_url;
112             e.printStackTrace();
113         } catch (IOException e) {
114             error = R.string.err_net_io_error;
115             e.printStackTrace();
116         }
117         catch (Exception e) {
118             error = R.string.err_net_error;
119             e.printStackTrace();
120         }
121
122         return null;
123     }
124
125     abstract protected void onProgressUpdate(Integer... values);
126
127     abstract protected void onPostExecute(Void result);
128 }