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