package net.ktnx.mobileledger;
+import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
+import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
+import android.widget.ProgressBar;
+import android.widget.TextView;
import java.util.Date;
+import static android.view.View.GONE;
import static net.ktnx.mobileledger.MobileLedgerDB.db;
public class LatestTransactions extends AppCompatActivity {
private void update_accounts() {
prepare_db();
- RetrieveAccountsTask task = new RetrieveAccountsTask();
+ Activity activity = this;
+
+ ProgressBar pb = findViewById(R.id.progressBar);
+ TextView pt = findViewById(R.id.textProgress);
+ pb.setIndeterminate(true);
+
+ RetrieveAccountsTask task = new RetrieveAccountsTask() {
+ @Override
+ protected void onProgressUpdate(Integer... values) {
+ if ( values[0] == 0 )
+ pt.setText(R.string.progress_connecting);
+ else
+ pt.setText(String.format(getResources().getString(R.string.progress_N_accounts_loaded), values[0]));
+ }
+
+ @Override
+ protected void onPostExecute(Void result) {
+ pb.setVisibility(GONE);
+ pt.setVisibility(GONE);
+ if (this.error != 0)
+ Snackbar.make(drawer, activity.getResources().getString(this.error), Snackbar.LENGTH_LONG );
+ }
+ };
task.setPref(PreferenceManager.getDefaultSharedPreferences(this));
task.execute(db);
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, Void, Void> {
- private int error;
+abstract public class RetrieveAccountsTask extends android.os.AsyncTask<SQLiteDatabase, Integer, Void> {
+ protected int error;
SharedPreferences pref;
public void setPref(SharedPreferences pref) {
// 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.setAllowUserInteraction(false);
http.setRequestProperty("Accept-Charset", "UTF-8");
+ publishProgress(0);
InputStream resp = http.getInputStream();
try {
Log.d("update_accounts", String.valueOf(http.getResponseCode()));
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%]+)\\\"");
+ int count = 0;
while ((line = buf.readLine()) != null) {
Matcher m = re.matcher(line);
while (m.find()) {
Log.d("account-parser", acct_name);
db.execSQL("insert into accounts(name) values(?)", new Object[]{acct_name} );
+ publishProgress(++count);
}
}
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");
- }
+ abstract protected void onProgressUpdate(Integer... values);
+
+ abstract protected void onPostExecute(Void result);
}
tools:showIn="@layout/app_bar_latest_transactions">
<TextView
+ android:id="@+id/textProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:text="Hello World!"
+ android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent" />
+
+ <ProgressBar
+ android:id="@+id/progressBar"
+ style="?android:attr/progressBarStyle"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="8dp"
+ android:layout_marginEnd="8dp"
+ android:layout_marginBottom="16dp"
+ android:visibility="visible"
+ app:layout_constraintBottom_toTopOf="@+id/textProgress"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<string name="err_net_io_error">Network I/O error</string>
<string name="err_http_error">HTTP error</string>
<string name="err_net_error">Network error</string>
+ <string name="progress_connecting">Connecting…</string>
+ <string name="progress_N_accounts_loaded">%d accounts loaded</string>
</resources>