Skip to content

Commit 42d5a3b

Browse files
committed
Display 'Connecting...' when connection to daemon is lost
1 parent dd92f7b commit 42d5a3b

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private void onWalletRescan() {
230230
final WalletFragment walletFragment = getWalletFragment();
231231
getWallet().rescanBlockchainAsync();
232232
synced = false;
233-
walletFragment.unsync();
233+
walletFragment.onStartRescan();
234234
invalidateOptionsMenu();
235235
} catch (ClassCastException ex) {
236236
Timber.d(ex.getLocalizedMessage());

app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ public void unsync() {
374374
bSend.setEnabled(false);
375375
}
376376
if (isVisible()) enableAccountsList(false); //otherwise it is enabled in onResume()
377+
}
378+
379+
public void onStartRescan() {
380+
unsync();
377381
firstBlock = 0;
378382
}
379383

@@ -460,10 +464,17 @@ private void updateStatus(Wallet wallet) {
460464
} else {
461465
sync = getString(R.string.status_synced) + " " + formatter.format(wallet.getBlockChainHeight());
462466
ivSynced.setVisibility(View.VISIBLE);
467+
setProgress(-1);
463468
}
464469
} else {
465470
sync = getString(R.string.status_wallet_connecting);
466471
setProgress(101);
472+
ivSynced.setVisibility(View.GONE);
473+
}
474+
if (wallet.isSynchronized()) {
475+
onSynced();
476+
} else {
477+
unsync();
467478
}
468479
setProgress(sync);
469480
// TODO show connected status somewhere

app/src/main/java/com/m2049r/xmrwallet/model/Wallet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ public void setSynchronized() {
283283
this.synced = true;
284284
}
285285

286+
public void setUnsynchronized() {
287+
this.synced = false;
288+
}
289+
286290
public static native String getDisplayAmount(long amount);
287291

288292
public static native long getAmountFromString(String amount);

app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ public void newBlock(long height) {
121121
Timber.d("newBlock() @ %d with observer %s", height, observer);
122122
if (observer != null) {
123123
boolean fullRefresh = false;
124-
updateDaemonState(wallet, wallet.isSynchronized() ? height : 0);
125-
if (!wallet.isSynchronized()) {
124+
boolean receivedNewBlock = true;
125+
boolean updatedWalletConnectionStatus = updateDaemonState(wallet, height, receivedNewBlock);
126+
if (updatedWalletConnectionStatus || !wallet.isSynchronized()) {
126127
updated = true;
127128
// we want to see our transactions as they come in
128129
wallet.refreshHistory();
@@ -146,13 +147,13 @@ public void updated() {
146147
updated = true;
147148
}
148149

149-
public void refreshed() { // this means it's synced
150+
public void refreshed() {
150151
Timber.d("refreshed()");
151152
final Wallet wallet = getWallet();
152153
if (wallet == null) throw new IllegalStateException("No wallet!");
153-
wallet.setSynchronized();
154-
if (updated) {
155-
updateDaemonState(wallet, wallet.getBlockChainHeight());
154+
boolean receivedNewBlock = false;
155+
boolean updatedWalletConnectionStatus = updateDaemonState(wallet, wallet.getBlockChainHeight(), receivedNewBlock);
156+
if (updated || updatedWalletConnectionStatus) {
156157
wallet.refreshHistory();
157158
if (observer != null) {
158159
updated = !observer.onRefreshed(wallet, true);
@@ -164,16 +165,20 @@ public void refreshed() { // this means it's synced
164165
private long lastDaemonStatusUpdate = 0;
165166
private long daemonHeight = 0;
166167
private Wallet.ConnectionStatus connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Disconnected;
167-
private static final long STATUS_UPDATE_INTERVAL = 120000; // 120s (blocktime)
168+
private static final long STATUS_UPDATE_INTERVAL_SYNCED = 120000; // 120s (blocktime)
169+
private static final long STATUS_UPDATE_INTERVAL_SYNCING = 10000; // 10s
168170

169-
private void updateDaemonState(Wallet wallet, long height) {
171+
private boolean updateDaemonState(Wallet wallet, long height, boolean receivedNewBlock) {
172+
Wallet.ConnectionStatus startConnectionStatus = connectionStatus;
170173
long t = System.currentTimeMillis();
171-
if (height > 0) { // if we get a height, we are connected
172-
daemonHeight = height;
174+
if (daemonHeight > 0 && height > 0 && (height > daemonHeight || receivedNewBlock)) {
175+
if (height > daemonHeight)
176+
daemonHeight = height;
173177
connectionStatus = Wallet.ConnectionStatus.ConnectionStatus_Connected;
174178
lastDaemonStatusUpdate = t;
175179
} else {
176-
if (t - lastDaemonStatusUpdate > STATUS_UPDATE_INTERVAL) {
180+
long statusUpdateInterval = wallet.isSynchronized() ? STATUS_UPDATE_INTERVAL_SYNCED : STATUS_UPDATE_INTERVAL_SYNCING;
181+
if (daemonHeight == 0 || t - lastDaemonStatusUpdate > statusUpdateInterval) {
177182
lastDaemonStatusUpdate = t;
178183
// these calls really connect to the daemon - wasting time
179184
daemonHeight = wallet.getDaemonBlockChainHeight();
@@ -185,6 +190,16 @@ private void updateDaemonState(Wallet wallet, long height) {
185190
}
186191
}
187192
}
193+
setWalletSyncState(wallet);
194+
return startConnectionStatus != connectionStatus;
195+
}
196+
197+
public void setWalletSyncState(Wallet wallet) {
198+
if (daemonHeight > 0 && daemonHeight <= wallet.getBlockChainHeight()) {
199+
wallet.setSynchronized();
200+
} else {
201+
wallet.setUnsynchronized();
202+
}
188203
}
189204

190205
public long getDaemonHeight() {

0 commit comments

Comments
 (0)