From eb198c42090580a7bc2e72e1ba4738eda914ce3e Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 2 Dec 2018 10:57:35 +0000 Subject: [PATCH] refresh account list upon start in the future this should be done on preferences change or on request --- .../ktnx/mobileledger/LatestTransactions.java | 29 ++++- .../mobileledger/RetrieveAccountsTask.java | 113 ++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java diff --git a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java index 98391f29..8e0269f5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java +++ b/app/src/main/java/net/ktnx/mobileledger/LatestTransactions.java @@ -3,6 +3,7 @@ package net.ktnx.mobileledger; import android.content.Intent; import android.content.pm.PackageInfo; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; @@ -13,7 +14,18 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; +import java.util.Date; + +import static net.ktnx.mobileledger.MobileLedgerDB.db; + public class LatestTransactions extends AppCompatActivity { + DrawerLayout drawer; + + private static Date account_list_last_updated; + private static boolean account_list_needs_update = true; + public static void preferences_changed() { + account_list_needs_update = true; + } @Override protected void onCreate(Bundle savedInstanceState) { @@ -22,7 +34,7 @@ public class LatestTransactions extends AppCompatActivity { Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); - DrawerLayout drawer = findViewById(R.id.drawer_layout); + drawer = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); @@ -35,6 +47,8 @@ public class LatestTransactions extends AppCompatActivity { ver.setText(pi.versionName); } catch (Exception e) { } + + update_accounts(); } public void fab_new_transaction_clicked(View view) { @@ -84,4 +98,17 @@ public class LatestTransactions extends AppCompatActivity { return super.onOptionsItemSelected(item); } + private void prepare_db() { + MobileLedgerDB.setDb_filename(this.getApplicationInfo().deviceProtectedDataDir + "/" + MobileLedgerDB.DATABASE_NAME); + MobileLedgerDB.initDB(); + } + private void update_accounts() { + prepare_db(); + + RetrieveAccountsTask task = new RetrieveAccountsTask(); + + task.setPref(PreferenceManager.getDefaultSharedPreferences(this)); + task.execute(db); + + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java b/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java new file mode 100644 index 00000000..60e6c456 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/RetrieveAccountsTask.java @@ -0,0 +1,113 @@ +package net.ktnx.mobileledger; + +import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; +import android.util.Log; + +import java.io.BufferedReader; +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; + +public class RetrieveAccountsTask extends android.os.AsyncTask { + private int error; + + SharedPreferences pref; + public void setPref(SharedPreferences pref) { + this.pref = pref; + } + public 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]; + 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"); + } + http.setAllowUserInteraction(true); + http.setRequestProperty("Accept-Charset", "UTF-8"); + InputStream resp = http.getInputStream(); + try { + Log.d("update_accounts", String.valueOf(http.getResponseCode())); + if (http.getResponseCode() != 200) { + error = R.string.err_http_error; + } + else { + db.beginTransaction(); + db.execSQL("delete from accounts;"); + + try { + String line; + BufferedReader buf = new BufferedReader(new InputStreamReader(resp, "UTF-8")); + // %3A is '=' + Pattern re = Pattern.compile('"' + backend_url + "/register\\?q=inacct%3A([a-zA-Z0-9%]+)\\\""); + while ((line = buf.readLine()) != null) { + Matcher m = re.matcher(line); + while (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.setTransactionSuccessful(); + } + finally { + db.endTransaction(); + } + + } + } + finally { + resp.close(); + } + } catch (MalformedURLException e) { + error = R.string.err_bad_backend_url; + e.printStackTrace(); + } catch (IOException e) { + error = R.string.err_net_io_error; + e.printStackTrace(); + } + catch (Exception e) { + error = R.string.err_net_error; + e.printStackTrace(); + } + + return null; + } + + protected void onPostExecute(Void result) { + if (error != 0) + Log.e("async-http", String.valueOf(error)); + else + Log.d("async-http", "Accounts updated successfuly"); + } +} -- 2.39.5