UI & progress bar tweaks

service start/stop in activity onCreate/onDestroy
update continues in background if screen turned off
This commit is contained in:
m2049r 2017-08-13 15:39:11 +02:00
parent 282f00959d
commit c19ee65dd1
4 changed files with 99 additions and 21 deletions

View File

@ -89,6 +89,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/cmake" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />

View File

@ -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);
}
}
});
}

View File

@ -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;
}

View File

@ -11,11 +11,12 @@
<string name="status_walletlist_loading">Loading Wallet List</string>
<string name="status_wallet_loading">Loading Wallet &#8230;</string>
<string name="status_wallet_unloading">Saving Wallet</string>
<string name="status_wallet_connecting">Connecting &#8230;</string>
<string name="prompt_password">Password for</string>
<string name="bad_password">Bad password!</string>
<string name="prompt_daemon_missing">Daemon address must be set!</string>
<string name="prompt_wrong_net">Daemon type does not fit to wallet!</string>
<string name="warn_daemon_unavailable">Warning: cannot reach daemon!</string>
<string name="warn_daemon_unavailable">Cannot connect to daemon!</string>
<string name="panic">Something\'s wrong!</string>
<string name="title_amount">Amount</string>