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