From 4ac01abe102cad34cf916279c480edd10d9a2440 Mon Sep 17 00:00:00 2001 From: j-berman Date: Fri, 25 Mar 2022 12:34:47 -0700 Subject: [PATCH] Display 'Connecting...' when connection to daemon is lost --- .../com/m2049r/xmrwallet/WalletActivity.java | 2 +- .../com/m2049r/xmrwallet/WalletFragment.java | 11 ++++++ .../com/m2049r/xmrwallet/model/Wallet.java | 4 ++ .../xmrwallet/service/WalletService.java | 37 +++++++++++++------ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java index 5ab6ad7b6b..a8d729c0c2 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java @@ -230,7 +230,7 @@ private void onWalletRescan() { final WalletFragment walletFragment = getWalletFragment(); getWallet().rescanBlockchainAsync(); synced = false; - walletFragment.unsync(); + walletFragment.onStartRescan(); invalidateOptionsMenu(); } catch (ClassCastException ex) { Timber.d(ex.getLocalizedMessage()); diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java index 515d2d8a79..05f4d2a308 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java @@ -374,6 +374,10 @@ public void unsync() { bSend.setEnabled(false); } if (isVisible()) enableAccountsList(false); //otherwise it is enabled in onResume() + } + + public void onStartRescan() { + unsync(); firstBlock = 0; } @@ -460,10 +464,17 @@ private void updateStatus(Wallet wallet) { } else { sync = getString(R.string.status_synced) + " " + formatter.format(wallet.getBlockChainHeight()); ivSynced.setVisibility(View.VISIBLE); + setProgress(-1); } } else { sync = getString(R.string.status_wallet_connecting); setProgress(101); + ivSynced.setVisibility(View.GONE); + } + if (wallet.isSynchronized()) { + onSynced(); + } else { + unsync(); } setProgress(sync); // TODO show connected status somewhere diff --git a/app/src/main/java/com/m2049r/xmrwallet/model/Wallet.java b/app/src/main/java/com/m2049r/xmrwallet/model/Wallet.java index e85d0f8526..ef1e0a6e21 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/model/Wallet.java +++ b/app/src/main/java/com/m2049r/xmrwallet/model/Wallet.java @@ -283,6 +283,10 @@ public void setSynchronized() { this.synced = true; } + public void setUnsynchronized() { + this.synced = false; + } + public static native String getDisplayAmount(long amount); public static native long getAmountFromString(String amount); 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 ece003eace..214ecac6ec 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java +++ b/app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java @@ -121,8 +121,9 @@ public void newBlock(long height) { Timber.d("newBlock() @ %d with observer %s", height, observer); if (observer != null) { boolean fullRefresh = false; - updateDaemonState(wallet, wallet.isSynchronized() ? height : 0); - if (!wallet.isSynchronized()) { + boolean receivedNewBlock = true; + boolean updatedWalletConnectionStatus = updateDaemonState(wallet, height, receivedNewBlock); + if (updatedWalletConnectionStatus || !wallet.isSynchronized()) { updated = true; // we want to see our transactions as they come in wallet.refreshHistory(); @@ -146,13 +147,13 @@ public void updated() { updated = true; } - public void refreshed() { // this means it's synced + public void refreshed() { Timber.d("refreshed()"); final Wallet wallet = getWallet(); if (wallet == null) throw new IllegalStateException("No wallet!"); - wallet.setSynchronized(); - if (updated) { - updateDaemonState(wallet, wallet.getBlockChainHeight()); + boolean receivedNewBlock = false; + boolean updatedWalletConnectionStatus = updateDaemonState(wallet, wallet.getBlockChainHeight(), receivedNewBlock); + if (updated || updatedWalletConnectionStatus) { wallet.refreshHistory(); if (observer != null) { updated = !observer.onRefreshed(wallet, true); @@ -164,16 +165,20 @@ public void refreshed() { // this means it's synced 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 static final long STATUS_UPDATE_INTERVAL_SYNCED = 120000; // 120s (blocktime) + private static final long STATUS_UPDATE_INTERVAL_SYNCING = 10000; // 10s - private void updateDaemonState(Wallet wallet, long height) { + private boolean updateDaemonState(Wallet wallet, long height, boolean receivedNewBlock) { + Wallet.ConnectionStatus startConnectionStatus = connectionStatus; long t = System.currentTimeMillis(); - if (height > 0) { // if we get a height, we are connected - daemonHeight = height; + if (daemonHeight > 0 && height > 0 && (height > daemonHeight || receivedNewBlock)) { + if (height > daemonHeight) + daemonHeight = height; connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Connected; lastDaemonStatusUpdate = t; } else { - if (t - lastDaemonStatusUpdate > STATUS_UPDATE_INTERVAL) { + long statusUpdateInterval = wallet.isSynchronized() ? STATUS_UPDATE_INTERVAL_SYNCED : STATUS_UPDATE_INTERVAL_SYNCING; + if (daemonHeight == 0 || t - lastDaemonStatusUpdate > statusUpdateInterval) { lastDaemonStatusUpdate = t; // these calls really connect to the daemon - wasting time daemonHeight = wallet.getDaemonBlockChainHeight(); @@ -185,6 +190,16 @@ private void updateDaemonState(Wallet wallet, long height) { } } } + setWalletSyncState(wallet); + return startConnectionStatus != connectionStatus; + } + + public void setWalletSyncState(Wallet wallet) { + if (daemonHeight > 0 && daemonHeight <= wallet.getBlockChainHeight()) { + wallet.setSynchronized(); + } else { + wallet.setUnsynchronized(); + } } public long getDaemonHeight() {