forked from thunderbird/thunderbird-android
/
AttachmentResolver.java
82 lines (66 loc) · 2.7 KB
/
AttachmentResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.fsck.k9.mailstore;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
import timber.log.Timber;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.message.extractors.AttachmentInfoExtractor;
/**
* This class is used to encapsulate a message part, providing an interface to
* get relevant info for a given Content-ID URI.
*
* The point of this class is to keep the Content-ID loading code agnostic of
* the underlying part structure.
*/
public class AttachmentResolver {
Map<String,Uri> contentIdToAttachmentUriMap;
private AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap) {
this.contentIdToAttachmentUriMap = contentIdToAttachmentUriMap;
}
@Nullable
public Uri getAttachmentUriForContentId(String cid) {
return contentIdToAttachmentUriMap.get(cid);
}
@WorkerThread
public static AttachmentResolver createFromPart(Part part) {
AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
Map<String, Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(attachmentInfoExtractor, part);
return new AttachmentResolver(contentIdToAttachmentUriMap);
}
@VisibleForTesting
static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor,
Part rootPart) {
HashMap<String,Uri> result = new HashMap<>();
Stack<Part> partsToCheck = new Stack<>();
partsToCheck.push(rootPart);
while (!partsToCheck.isEmpty()) {
Part part = partsToCheck.pop();
Body body = part.getBody();
if (body instanceof Multipart) {
Multipart multipart = (Multipart) body;
for (Part bodyPart : multipart.getBodyParts()) {
partsToCheck.push(bodyPart);
}
} else {
try {
String contentId = part.getContentId();
if (contentId != null) {
AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part);
result.put(contentId, attachmentInfo.internalUri);
}
} catch (MessagingException e) {
Timber.e(e, "Error extracting attachment info");
}
}
}
return Collections.unmodifiableMap(result);
}
}