]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java
machinery for retrieving transaction journal from hledger-web
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / RetrieveAccountsTask.java
index 21963dd6b37abbbe0b85cc68af4682ab6cbdaf37..6e66453b38a6021bd54b8096d147db63e16c7e1e 100644 (file)
@@ -1,6 +1,22 @@
+/*
+ * Copyright © 2018 Damyan Ivanov.
+ * This file is part of Mobile-Ledger.
+ * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * under the term of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
+ *
+ * Mobile-Ledger is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+ */
+
 package net.ktnx.mobileledger;
 
-import android.content.Context;
 import android.content.SharedPreferences;
 import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
@@ -10,19 +26,20 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.lang.ref.WeakReference;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URLDecoder;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
+class RetrieveAccountsTask extends android.os.AsyncTask<Void, Integer, Void> {
     int error;
 
     private SharedPreferences pref;
-    private final Context mContext;
+    WeakReference<AccountSummary> mContext;
 
-    RetrieveAccountsTask(Context context) {
+    RetrieveAccountsTask(WeakReference<AccountSummary> context) {
         mContext = context;
         error = 0;
     }
@@ -34,10 +51,8 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, In
     protected Void doInBackground(Void... params) {
         try {
             HttpURLConnection http = NetworkUtil.prepare_connection( pref, "add");
-            http.setAllowUserInteraction(false);
-            http.setRequestProperty("Accept-Charset", "UTF-8");
             publishProgress(0);
-            try(MobileLedgerDatabase dbh = new MobileLedgerDatabase(mContext)) {
+            try(MobileLedgerDatabase dbh = new MobileLedgerDatabase(mContext.get())) {
                 try(SQLiteDatabase db = dbh.getWritableDatabase()) {
                     try (InputStream resp = http.getInputStream()) {
                         Log.d("update_accounts", String.valueOf(http.getResponseCode()));
@@ -74,10 +89,7 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, In
                                         acct_name = acct_name.replace("\"", "");
                                         Log.d("account-parser", acct_name);
 
-                                        db.execSQL(
-                                                "insert or replace into accounts(name, name_upper, "
-                                                        + "keep) values(?, ?, 1)",
-                                                new Object[]{acct_name, acct_name.toUpperCase()});
+                                        addAccount(db, acct_name);
                                         publishProgress(++count);
 
                                         last_account_name = acct_name;
@@ -162,7 +174,23 @@ abstract public class RetrieveAccountsTask extends android.os.AsyncTask<Void, In
         return null;
     }
 
-    abstract protected void onProgressUpdate(Integer... values);
+    private void addAccount(SQLiteDatabase db, String name) {
+        do {
+            LedgerAccount acc = new LedgerAccount(name);
+            db.execSQL(
+                    "update accounts set level = ?, keep = 1 where name = ?",
+                    new Object[]{acc.getLevel(), name});
+            db.execSQL("insert into accounts(name, name_upper, parent_name, level) select ?,?,"
+                            + "?,? " + "where (select changes() = 0)",
+                    new Object[]{name, name.toUpperCase(), acc.getParentName(), acc.getLevel()});
+            name = acc.getParentName();
+        } while (name != null);
+    }
+    @Override
+    protected void onPostExecute(Void result) {
+        AccountSummary ctx = mContext.get();
+        if (ctx == null) return;
+        ctx.onAccountRefreshDone(this.error);
+    }
 
-    abstract protected void onPostExecute(Void result);
 }