X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2FRetrieveAccountsTask.java;h=16919b336b62fac88f73489492924c0178c35dfd;hb=5a75a6d1ffb0c52d5aadca78aa2d6ee992bfbc84;hp=85f2306cc9b3a5bbe7c39263cfb50e6fb0dfeaa6;hpb=67f722dc9b6989b7574f45c1bb0903ee11326605;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java index 85f2306c..16919b33 100644 --- a/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java @@ -1,54 +1,37 @@ package net.ktnx.mobileledger; import android.content.SharedPreferences; -import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.io.BufferedReader; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.PasswordAuthentication; -import java.net.URL; import java.net.URLDecoder; import java.util.regex.Matcher; import java.util.regex.Pattern; -abstract public class RetrieveAccountsTask extends android.os.AsyncTask { - protected int error; +import static net.ktnx.mobileledger.MobileLedgerDB.db; - SharedPreferences pref; - public void setPref(SharedPreferences pref) { - this.pref = pref; - } - public RetrieveAccountsTask() { +abstract public class RetrieveAccountsTask extends android.os.AsyncTask { + int error; + + private SharedPreferences pref; + + RetrieveAccountsTask() { error = 0; } - protected Void doInBackground(SQLiteDatabase... sqLiteDatabases) { - final String backend_url = pref.getString("backend_url", ""); - final boolean use_auth = pref.getBoolean("backend_use_http_auth", false); - final SQLiteDatabase db = sqLiteDatabases[0]; + void setPref(SharedPreferences pref) { + this.pref = pref; + } + + protected Void doInBackground(Void... params) { try { - Log.d("update_accounts", "Connecting to "+backend_url); - HttpURLConnection http = (HttpURLConnection) new URL(backend_url + "/journal").openConnection(); - if (use_auth) { - final String auth_user = pref.getString("backend_auth_user", ""); - final String auth_password = pref.getString("backend_auth_password", ""); - Authenticator.setDefault(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - Log.d("http-auth", "called"); - return new PasswordAuthentication(auth_user, auth_password.toCharArray()); - } - }); -// final String basic_auth = String.format("Basic %s:%s", auth_user, auth_password); -// http.setRequestProperty("Authorization", basic_auth); - Log.d("update_accounts", "Will auth as "+auth_user+" with password of "+auth_password.length()+" characters"); - } + HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add"); http.setAllowUserInteraction(false); http.setRequestProperty("Accept-Charset", "UTF-8"); publishProgress(0); @@ -56,31 +39,89 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?"); + Pattern tr_re = Pattern.compile(""); + Pattern descriptions_line_re = Pattern.compile("\\bdescriptionsSuggester\\s*=\\s*new\\b"); + Pattern description_items_re = Pattern.compile("\"value\":\"([^\"]+)\""); int count = 0; while ((line = buf.readLine()) != null) { - Matcher m = re.matcher(line); - while (m.find()) { + Matcher m = account_name_re.matcher(line); + if (m.find()) { String acct_encoded = m.group(1); String acct_name = URLDecoder.decode(acct_encoded, "UTF-8"); acct_name = acct_name.replace("\"", ""); Log.d("account-parser", acct_name); - db.execSQL("insert into accounts(name) values(?)", new Object[]{acct_name} ); + db.execSQL("insert or replace into accounts(name, name_upper, " + + "keep) values(?, ?, 1)", + new Object[]{acct_name, acct_name.toUpperCase()}); publishProgress(++count); + + last_account_name = acct_name; + + continue; + } + + Matcher tr_m = tr_re.matcher(line); + if (tr_m.find()) { + Log.d("account-parser", " - another account expected"); + last_account_name = null; + continue; + } + + if (last_account_name != null) { + m = value_re.matcher(line); + boolean match_found = false; + while (m.find()) { + match_found = true; + String value = m.group(1); + String currency = m.group(2); + if (currency == null) currency = ""; + value = value.replace(',', '.'); + Log.d("db", "curr=" + currency + ", value=" + value); + db.execSQL("insert or replace into account_values(account, currency, value, keep) values(?, ?, ?, 1);", + new Object[]{last_account_name, currency, Float.valueOf(value)}); + } + + if (match_found) continue; + } + + m = descriptions_line_re.matcher(line); + if (m.find()) { + db.execSQL("update description_history set keep=0;"); + m = description_items_re.matcher(line); + while(m.find()) { + String description = m.group(1); + if (description.isEmpty()) continue; + + Log.d("db", String.format("Stored description: %s", description)); + db.execSQL("insert or replace into description_history" + + "(description, description_upper, keep) " + + "values(?, ?, 1);", + new Object[]{description, description.toUpperCase()}); + } } } + db.execSQL("delete from account_values where keep=0;"); + db.execSQL("delete from accounts where keep=0;"); +// db.execSQL("delete from description_history where keep=0;"); db.setTransactionSuccessful(); } finally { @@ -95,7 +136,12 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask