mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Make body cache thread-safe, reduce visibility of classes.
This commit is contained in:
@@ -77,11 +77,11 @@ import org.briarproject.util.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -106,7 +106,7 @@ public class ConversationActivity extends BriarActivity
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(ConversationActivity.class.getName());
|
||||
private static final int REQUEST_CODE_INTRODUCTION = 1;
|
||||
public static final String SHOW_ONBOARDING_INTRODUCTION =
|
||||
private static final String SHOW_ONBOARDING_INTRODUCTION =
|
||||
"showOnboardingIntroduction";
|
||||
|
||||
@Inject
|
||||
@@ -115,7 +115,9 @@ public class ConversationActivity extends BriarActivity
|
||||
ConnectionRegistry connectionRegistry;
|
||||
@Inject
|
||||
@CryptoExecutor
|
||||
protected Executor cryptoExecutor;
|
||||
Executor cryptoExecutor;
|
||||
|
||||
private final Map<MessageId, byte[]> bodyCache = new ConcurrentHashMap<>();
|
||||
|
||||
private ConversationAdapter adapter;
|
||||
private Toolbar toolbar;
|
||||
@@ -148,7 +150,6 @@ public class ConversationActivity extends BriarActivity
|
||||
private volatile String contactName = null;
|
||||
private volatile byte[] contactIdenticonKey = null;
|
||||
private volatile boolean connected = false;
|
||||
private volatile Map<MessageId, byte[]> bodyCache = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
@@ -167,7 +168,8 @@ public class ConversationActivity extends BriarActivity
|
||||
if (toolbar != null) {
|
||||
toolbarAvatar =
|
||||
(CircleImageView) toolbar.findViewById(R.id.contactAvatar);
|
||||
toolbarStatus = (ImageView) toolbar.findViewById(R.id.contactStatus);
|
||||
toolbarStatus =
|
||||
(ImageView) toolbar.findViewById(R.id.contactStatus);
|
||||
toolbarTitle = (TextView) toolbar.findViewById(R.id.contactName);
|
||||
setSupportActionBar(toolbar);
|
||||
}
|
||||
@@ -383,11 +385,9 @@ public class ConversationActivity extends BriarActivity
|
||||
} else {
|
||||
List<ConversationItem> items = new ArrayList<>();
|
||||
for (PrivateMessageHeader h : headers) {
|
||||
ConversationMessageItem item =
|
||||
(ConversationMessageItem) ConversationItem
|
||||
.from(h);
|
||||
ConversationMessageItem item = ConversationItem.from(h);
|
||||
byte[] body = bodyCache.get(h.getId());
|
||||
if (body == null) loadMessageBody(h);
|
||||
if (body == null) loadMessageBody(h.getId());
|
||||
else item.setBody(body);
|
||||
items.add(item);
|
||||
}
|
||||
@@ -406,12 +406,10 @@ public class ConversationActivity extends BriarActivity
|
||||
}
|
||||
for (InvitationMessage i : invitations) {
|
||||
if (i instanceof InvitationRequest) {
|
||||
InvitationRequest r =
|
||||
(InvitationRequest) i;
|
||||
InvitationRequest r = (InvitationRequest) i;
|
||||
items.add(ConversationItem.from(r));
|
||||
} else if (i instanceof InvitationResponse) {
|
||||
InvitationResponse r =
|
||||
(InvitationResponse) i;
|
||||
InvitationResponse r = (InvitationResponse) i;
|
||||
items.add(ConversationItem
|
||||
.from(ConversationActivity.this,
|
||||
contactName, r));
|
||||
@@ -425,17 +423,17 @@ public class ConversationActivity extends BriarActivity
|
||||
});
|
||||
}
|
||||
|
||||
private void loadMessageBody(final PrivateMessageHeader h) {
|
||||
private void loadMessageBody(final MessageId m) {
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
byte[] body = messagingManager.getMessageBody(h.getId());
|
||||
byte[] body = messagingManager.getMessageBody(m);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading message took " + duration + " ms");
|
||||
displayMessageBody(h.getId(), body);
|
||||
LOG.info("Loading body took " + duration + " ms");
|
||||
displayMessageBody(m, body);
|
||||
} catch (NoSuchMessageException e) {
|
||||
// The item will be removed when we get the event
|
||||
} catch (DbException e) {
|
||||
@@ -525,7 +523,7 @@ public class ConversationActivity extends BriarActivity
|
||||
LOG.info("Message received, adding");
|
||||
PrivateMessageHeader h = p.getMessageHeader();
|
||||
addConversationItem(ConversationItem.from(h));
|
||||
loadMessageBody(h);
|
||||
loadMessageBody(h.getId());
|
||||
markMessageReadIfNew(h);
|
||||
}
|
||||
} else if (e instanceof MessagesSentEvent) {
|
||||
@@ -663,9 +661,9 @@ public class ConversationActivity extends BriarActivity
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
storeMessage(privateMessageFactory
|
||||
.createPrivateMessage(groupId, timestamp, null,
|
||||
"text/plain", body), body);
|
||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||
groupId, timestamp, null, "text/plain", body),
|
||||
body);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -684,14 +682,13 @@ public class ConversationActivity extends BriarActivity
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing message took " + duration + " ms");
|
||||
|
||||
PrivateMessageHeader h = new PrivateMessageHeader(
|
||||
m.getMessage().getId(),
|
||||
MessageId id = m.getMessage().getId();
|
||||
PrivateMessageHeader h = new PrivateMessageHeader(id,
|
||||
m.getMessage().getTimestamp(), m.getContentType(),
|
||||
true, false, false, false);
|
||||
ConversationMessageItem item =
|
||||
(ConversationMessageItem) ConversationItem.from(h);
|
||||
ConversationMessageItem item = ConversationItem.from(h);
|
||||
item.setBody(body);
|
||||
bodyCache.put(m.getMessage().getId(), body);
|
||||
bodyCache.put(id, body);
|
||||
addConversationItem(item);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
|
||||
@@ -3,12 +3,12 @@ package org.briarproject.android.contact;
|
||||
import org.briarproject.api.introduction.IntroductionRequest;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class ConversationIntroductionInItem extends ConversationIntroductionItem
|
||||
class ConversationIntroductionInItem extends ConversationIntroductionItem
|
||||
implements ConversationItem.IncomingItem {
|
||||
|
||||
private boolean read;
|
||||
|
||||
public ConversationIntroductionInItem(IntroductionRequest ir) {
|
||||
ConversationIntroductionInItem(IntroductionRequest ir) {
|
||||
super(ir);
|
||||
|
||||
this.read = ir.isRead();
|
||||
|
||||
@@ -8,22 +8,22 @@ abstract class ConversationIntroductionItem extends ConversationItem {
|
||||
private final IntroductionRequest ir;
|
||||
private boolean answered;
|
||||
|
||||
public ConversationIntroductionItem(IntroductionRequest ir) {
|
||||
ConversationIntroductionItem(IntroductionRequest ir) {
|
||||
super(ir.getMessageId(), ir.getTimestamp());
|
||||
|
||||
this.ir = ir;
|
||||
this.answered = ir.wasAnswered();
|
||||
}
|
||||
|
||||
public IntroductionRequest getIntroductionRequest() {
|
||||
IntroductionRequest getIntroductionRequest() {
|
||||
return ir;
|
||||
}
|
||||
|
||||
public boolean wasAnswered() {
|
||||
boolean wasAnswered() {
|
||||
return answered;
|
||||
}
|
||||
|
||||
public void setAnswered(boolean answered) {
|
||||
void setAnswered(boolean answered) {
|
||||
this.answered = answered;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,12 @@ import org.briarproject.api.introduction.IntroductionRequest;
|
||||
*
|
||||
* This class is not thread-safe
|
||||
*/
|
||||
public class ConversationIntroductionOutItem
|
||||
extends ConversationIntroductionItem
|
||||
class ConversationIntroductionOutItem extends ConversationIntroductionItem
|
||||
implements ConversationItem.OutgoingItem {
|
||||
|
||||
private boolean sent, seen;
|
||||
|
||||
public ConversationIntroductionOutItem(IntroductionRequest ir) {
|
||||
ConversationIntroductionOutItem(IntroductionRequest ir) {
|
||||
super(ir);
|
||||
this.sent = ir.isSent();
|
||||
this.seen = ir.isSeen();
|
||||
|
||||
@@ -48,14 +48,15 @@ public abstract class ConversationItem {
|
||||
return time;
|
||||
}
|
||||
|
||||
public static ConversationItem from(PrivateMessageHeader h) {
|
||||
if (h.isLocal())
|
||||
public static ConversationMessageItem from(PrivateMessageHeader h) {
|
||||
if (h.isLocal()) {
|
||||
return new ConversationMessageOutItem(h);
|
||||
else
|
||||
} else {
|
||||
return new ConversationMessageInItem(h);
|
||||
}
|
||||
}
|
||||
|
||||
public static ConversationItem from(IntroductionRequest ir) {
|
||||
public static ConversationIntroductionItem from(IntroductionRequest ir) {
|
||||
if (ir.isLocal()) {
|
||||
return new ConversationIntroductionOutItem(ir);
|
||||
} else {
|
||||
@@ -63,7 +64,7 @@ public abstract class ConversationItem {
|
||||
}
|
||||
}
|
||||
|
||||
public static ConversationItem from(Context ctx, String contactName,
|
||||
public static ConversationNoticeItem from(Context ctx, String contactName,
|
||||
IntroductionResponse ir) {
|
||||
|
||||
if (ir.isLocal()) {
|
||||
@@ -101,7 +102,8 @@ public abstract class ConversationItem {
|
||||
}
|
||||
}
|
||||
|
||||
public static ConversationItem from(InvitationRequest fim) {
|
||||
public static ConversationShareableInvitationItem from(
|
||||
InvitationRequest fim) {
|
||||
if (fim.isLocal()) {
|
||||
return new ConversationShareableInvitationOutItem(fim);
|
||||
} else {
|
||||
@@ -109,7 +111,7 @@ public abstract class ConversationItem {
|
||||
}
|
||||
}
|
||||
|
||||
public static ConversationItem from(Context ctx, String contactName,
|
||||
public static ConversationNoticeItem from(Context ctx, String contactName,
|
||||
InvitationResponse ir) {
|
||||
|
||||
if (ir instanceof ForumInvitationResponse) {
|
||||
@@ -121,7 +123,7 @@ public abstract class ConversationItem {
|
||||
}
|
||||
}
|
||||
|
||||
private static ConversationItem from(Context ctx, String contactName,
|
||||
private static ConversationNoticeItem from(Context ctx, String contactName,
|
||||
ForumInvitationResponse fir) {
|
||||
|
||||
if (fir.isLocal()) {
|
||||
@@ -153,7 +155,7 @@ public abstract class ConversationItem {
|
||||
}
|
||||
}
|
||||
|
||||
private static ConversationItem from(Context ctx, String contactName,
|
||||
private static ConversationNoticeItem from(Context ctx, String contactName,
|
||||
BlogInvitationResponse fir) {
|
||||
|
||||
if (fir.isLocal()) {
|
||||
|
||||
@@ -3,12 +3,12 @@ package org.briarproject.android.contact;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class ConversationMessageInItem extends ConversationMessageItem
|
||||
class ConversationMessageInItem extends ConversationMessageItem
|
||||
implements ConversationItem.IncomingItem {
|
||||
|
||||
private boolean read;
|
||||
|
||||
public ConversationMessageInItem(PrivateMessageHeader header) {
|
||||
ConversationMessageInItem(PrivateMessageHeader header) {
|
||||
super(header);
|
||||
|
||||
read = header.isRead();
|
||||
|
||||
@@ -8,7 +8,7 @@ abstract class ConversationMessageItem extends ConversationItem {
|
||||
private final PrivateMessageHeader header;
|
||||
private byte[] body;
|
||||
|
||||
public ConversationMessageItem(PrivateMessageHeader header) {
|
||||
ConversationMessageItem(PrivateMessageHeader header) {
|
||||
super(header.getId(), header.getTimestamp());
|
||||
|
||||
this.header = header;
|
||||
|
||||
@@ -3,12 +3,12 @@ package org.briarproject.android.contact;
|
||||
import org.briarproject.api.messaging.PrivateMessageHeader;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class ConversationMessageOutItem extends ConversationMessageItem
|
||||
class ConversationMessageOutItem extends ConversationMessageItem
|
||||
implements ConversationItem.OutgoingItem {
|
||||
|
||||
private boolean sent, seen;
|
||||
|
||||
public ConversationMessageOutItem(PrivateMessageHeader header) {
|
||||
ConversationMessageOutItem(PrivateMessageHeader header) {
|
||||
super(header);
|
||||
|
||||
sent = header.isSent();
|
||||
|
||||
@@ -3,12 +3,12 @@ package org.briarproject.android.contact;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class ConversationNoticeInItem extends ConversationNoticeItem
|
||||
class ConversationNoticeInItem extends ConversationNoticeItem
|
||||
implements ConversationItem.IncomingItem {
|
||||
|
||||
private boolean read;
|
||||
|
||||
public ConversationNoticeInItem(MessageId id, String text, long time,
|
||||
ConversationNoticeInItem(MessageId id, String text, long time,
|
||||
boolean read) {
|
||||
super(id, text, time);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ abstract class ConversationNoticeItem extends ConversationItem {
|
||||
|
||||
private final String text;
|
||||
|
||||
public ConversationNoticeItem(MessageId id, String text, long time) {
|
||||
ConversationNoticeItem(MessageId id, String text, long time) {
|
||||
super(id, time);
|
||||
|
||||
this.text = text;
|
||||
|
||||
@@ -3,12 +3,12 @@ package org.briarproject.android.contact;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class ConversationNoticeOutItem extends ConversationNoticeItem
|
||||
class ConversationNoticeOutItem extends ConversationNoticeItem
|
||||
implements ConversationItem.OutgoingItem {
|
||||
|
||||
private boolean sent, seen;
|
||||
|
||||
public ConversationNoticeOutItem(MessageId id, String text, long time,
|
||||
ConversationNoticeOutItem(MessageId id, String text, long time,
|
||||
boolean sent, boolean seen) {
|
||||
super(id, text, time);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user