From c19ee65dd11be2abff7fab5147bec05c98a769fc Mon Sep 17 00:00:00 2001
From: m2049r <30435443+m2049r@users.noreply.github.com>
Date: Sun, 13 Aug 2017 15:39:11 +0200
Subject: [PATCH] UI & progress bar tweaks service start/stop in activity
onCreate/onDestroy update continues in background if screen turned off
---
app/app.iml | 1 +
.../com/m2049r/xmrwallet/WalletActivity.java | 43 +++++++----
.../xmrwallet/service/WalletService.java | 73 +++++++++++++++++--
app/src/main/res/values/strings.xml | 3 +-
4 files changed, 99 insertions(+), 21 deletions(-)
diff --git a/app/app.iml b/app/app.iml
index b6bdf79..25bc19b 100644
--- a/app/app.iml
+++ b/app/app.iml
@@ -89,6 +89,7 @@
+
diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java
index 336c827..9d92711 100644
--- a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java
+++ b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java
@@ -58,6 +58,10 @@ public class WalletActivity extends AppCompatActivity
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart()");
+ this.synced = false; // init syncing logic
+ }
+
+ private void startWalletService() {
acquireWakeLock();
Bundle extras = getIntent().getExtras();
if (extras != null) {
@@ -67,6 +71,8 @@ public class WalletActivity extends AppCompatActivity
} else {
throw new IllegalStateException("No extras passed! Panic!");
}
+
+ onProgress(getString(R.string.status_wallet_loading));
showProgress();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@@ -75,7 +81,11 @@ public class WalletActivity extends AppCompatActivity
onProgress(10); // look like we are working!
}
}, 250);
- //Log.d(TAG, "onStart() done.");
+ }
+
+ private void stopWalletService() {
+ releaseWakeLock();
+ disconnectWalletService();
}
private String title = null;
@@ -95,15 +105,13 @@ public class WalletActivity extends AppCompatActivity
@Override
protected void onStop() {
Log.d(TAG, "onStop()");
- releaseWakeLock();
- disconnectWalletService();
- this.synced = false;
super.onStop();
}
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy()");
+ stopWalletService();
super.onDestroy();
}
@@ -129,6 +137,7 @@ public class WalletActivity extends AppCompatActivity
recyclerView.setAdapter(adapter);
setTitle(getString(R.string.status_wallet_loading));
+ startWalletService();
//Log.d(TAG, "onCreate() done.");
}
@@ -136,33 +145,34 @@ public class WalletActivity extends AppCompatActivity
private boolean synced = false;
private void updateStatus(Wallet wallet) {
+ Log.d(TAG, "updateStatus()");
setActivityTitle(wallet);
final TextView balanceView = (TextView) findViewById(R.id.tvBalance);
final TextView unlockedView = (TextView) findViewById(R.id.tvUnlockedBalance);
final TextView syncProgressView = (TextView) findViewById(R.id.tvBlockHeightProgress);
final TextView connectionStatusView = (TextView) findViewById(R.id.tvConnectionStatus);
-
- //Wallet wallet = getWallet();
balanceView.setText(Wallet.getDisplayAmount(wallet.getBalance()));
unlockedView.setText(Wallet.getDisplayAmount(wallet.getUnlockedBalance()));
String sync = "";
- // TODO: getConnectionStatus() blocks as it tries to connect - this is bad in the UI thread!
- if (wallet.getConnectionStatus() == Wallet.ConnectionStatus.ConnectionStatus_Connected) {
+ if (mBoundService == null) throw new IllegalStateException("WalletService not bound.");
+ Wallet.ConnectionStatus daemonConnected = mBoundService.getConnectionStatus();
+ if (daemonConnected == Wallet.ConnectionStatus.ConnectionStatus_Connected) {
+ long daemonHeight = mBoundService.getDaemonHeight();
if (!wallet.isSynchronized()) {
- long n = wallet.getDaemonBlockChainHeight() - wallet.getBlockChainHeight();
+ long n = daemonHeight - wallet.getBlockChainHeight();
sync = n + " " + getString(R.string.status_remaining);
if (firstBlock == 0) {
firstBlock = wallet.getBlockChainHeight();
}
- int x = 100 - Math.round(100f * n / (1f * wallet.getDaemonBlockChainHeight() - firstBlock));
- //Log.d(TAG, n + "/" + (wallet.getDaemonBlockChainHeight() - firstBlock));
+ int x = 100 - Math.round(100f * n / (1f * daemonHeight - firstBlock));
onProgress(getString(R.string.status_syncing) + " " + sync);
+ if (x == 0) x = -1;
onProgress(x);
} else {
sync = getString(R.string.status_synced) + ": " + wallet.getBlockChainHeight();
if (!synced) {
hideProgress();
- saveWallet(); // save ONLY on first sync
+ saveWallet(); // save on first sync
// the usual use case is:
// open the wallet, wait for sync, check balance, close app
// even if we wait for new transactions, they will be synced and saved next time
@@ -174,7 +184,7 @@ public class WalletActivity extends AppCompatActivity
}
String net = (wallet.isTestNet() ? getString(R.string.connect_testnet) : getString(R.string.connect_mainnet));
syncProgressView.setText(sync);
- connectionStatusView.setText(net + " " + wallet.getConnectionStatus().toString().substring(17));
+ connectionStatusView.setText(net + " " + daemonConnected.toString().substring(17));
}
@Override
@@ -364,7 +374,12 @@ public class WalletActivity extends AppCompatActivity
runOnUiThread(new Runnable() {
public void run() {
ProgressBar progress = (ProgressBar) findViewById(R.id.pbProgress);
- progress.setProgress(n);
+ if (n >= 0) {
+ progress.setIndeterminate(false);
+ progress.setProgress(n);
+ } else {
+ progress.setIndeterminate(true);
+ }
}
});
}
diff --git a/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java b/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java
index 4cdc4f0..6528301 100644
--- a/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java
+++ b/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java
@@ -27,6 +27,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;
+import android.widget.Toast;
import com.m2049r.xmrwallet.R;
import com.m2049r.xmrwallet.model.Wallet;
@@ -97,15 +98,27 @@ public class WalletService extends Service {
}
long lastBlockTime = 0;
+ int lastTxCount = 0;
public void newBlock(long height) {
if (wallet == null) throw new IllegalStateException("No wallet!");
// don't flood with an update for every block ...
if (lastBlockTime < System.currentTimeMillis() - 2000) {
- Log.d(TAG, "newBlock() @" + height + "with observer " + observer);
+ Log.d(TAG, "newBlock() @" + height + " with observer " + observer);
lastBlockTime = System.currentTimeMillis();
if (observer != null) {
- observer.onRefreshed(wallet, false);
+ boolean fullRefresh = false;
+ updateDaemonState(wallet, wallet.isSynchronized() ? height : 0);
+ if (!wallet.isSynchronized()) {
+ // we want to see our transactions as they come in
+ wallet.getHistory().refresh();
+ int txCount = wallet.getHistory().getCount();
+ if (txCount > lastTxCount) {
+ lastTxCount = txCount;
+ fullRefresh = true;
+ }
+ }
+ observer.onRefreshed(wallet, fullRefresh);
}
}
}
@@ -118,9 +131,10 @@ public class WalletService extends Service {
public void refreshed() {
if (wallet == null) throw new IllegalStateException("No wallet!");
- Log.d(TAG, "refreshed() " + wallet.getBalance() + " sync=" + wallet.isSynchronized() + "with observer " + observer);
+ Log.d(TAG, "refreshed() " + wallet.getBalance() + " sync=" + wallet.isSynchronized() + " with observer " + observer);
if (updated) {
if (observer != null) {
+ updateDaemonState(wallet, 0);
wallet.getHistory().refresh();
observer.onRefreshed(wallet, true);
updated = false;
@@ -129,6 +143,42 @@ public class WalletService extends Service {
}
}
+ private long lastDaemonStatusUpdate = 0;
+ private long daemonHeight = 0;
+ private Wallet.ConnectionStatus connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Disconnected;
+ private static final long STATUS_UPDATE_INTERVAL = 120000; // 120s (blocktime)
+
+ private void updateDaemonState(Wallet wallet, long height) {
+ long t = System.currentTimeMillis();
+ if (height > 0) { // if we get a height, we are connected
+ daemonHeight = height;
+ connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Connected;
+ lastDaemonStatusUpdate = t;
+ } else {
+ if (t - lastDaemonStatusUpdate > STATUS_UPDATE_INTERVAL) {
+ lastDaemonStatusUpdate = t;
+ // these calls really connect to the daemon - wasting time
+ daemonHeight = wallet.getDaemonBlockChainHeight();
+ if (daemonHeight > 0) {
+ // if we get a valid height, the obviously we are connected
+ connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Connected;
+ } else {
+ // TODO: or connectionStatus = wallet.getConnectionStatus(); ?
+ connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Disconnected;
+ }
+ }
+ }
+ //Log.d(TAG, "updated daemon status: " + daemonHeight + "/" + connectionStatus.toString());
+ }
+
+ public long getDaemonHeight() {
+ return this.daemonHeight;
+ }
+
+ public Wallet.ConnectionStatus getConnectionStatus() {
+ return this.connectionStatus;
+ }
+
/////////////////////////////////////////////
// communication back to client (activity) //
/////////////////////////////////////////////
@@ -296,12 +346,18 @@ public class WalletService extends Service {
listener.start();
showProgress(95);
}
- Log.d(TAG, "start() done");
+ Log.d(TAG, "start() notify obeserver first time");
if (observer != null) {
Wallet myWallet = getWallet();
+ showProgress(getString(R.string.status_wallet_connecting));
+ showProgress(-1);
+ updateDaemonState(myWallet, 0);
myWallet.getHistory().refresh();
- observer.onRefreshed(myWallet, true);
+ if (observer != null) { // TODO this could still happen - need to sync threads
+ observer.onRefreshed(myWallet, true);
+ }
}
+ Log.d(TAG, "start() done");
}
public void stop() {
@@ -309,8 +365,13 @@ public class WalletService extends Service {
setObserver(null); // in case it was not reset already
if (listener != null) {
listener.stop();
+ Wallet myWallet = getWallet();
+// if (!myWallet.isSynchronized()) { // save only if NOT synced (to continue later)
+// Log.d(TAG, "stop() saving");
+// myWallet.store();
+// }
Log.d(TAG, "stop() closing");
- listener.getWallet().close();
+ myWallet.close();
Log.d(TAG, "stop() closed");
listener = null;
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 0ad67e4..e616d69 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -11,11 +11,12 @@
Loading Wallet List
Loading Wallet …
Saving Wallet
+ Connecting …
Password for
Bad password!
Daemon address must be set!
Daemon type does not fit to wallet!
- Warning: cannot reach daemon!
+ Cannot connect to daemon!
Something\'s wrong!
Amount