Commit 208ae6a4 authored by akwizgran's avatar akwizgran

Show recently polled pending contacts as "connecting".

parent fe1df6da
...@@ -6,17 +6,24 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault; ...@@ -6,17 +6,24 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
import static org.briarproject.bramble.api.contact.PendingContactState.CONNECTING;
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
@Immutable @Immutable
@NotNullByDefault @NotNullByDefault
class PendingContactItem { class PendingContactItem {
static final int POLL_DURATION_MS = 15_000;
private final PendingContact pendingContact; private final PendingContact pendingContact;
private final PendingContactState state; private final PendingContactState state;
private final long lastPoll;
PendingContactItem(PendingContact pendingContact, PendingContactItem(PendingContact pendingContact,
PendingContactState state) { PendingContactState state, long lastPoll) {
this.pendingContact = pendingContact; this.pendingContact = pendingContact;
this.state = state; this.state = state;
this.lastPoll = lastPoll;
} }
PendingContact getPendingContact() { PendingContact getPendingContact() {
...@@ -24,6 +31,10 @@ class PendingContactItem { ...@@ -24,6 +31,10 @@ class PendingContactItem {
} }
PendingContactState getState() { PendingContactState getState() {
if (state == WAITING_FOR_CONNECTION &&
System.currentTimeMillis() - lastPoll < POLL_DURATION_MS) {
return CONNECTING;
}
return state; return state;
} }
} }
...@@ -23,6 +23,7 @@ import javax.annotation.Nullable; ...@@ -23,6 +23,7 @@ import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import static org.briarproject.bramble.api.contact.PendingContactState.FAILED; import static org.briarproject.bramble.api.contact.PendingContactState.FAILED;
import static org.briarproject.briar.android.contact.add.remote.PendingContactItem.POLL_DURATION_MS;
@MethodsNotNullByDefault @MethodsNotNullByDefault
@ParametersNotNullByDefault @ParametersNotNullByDefault
...@@ -69,7 +70,7 @@ public class PendingContactListActivity extends BriarActivity ...@@ -69,7 +70,7 @@ public class PendingContactListActivity extends BriarActivity
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
list.startPeriodicUpdate(); list.startPeriodicUpdate(POLL_DURATION_MS);
} }
@Override @Override
......
...@@ -18,6 +18,9 @@ import org.briarproject.bramble.api.event.Event; ...@@ -18,6 +18,9 @@ import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventBus; import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.event.EventListener; import org.briarproject.bramble.api.event.EventListener;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TorConstants;
import org.briarproject.bramble.api.rendezvous.RendezvousPoller;
import org.briarproject.bramble.api.rendezvous.event.RendezvousPollEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
...@@ -41,6 +44,7 @@ public class PendingContactListViewModel extends AndroidViewModel ...@@ -41,6 +44,7 @@ public class PendingContactListViewModel extends AndroidViewModel
@DatabaseExecutor @DatabaseExecutor
private final Executor dbExecutor; private final Executor dbExecutor;
private final ContactManager contactManager; private final ContactManager contactManager;
private final RendezvousPoller rendezvousPoller;
private final EventBus eventBus; private final EventBus eventBus;
private final MutableLiveData<Collection<PendingContactItem>> private final MutableLiveData<Collection<PendingContactItem>>
...@@ -49,10 +53,13 @@ public class PendingContactListViewModel extends AndroidViewModel ...@@ -49,10 +53,13 @@ public class PendingContactListViewModel extends AndroidViewModel
@Inject @Inject
PendingContactListViewModel(Application application, PendingContactListViewModel(Application application,
@DatabaseExecutor Executor dbExecutor, @DatabaseExecutor Executor dbExecutor,
ContactManager contactManager, EventBus eventBus) { ContactManager contactManager,
RendezvousPoller rendezvousPoller,
EventBus eventBus) {
super(application); super(application);
this.dbExecutor = dbExecutor; this.dbExecutor = dbExecutor;
this.contactManager = contactManager; this.contactManager = contactManager;
this.rendezvousPoller = rendezvousPoller;
this.eventBus = eventBus; this.eventBus = eventBus;
this.eventBus.addListener(this); this.eventBus.addListener(this);
loadPendingContacts(); loadPendingContacts();
...@@ -67,7 +74,8 @@ public class PendingContactListViewModel extends AndroidViewModel ...@@ -67,7 +74,8 @@ public class PendingContactListViewModel extends AndroidViewModel
@Override @Override
public void eventOccurred(Event e) { public void eventOccurred(Event e) {
if (e instanceof PendingContactStateChangedEvent || if (e instanceof PendingContactStateChangedEvent ||
e instanceof PendingContactRemovedEvent) { e instanceof PendingContactRemovedEvent ||
e instanceof RendezvousPollEvent) {
loadPendingContacts(); loadPendingContacts();
} }
} }
...@@ -75,12 +83,14 @@ public class PendingContactListViewModel extends AndroidViewModel ...@@ -75,12 +83,14 @@ public class PendingContactListViewModel extends AndroidViewModel
private void loadPendingContacts() { private void loadPendingContacts() {
dbExecutor.execute(() -> { dbExecutor.execute(() -> {
try { try {
long lastPoll =
rendezvousPoller.getLastPollTime(TorConstants.ID);
Collection<Pair<PendingContact, PendingContactState>> pairs = Collection<Pair<PendingContact, PendingContactState>> pairs =
contactManager.getPendingContacts(); contactManager.getPendingContacts();
List<PendingContactItem> items = new ArrayList<>(pairs.size()); List<PendingContactItem> items = new ArrayList<>(pairs.size());
for (Pair<PendingContact, PendingContactState> p : pairs) { for (Pair<PendingContact, PendingContactState> p : pairs) {
items.add(new PendingContactItem(p.getFirst(), items.add(new PendingContactItem(p.getFirst(),
p.getSecond())); p.getSecond(), lastPoll));
} }
pendingContacts.postValue(items); pendingContacts.postValue(items);
} catch (DbException e) { } catch (DbException e) {
......
...@@ -211,15 +211,19 @@ public class BriarRecyclerView extends FrameLayout { ...@@ -211,15 +211,19 @@ public class BriarRecyclerView extends FrameLayout {
} }
public void startPeriodicUpdate() { public void startPeriodicUpdate() {
startPeriodicUpdate(MIN_DATE_RESOLUTION);
}
public void startPeriodicUpdate(long interval) {
if (recyclerView == null || recyclerView.getAdapter() == null) { if (recyclerView == null || recyclerView.getAdapter() == null) {
throw new IllegalStateException("Need to call setAdapter() first!"); throw new IllegalStateException("Need to call setAdapter() first!");
} }
refresher = () -> { refresher = () -> {
Adapter adapter = recyclerView.getAdapter(); Adapter adapter = recyclerView.getAdapter();
adapter.notifyItemRangeChanged(0, adapter.getItemCount()); adapter.notifyItemRangeChanged(0, adapter.getItemCount());
handler.postDelayed(refresher, MIN_DATE_RESOLUTION); handler.postDelayed(refresher, interval);
}; };
handler.postDelayed(refresher, MIN_DATE_RESOLUTION); handler.postDelayed(refresher, interval);
} }
public void stopPeriodicUpdate() { public void stopPeriodicUpdate() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment