mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Make sure contact name is initialized when needed
This commit is contained in:
@@ -2,6 +2,7 @@ package org.briarproject.briar.android.activity;
|
|||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.support.annotation.UiThread;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
@@ -118,6 +119,7 @@ public abstract class BaseActivity extends AppCompatActivity
|
|||||||
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
((InputMethodManager) o).hideSoftInputFromWindow(token, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
public void handleDbException(DbException e) {
|
public void handleDbException(DbException e) {
|
||||||
supportFinishAfterTransition();
|
supportFinishAfterTransition();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ import org.briarproject.briar.api.sharing.InvitationRequest;
|
|||||||
import org.briarproject.briar.api.sharing.InvitationResponse;
|
import org.briarproject.briar.api.sharing.InvitationResponse;
|
||||||
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
|
import org.briarproject.briar.api.sharing.event.InvitationRequestReceivedEvent;
|
||||||
import org.briarproject.briar.api.sharing.event.InvitationResponseReceivedEvent;
|
import org.briarproject.briar.api.sharing.event.InvitationResponseReceivedEvent;
|
||||||
|
import org.thoughtcrime.securesms.components.util.FutureTaskListener;
|
||||||
|
import org.thoughtcrime.securesms.components.util.ListenableFutureTask;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -86,8 +88,10 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -139,6 +143,18 @@ public class ConversationActivity extends BriarActivity
|
|||||||
private BriarRecyclerView list;
|
private BriarRecyclerView list;
|
||||||
private TextInputView textInputView;
|
private TextInputView textInputView;
|
||||||
|
|
||||||
|
private final ListenableFutureTask<String> contactNameTask =
|
||||||
|
new ListenableFutureTask<>(new Callable<String>() {
|
||||||
|
@Override
|
||||||
|
public String call() throws Exception {
|
||||||
|
Contact c = contactManager.getContact(contactId);
|
||||||
|
contactName = c.getAuthor().getName();
|
||||||
|
return c.getAuthor().getName();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
private final AtomicBoolean contactNameTaskStarted =
|
||||||
|
new AtomicBoolean(false);
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
// Fields that are accessed from background threads must be volatile
|
||||||
@Inject
|
@Inject
|
||||||
volatile ContactManager contactManager;
|
volatile ContactManager contactManager;
|
||||||
@@ -160,8 +176,11 @@ public class ConversationActivity extends BriarActivity
|
|||||||
volatile GroupInvitationManager groupInvitationManager;
|
volatile GroupInvitationManager groupInvitationManager;
|
||||||
|
|
||||||
private volatile ContactId contactId;
|
private volatile ContactId contactId;
|
||||||
|
@Nullable
|
||||||
private volatile String contactName;
|
private volatile String contactName;
|
||||||
|
@Nullable
|
||||||
private volatile AuthorId contactAuthorId;
|
private volatile AuthorId contactAuthorId;
|
||||||
|
@Nullable
|
||||||
private volatile GroupId messagingGroupId;
|
private volatile GroupId messagingGroupId;
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
@@ -223,8 +242,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
eventBus.addListener(this);
|
eventBus.addListener(this);
|
||||||
notificationManager.blockContactNotification(contactId);
|
notificationManager.blockContactNotification(contactId);
|
||||||
notificationManager.clearContactNotification(contactId);
|
notificationManager.clearContactNotification(contactId);
|
||||||
loadContactDetails();
|
displayContactOnlineStatus();
|
||||||
loadMessages();
|
loadContactDetailsAndMessages();
|
||||||
list.startPeriodicUpdate();
|
list.startPeriodicUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,7 +288,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadContactDetails() {
|
private void loadContactDetailsAndMessages() {
|
||||||
runOnDbThread(new Runnable() {
|
runOnDbThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -283,6 +302,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
long duration = System.currentTimeMillis() - now;
|
long duration = System.currentTimeMillis() - now;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Loading contact took " + duration + " ms");
|
LOG.info("Loading contact took " + duration + " ms");
|
||||||
|
loadMessages();
|
||||||
displayContactDetails();
|
displayContactDetails();
|
||||||
} catch (NoSuchContactException e) {
|
} catch (NoSuchContactException e) {
|
||||||
finishOnUiThread();
|
finishOnUiThread();
|
||||||
@@ -298,10 +318,18 @@ public class ConversationActivity extends BriarActivity
|
|||||||
runOnUiThreadUnlessDestroyed(new Runnable() {
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
//noinspection ConstantConditions
|
||||||
toolbarAvatar.setImageDrawable(
|
toolbarAvatar.setImageDrawable(
|
||||||
new IdenticonDrawable(contactAuthorId.getBytes()));
|
new IdenticonDrawable(contactAuthorId.getBytes()));
|
||||||
toolbarTitle.setText(contactName);
|
toolbarTitle.setText(contactName);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void displayContactOnlineStatus() {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
if (connectionRegistry.isConnected(contactId)) {
|
if (connectionRegistry.isConnected(contactId)) {
|
||||||
toolbarStatus.setImageDrawable(ContextCompat
|
toolbarStatus.setImageDrawable(ContextCompat
|
||||||
.getDrawable(ConversationActivity.this,
|
.getDrawable(ConversationActivity.this,
|
||||||
@@ -341,7 +369,8 @@ public class ConversationActivity extends BriarActivity
|
|||||||
groupInvitationManager
|
groupInvitationManager
|
||||||
.getInvitationMessages(contactId);
|
.getInvitationMessages(contactId);
|
||||||
List<InvitationMessage> invitations = new ArrayList<>(
|
List<InvitationMessage> invitations = new ArrayList<>(
|
||||||
forumInvitations.size() + blogInvitations.size());
|
forumInvitations.size() + blogInvitations.size() +
|
||||||
|
groupInvitations.size());
|
||||||
invitations.addAll(forumInvitations);
|
invitations.addAll(forumInvitations);
|
||||||
invitations.addAll(blogInvitations);
|
invitations.addAll(blogInvitations);
|
||||||
invitations.addAll(groupInvitations);
|
invitations.addAll(groupInvitations);
|
||||||
@@ -384,6 +413,12 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates ConversationItems from headers loaded from the database.
|
||||||
|
*
|
||||||
|
* Attention: Call this only after contactName has been initialized.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("ConstantConditions")
|
||||||
private List<ConversationItem> createItems(
|
private List<ConversationItem> createItems(
|
||||||
Collection<PrivateMessageHeader> headers,
|
Collection<PrivateMessageHeader> headers,
|
||||||
Collection<IntroductionMessage> introductions,
|
Collection<IntroductionMessage> introductions,
|
||||||
@@ -462,18 +497,6 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addConversationItem(final ConversationItem item) {
|
|
||||||
runOnUiThreadUnlessDestroyed(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
adapter.incrementRevision();
|
|
||||||
adapter.add(item);
|
|
||||||
// Scroll to the bottom
|
|
||||||
list.scrollToPosition(adapter.getItemCount() - 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof ContactRemovedEvent) {
|
if (e instanceof ContactRemovedEvent) {
|
||||||
@@ -506,13 +529,13 @@ public class ConversationActivity extends BriarActivity
|
|||||||
ContactConnectedEvent c = (ContactConnectedEvent) e;
|
ContactConnectedEvent c = (ContactConnectedEvent) e;
|
||||||
if (c.getContactId().equals(contactId)) {
|
if (c.getContactId().equals(contactId)) {
|
||||||
LOG.info("Contact connected");
|
LOG.info("Contact connected");
|
||||||
displayContactDetails();
|
displayContactOnlineStatus();
|
||||||
}
|
}
|
||||||
} else if (e instanceof ContactDisconnectedEvent) {
|
} else if (e instanceof ContactDisconnectedEvent) {
|
||||||
ContactDisconnectedEvent c = (ContactDisconnectedEvent) e;
|
ContactDisconnectedEvent c = (ContactDisconnectedEvent) e;
|
||||||
if (c.getContactId().equals(contactId)) {
|
if (c.getContactId().equals(contactId)) {
|
||||||
LOG.info("Contact disconnected");
|
LOG.info("Contact disconnected");
|
||||||
displayContactDetails();
|
displayContactOnlineStatus();
|
||||||
}
|
}
|
||||||
} else if (e instanceof IntroductionRequestReceivedEvent) {
|
} else if (e instanceof IntroductionRequestReceivedEvent) {
|
||||||
IntroductionRequestReceivedEvent event =
|
IntroductionRequestReceivedEvent event =
|
||||||
@@ -520,9 +543,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (event.getContactId().equals(contactId)) {
|
if (event.getContactId().equals(contactId)) {
|
||||||
LOG.info("Introduction request received, adding...");
|
LOG.info("Introduction request received, adding...");
|
||||||
IntroductionRequest ir = event.getIntroductionRequest();
|
IntroductionRequest ir = event.getIntroductionRequest();
|
||||||
ConversationItem item =
|
handleIntroductionRequest(ir);
|
||||||
ConversationItem.from(this, contactName, ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
}
|
||||||
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
} else if (e instanceof IntroductionResponseReceivedEvent) {
|
||||||
IntroductionResponseReceivedEvent event =
|
IntroductionResponseReceivedEvent event =
|
||||||
@@ -530,9 +551,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (event.getContactId().equals(contactId)) {
|
if (event.getContactId().equals(contactId)) {
|
||||||
LOG.info("Introduction response received, adding...");
|
LOG.info("Introduction response received, adding...");
|
||||||
IntroductionResponse ir = event.getIntroductionResponse();
|
IntroductionResponse ir = event.getIntroductionResponse();
|
||||||
ConversationItem item =
|
handleIntroductionResponse(ir);
|
||||||
ConversationItem.from(this, contactName, ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
}
|
||||||
} else if (e instanceof InvitationRequestReceivedEvent) {
|
} else if (e instanceof InvitationRequestReceivedEvent) {
|
||||||
InvitationRequestReceivedEvent event =
|
InvitationRequestReceivedEvent event =
|
||||||
@@ -540,9 +559,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (event.getContactId().equals(contactId)) {
|
if (event.getContactId().equals(contactId)) {
|
||||||
LOG.info("Invitation received, adding...");
|
LOG.info("Invitation received, adding...");
|
||||||
InvitationRequest ir = event.getRequest();
|
InvitationRequest ir = event.getRequest();
|
||||||
ConversationItem item =
|
handleInvitationRequest(ir);
|
||||||
ConversationItem.from(this, contactName, ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
}
|
||||||
} else if (e instanceof InvitationResponseReceivedEvent) {
|
} else if (e instanceof InvitationResponseReceivedEvent) {
|
||||||
InvitationResponseReceivedEvent event =
|
InvitationResponseReceivedEvent event =
|
||||||
@@ -550,13 +567,127 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (event.getContactId().equals(contactId)) {
|
if (event.getContactId().equals(contactId)) {
|
||||||
LOG.info("Invitation response received, adding...");
|
LOG.info("Invitation response received, adding...");
|
||||||
InvitationResponse ir = event.getResponse();
|
InvitationResponse ir = event.getResponse();
|
||||||
ConversationItem item =
|
handleInvitationResponse(ir);
|
||||||
ConversationItem.from(this, contactName, ir);
|
|
||||||
addConversationItem(item);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addConversationItem(final ConversationItem item) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
adapter.incrementRevision();
|
||||||
|
adapter.add(item);
|
||||||
|
// Scroll to the bottom
|
||||||
|
list.scrollToPosition(adapter.getItemCount() - 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleIntroductionRequest(final IntroductionRequest m) {
|
||||||
|
getContactNameTask().addListener(new FutureTaskListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final String contactName) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ConversationItem item = ConversationItem
|
||||||
|
.from(ConversationActivity.this, contactName,
|
||||||
|
m);
|
||||||
|
addConversationItem(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable exception) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
handleDbException((DbException) exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleIntroductionResponse(final IntroductionResponse m) {
|
||||||
|
getContactNameTask().addListener(new FutureTaskListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final String contactName) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ConversationItem item = ConversationItem
|
||||||
|
.from(ConversationActivity.this, contactName,
|
||||||
|
m);
|
||||||
|
addConversationItem(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable exception) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
handleDbException((DbException) exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleInvitationRequest(final InvitationRequest m) {
|
||||||
|
getContactNameTask().addListener(new FutureTaskListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final String contactName) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ConversationItem item = ConversationItem
|
||||||
|
.from(ConversationActivity.this, contactName,
|
||||||
|
m);
|
||||||
|
addConversationItem(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable exception) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
handleDbException((DbException) exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleInvitationResponse(final InvitationResponse m) {
|
||||||
|
getContactNameTask().addListener(new FutureTaskListener<String>() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(final String contactName) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ConversationItem item = ConversationItem
|
||||||
|
.from(ConversationActivity.this, contactName,
|
||||||
|
m);
|
||||||
|
addConversationItem(item);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(final Throwable exception) {
|
||||||
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
handleDbException((DbException) exception);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void markMessages(final Collection<MessageId> messageIds,
|
private void markMessages(final Collection<MessageId> messageIds,
|
||||||
final boolean sent, final boolean seen) {
|
final boolean sent, final boolean seen) {
|
||||||
runOnUiThreadUnlessDestroyed(new Runnable() {
|
runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||||
@@ -617,6 +748,7 @@ public class ConversationActivity extends BriarActivity
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
//noinspection ConstantConditions init in loadGroupId()
|
||||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||||
messagingGroupId, timestamp, body), body);
|
messagingGroupId, timestamp, body), body);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
@@ -848,7 +980,6 @@ public class ConversationActivity extends BriarActivity
|
|||||||
if (LOG.isLoggable(WARNING))
|
if (LOG.isLoggable(WARNING))
|
||||||
LOG.log(WARNING, e.toString(), e);
|
LOG.log(WARNING, e.toString(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -919,4 +1050,10 @@ public class ConversationActivity extends BriarActivity
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ListenableFutureTask<String> getContactNameTask() {
|
||||||
|
if (!contactNameTaskStarted.getAndSet(true))
|
||||||
|
runOnDbThread(contactNameTask);
|
||||||
|
return contactNameTask;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user