]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
maybe better network error reporting
[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     void setPref(SharedPreferences pref) {
30         this.pref = pref;
31     }
32
33     protected Void doInBackground(Void... params) {
34         try {
35             HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
36             http.setAllowUserInteraction(false);
37             http.setRequestProperty("Accept-Charset", "UTF-8");
38             publishProgress(0);
39             InputStream resp = http.getInputStream();
40             try {
41                 Log.d("update_accounts", String.valueOf(http.getResponseCode()));
42                 if (http.getResponseCode() != 200) {
43                     throw new IOException(String.format("HTTP error: %d %s", http.getResponseCode(), http.getResponseMessage()));
44                 }
45                 else {
46                     if (db.inTransaction()) throw new AssertionError();
47
48                     db.beginTransaction();
49
50                     try {
51                         db.execSQL("update account_values set keep=0;");
52                         db.execSQL("update accounts set keep=0;");
53
54                         String line;
55                         String last_account_name = null;
56                         BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8"));
57                         // %3A is '='
58                         Pattern re = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
59                         Pattern value_re = Pattern.compile("<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
60                         Pattern tr_re = Pattern.compile("</tr>");
61                         int count = 0;
62                         while ((line = buf.readLine()) != null) {
63
64                             Matcher m = re.matcher(line);
65                             if (m.find()) {
66                                 String acct_encoded = m.group(1);
67                                 String acct_name = URLDecoder.decode(acct_encoded, "UTF-8");
68                                 acct_name = acct_name.replace("\"", "");
69                                 Log.d("account-parser", acct_name);
70
71                                 db.execSQL("insert or replace into accounts(name, keep) values(?, 1)", new Object[]{acct_name} );
72                                 publishProgress(++count);
73
74                                 last_account_name = acct_name;
75
76                                 continue;
77                             }
78
79                             Matcher tr_m = tr_re.matcher(line);
80                             if (tr_m.find()) {
81                                 last_account_name = null;
82                                 continue;
83                             }
84
85                             if (last_account_name == null) continue;
86
87                             m = value_re.matcher(line);
88                             while (m.find()) {
89                                 String value = m.group(1);
90                                 String currency = m.group(2);
91                                 if(currency == null) currency="";
92                                 value = value.replace(',', '.');
93                                 Log.d("db", "curr="+currency+", value="+value);
94                                 db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);",
95                                         new Object[]{last_account_name, currency, Float.valueOf(value)});
96                             }
97                         }
98
99                         db.execSQL("delete from account_values where keep=0;");
100                         db.execSQL("delete from accounts where keep=0;");
101                         db.setTransactionSuccessful();
102                     }
103                     finally {
104                         db.endTransaction();
105                     }
106
107                 }
108             }
109             finally {
110                 resp.close();
111             }
112         } catch (MalformedURLException e) {
113             error = R.string.err_bad_backend_url;
114             e.printStackTrace();
115         } catch (IOException e) {
116             error = R.string.err_net_io_error;
117             e.printStackTrace();
118         }
119         catch (Exception e) {
120             error = R.string.err_net_error;
121             e.printStackTrace();
122         }
123
124         return null;
125     }
126
127     abstract protected void onProgressUpdate(Integer... values);
128
129     abstract protected void onPostExecute(Void result);
130 }