[android] Address first round of review comments for attachments

This commit is contained in:
Torsten Grote
2019-04-12 09:25:45 -03:00
parent 11eefaedcf
commit 7358091699
4 changed files with 28 additions and 16 deletions

View File

@@ -37,7 +37,7 @@ class AttachmentCreationTask {
private final List<Uri> uris; private final List<Uri> uris;
private final boolean needsSize; private final boolean needsSize;
@Nullable @Nullable
private AttachmentCreator attachmentCreator; private volatile AttachmentCreator attachmentCreator;
private volatile boolean canceled = false; private volatile boolean canceled = false;
@@ -61,9 +61,10 @@ class AttachmentCreationTask {
@IoExecutor @IoExecutor
public void storeAttachments() { public void storeAttachments() {
for (Uri uri: uris) processUri(uri); for (Uri uri: uris) processUri(uri);
AttachmentCreator attachmentCreator = this.attachmentCreator;
if (!canceled && attachmentCreator != null) if (!canceled && attachmentCreator != null)
attachmentCreator.onAttachmentCreationFinished(); attachmentCreator.onAttachmentCreationFinished();
attachmentCreator = null; this.attachmentCreator = null;
} }
@IoExecutor @IoExecutor
@@ -71,16 +72,17 @@ class AttachmentCreationTask {
if (canceled) return; if (canceled) return;
try { try {
AttachmentHeader h = storeAttachment(uri); AttachmentHeader h = storeAttachment(uri);
AttachmentCreator attachmentCreator = this.attachmentCreator;
if (attachmentCreator != null) { if (attachmentCreator != null) {
attachmentCreator attachmentCreator.onAttachmentHeaderReceived(uri, h, needsSize);
.onAttachmentHeaderReceived(uri, h, needsSize);
} }
} catch (DbException | IOException e) { } catch (DbException | IOException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
AttachmentCreator attachmentCreator = this.attachmentCreator;
if (attachmentCreator != null) { if (attachmentCreator != null) {
attachmentCreator.onAttachmentError(uri, e); attachmentCreator.onAttachmentError(uri, e);
canceled = true;
} }
canceled = true;
} }
} }
@@ -90,9 +92,10 @@ class AttachmentCreationTask {
long start = now(); long start = now();
String contentType = contentResolver.getType(uri); String contentType = contentResolver.getType(uri);
if (contentType == null) throw new IOException("null content type"); if (contentType == null) throw new IOException("null content type");
if (!isValidMimeType(contentType)) if (!isValidMimeType(contentType)) {
throw new UnsupportedMimeTypeException("", contentType, String uriString = uri.toString();
uri.toString()); throw new UnsupportedMimeTypeException("", contentType, uriString);
}
InputStream is = contentResolver.openInputStream(uri); InputStream is = contentResolver.openInputStream(uri);
if (is == null) throw new IOException(); if (is == null) throw new IOException();
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();

View File

@@ -44,7 +44,7 @@ public class AttachmentCreator {
@IoExecutor @IoExecutor
private final Executor ioExecutor; private final Executor ioExecutor;
private final MessagingManager messagingManager; private final MessagingManager messagingManager;
private final AttachmentRetriever controller; private final AttachmentRetriever retriever;
private final Map<Uri, AttachmentItem> unsentItems = private final Map<Uri, AttachmentItem> unsentItems =
new ConcurrentHashMap<>(); new ConcurrentHashMap<>();
@@ -57,12 +57,11 @@ public class AttachmentCreator {
private AttachmentCreationTask task; private AttachmentCreationTask task;
public AttachmentCreator(Application app, @IoExecutor Executor ioExecutor, public AttachmentCreator(Application app, @IoExecutor Executor ioExecutor,
MessagingManager messagingManager, MessagingManager messagingManager, AttachmentRetriever retriever) {
AttachmentRetriever controller) {
this.app = app; this.app = app;
this.ioExecutor = ioExecutor; this.ioExecutor = ioExecutor;
this.messagingManager = messagingManager; this.messagingManager = messagingManager;
this.controller = controller; this.retriever = retriever;
} }
@UiThread @UiThread
@@ -101,8 +100,8 @@ public class AttachmentCreator {
boolean needsSize) { boolean needsSize) {
// get and cache AttachmentItem for ImagePreview // get and cache AttachmentItem for ImagePreview
try { try {
Attachment a = controller.getMessageAttachment(h); Attachment a = retriever.getMessageAttachment(h);
AttachmentItem item = controller.getAttachmentItem(h, a, needsSize); AttachmentItem item = retriever.getAttachmentItem(h, a, needsSize);
if (item.hasError()) throw new IOException(); if (item.hasError()) throw new IOException();
unsentItems.put(uri, item); unsentItems.put(uri, item);
MutableLiveData<AttachmentItemResult> result = MutableLiveData<AttachmentItemResult> result =
@@ -156,7 +155,7 @@ public class AttachmentCreator {
* @param id The MessageId of the sent message. * @param id The MessageId of the sent message.
*/ */
public void onAttachmentsSent(MessageId id) { public void onAttachmentsSent(MessageId id) {
controller.cachePut(id, new ArrayList<>(unsentItems.values())); retriever.cachePut(id, new ArrayList<>(unsentItems.values()));
resetState(); resetState();
} }

View File

@@ -9,7 +9,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
@UiThread @UiThread
public interface AttachmentManager{ public interface AttachmentManager {
AttachmentResult storeAttachments(Collection<Uri> uri); AttachmentResult storeAttachments(Collection<Uri> uri);

View File

@@ -138,6 +138,16 @@ public class TextAttachmentController extends TextSendController
return intent; return intent;
} }
/**
* This is called with the result Intent
* returned by the Activity started with {@link #getAttachFileIntent()}.
* <p>
* This method must be called at most once per call to
* {@link AttachImageListener#onAttachImage(Intent)}.
* Normally, this is true if called from
* {@link Activity#onActivityResult(int, int, Intent)} since this is called
* at most once per call to {@link Activity#startActivityForResult(Intent, int)}.
*/
public void onImageReceived(@Nullable Intent resultData) { public void onImageReceived(@Nullable Intent resultData) {
if (resultData == null) return; if (resultData == null) return;
if (loadingUris || !imageUris.isEmpty()) throw new AssertionError(); if (loadingUris || !imageUris.isEmpty()) throw new AssertionError();