mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Turn AttachmentReader into a proper class
and inject it where needed
This commit is contained in:
@@ -49,6 +49,7 @@ import org.briarproject.briar.api.forum.ForumManager;
|
||||
import org.briarproject.briar.api.forum.ForumSharingManager;
|
||||
import org.briarproject.briar.api.identity.AuthorManager;
|
||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||
import org.briarproject.briar.api.media.AttachmentReader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
import org.briarproject.briar.api.privategroup.GroupMessageFactory;
|
||||
@@ -95,6 +96,8 @@ public interface AndroidComponent
|
||||
|
||||
IdentityManager identityManager();
|
||||
|
||||
AttachmentReader attachmentReader();
|
||||
|
||||
AuthorManager authorManager();
|
||||
|
||||
PluginManager pluginManager();
|
||||
|
||||
@@ -35,7 +35,7 @@ import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
import static org.briarproject.briar.api.media.MediaConstants.MAX_IMAGE_SIZE;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentCreationTask {
|
||||
|
||||
@@ -36,7 +36,7 @@ import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR;
|
||||
import static org.briarproject.briar.android.util.UiUtils.observeForeverOnce;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_IMAGE_SIZE;
|
||||
import static org.briarproject.briar.api.media.MediaConstants.MAX_IMAGE_SIZE;
|
||||
|
||||
@NotNullByDefault
|
||||
class AttachmentCreatorImpl implements AttachmentCreator {
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem.State;
|
||||
import org.briarproject.briar.api.media.Attachment;
|
||||
import org.briarproject.briar.api.media.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.media.AttachmentReader;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
@@ -43,7 +43,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
|
||||
@DatabaseExecutor
|
||||
private final Executor dbExecutor;
|
||||
private final MessagingManager messagingManager;
|
||||
private final AttachmentReader attachmentReader;
|
||||
private final ImageHelper imageHelper;
|
||||
private final ImageSizeCalculator imageSizeCalculator;
|
||||
private final int defaultSize;
|
||||
@@ -57,11 +57,10 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
|
||||
@Inject
|
||||
AttachmentRetrieverImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
MessagingManager messagingManager,
|
||||
AttachmentDimensions dimensions, ImageHelper imageHelper,
|
||||
ImageSizeCalculator imageSizeCalculator) {
|
||||
AttachmentReader attachmentReader, AttachmentDimensions dimensions,
|
||||
ImageHelper imageHelper, ImageSizeCalculator imageSizeCalculator) {
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.messagingManager = messagingManager;
|
||||
this.attachmentReader = attachmentReader;
|
||||
this.imageHelper = imageHelper;
|
||||
this.imageSizeCalculator = imageSizeCalculator;
|
||||
defaultSize = dimensions.defaultSize;
|
||||
@@ -75,7 +74,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
@DatabaseExecutor
|
||||
public Attachment getMessageAttachment(AttachmentHeader h)
|
||||
throws DbException {
|
||||
return messagingManager.getAttachment(h);
|
||||
return attachmentReader.getAttachment(h);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,13 +85,11 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
boolean needsSize = headers.size() == 1;
|
||||
for (AttachmentHeader h : headers) {
|
||||
// try cache for existing item live data
|
||||
MutableLiveData<AttachmentItem> liveData;
|
||||
if (needsSize) liveData = itemsWithSize.get(h.getMessageId());
|
||||
else {
|
||||
// try items with size first, as they work as well
|
||||
liveData = itemsWithSize.get(h.getMessageId());
|
||||
if (liveData == null)
|
||||
liveData = itemsWithoutSize.get(h.getMessageId());
|
||||
MutableLiveData<AttachmentItem> liveData =
|
||||
itemsWithSize.get(h.getMessageId());
|
||||
if (!needsSize && liveData == null) {
|
||||
// check cache for items that don't need the size
|
||||
liveData = itemsWithoutSize.get(h.getMessageId());
|
||||
}
|
||||
|
||||
// create new live data with LOADING item if cache miss
|
||||
@@ -131,7 +128,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
// If a live data is already cached we don't need to do anything
|
||||
if (itemsWithSize.containsKey(h.getMessageId())) return;
|
||||
try {
|
||||
Attachment a = messagingManager.getAttachment(h);
|
||||
Attachment a = attachmentReader.getAttachment(h);
|
||||
AttachmentItem item = createAttachmentItem(a, true);
|
||||
MutableLiveData<AttachmentItem> liveData =
|
||||
new MutableLiveData<>(item);
|
||||
@@ -173,7 +170,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever {
|
||||
Attachment a;
|
||||
AttachmentItem item;
|
||||
try {
|
||||
a = messagingManager.getAttachment(h);
|
||||
a = attachmentReader.getAttachment(h);
|
||||
item = createAttachmentItem(a, needsSize);
|
||||
} catch (NoSuchMessageException e) {
|
||||
LOG.info("Attachment not received yet");
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.briarproject.briar.android.viewmodel.DbViewModel;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
import org.briarproject.briar.api.media.Attachment;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.media.AttachmentReader;
|
||||
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
|
||||
|
||||
import java.io.File;
|
||||
@@ -56,7 +56,7 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
||||
|
||||
private static final Logger LOG = getLogger(ImageViewModel.class.getName());
|
||||
|
||||
private final MessagingManager messagingManager;
|
||||
private final AttachmentReader attachmentReader;
|
||||
private final EventBus eventBus;
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
@@ -75,16 +75,13 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
||||
private int toolbarTop, toolbarBottom;
|
||||
|
||||
@Inject
|
||||
ImageViewModel(Application application,
|
||||
MessagingManager messagingManager,
|
||||
EventBus eventBus,
|
||||
@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager,
|
||||
ImageViewModel(Application application, AttachmentReader attachmentReader,
|
||||
EventBus eventBus, @DatabaseExecutor Executor dbExecutor,LifecycleManager lifecycleManager,
|
||||
TransactionManager db,
|
||||
AndroidExecutor androidExecutor,
|
||||
@IoExecutor Executor ioExecutor) {
|
||||
super(application, dbExecutor, lifecycleManager, db, androidExecutor);
|
||||
this.messagingManager = messagingManager;
|
||||
this.attachmentReader = attachmentReader;
|
||||
this.eventBus = eventBus;
|
||||
this.ioExecutor = ioExecutor;
|
||||
|
||||
@@ -202,7 +199,7 @@ public class ImageViewModel extends DbViewModel implements EventListener {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Attachment a =
|
||||
messagingManager.getAttachment(attachment.getHeader());
|
||||
attachmentReader.getAttachment(attachment.getHeader());
|
||||
copyImageFromDb(a, osp, afterCopy);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
|
||||
@@ -4,13 +4,12 @@ import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.api.media.Attachment;
|
||||
import org.briarproject.briar.media.AttachmentReader;
|
||||
import org.briarproject.briar.api.media.AttachmentReader;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -31,7 +30,7 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
|
||||
private final static Logger LOG =
|
||||
getLogger(BriarDataFetcher.class.getName());
|
||||
|
||||
private final ClientHelper clientHelper;
|
||||
private final AttachmentReader attachmentReader;
|
||||
@DatabaseExecutor
|
||||
private final Executor dbExecutor;
|
||||
private final AttachmentItem attachment;
|
||||
@@ -41,9 +40,9 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
|
||||
private volatile boolean cancel = false;
|
||||
|
||||
@Inject
|
||||
BriarDataFetcher(ClientHelper clientHelper,
|
||||
BriarDataFetcher(AttachmentReader attachmentReader,
|
||||
@DatabaseExecutor Executor dbExecutor, AttachmentItem attachment) {
|
||||
this.clientHelper = clientHelper;
|
||||
this.attachmentReader = attachmentReader;
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.attachment = attachment;
|
||||
}
|
||||
@@ -54,8 +53,8 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
|
||||
dbExecutor.execute(() -> {
|
||||
if (cancel) return;
|
||||
try {
|
||||
Attachment a = AttachmentReader
|
||||
.getAttachment(clientHelper, attachment.getHeader());
|
||||
Attachment a =
|
||||
attachmentReader.getAttachment(attachment.getHeader());
|
||||
inputStream = a.getStream();
|
||||
callback.onDataReady(inputStream);
|
||||
} catch (DbException e) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.briarproject.briar.android.conversation.glide;
|
||||
|
||||
import org.briarproject.bramble.api.client.ClientHelper;
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.attachment.AttachmentItem;
|
||||
import org.briarproject.briar.api.media.AttachmentReader;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
@@ -12,19 +12,19 @@ import javax.inject.Inject;
|
||||
@NotNullByDefault
|
||||
public class BriarDataFetcherFactory {
|
||||
|
||||
private final ClientHelper clientHelper;
|
||||
private final AttachmentReader attachmentReader;
|
||||
@DatabaseExecutor
|
||||
private final Executor dbExecutor;
|
||||
|
||||
@Inject
|
||||
public BriarDataFetcherFactory(ClientHelper clientHelper,
|
||||
public BriarDataFetcherFactory(AttachmentReader attachmentReader,
|
||||
@DatabaseExecutor Executor dbExecutor) {
|
||||
this.clientHelper = clientHelper;
|
||||
this.attachmentReader = attachmentReader;
|
||||
this.dbExecutor = dbExecutor;
|
||||
}
|
||||
|
||||
BriarDataFetcher createBriarDataFetcher(AttachmentItem model) {
|
||||
return new BriarDataFetcher(clientHelper, dbExecutor, model);
|
||||
return new BriarDataFetcher(attachmentReader, dbExecutor, model);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user