Skip to content

Commit c2608dc

Browse files
committed
Added request the attachments details when loading the message details. | #55.
1 parent 216a65e commit c2608dc

File tree

4 files changed

+137
-9
lines changed

4 files changed

+137
-9
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Business Source License 1.0 © 2017 FlowCrypt Limited ([email protected]).
3+
* Use limitations apply. See https://github.com/FlowCrypt/flowcrypt-android/blob/master/LICENSE
4+
* Contributors: DenBond7
5+
*/
6+
7+
package com.flowcrypt.email.api.email.model;
8+
9+
import android.os.Parcel;
10+
import android.os.Parcelable;
11+
12+
/**
13+
* Simple POJO which defines an information about email attachments.
14+
*
15+
* @author Denis Bondarenko
16+
* Date: 07.08.2017
17+
* Time: 18:38
18+
19+
*/
20+
21+
public class AttachmentInfo implements Parcelable {
22+
public static final Parcelable.Creator<AttachmentInfo> CREATOR = new Parcelable
23+
.Creator<AttachmentInfo>() {
24+
@Override
25+
public AttachmentInfo createFromParcel(Parcel source) {
26+
return new AttachmentInfo(source);
27+
}
28+
29+
@Override
30+
public AttachmentInfo[] newArray(int size) {
31+
return new AttachmentInfo[size];
32+
}
33+
};
34+
35+
private String name;
36+
private int encodedSize;
37+
private String type;
38+
39+
public AttachmentInfo(String name, int encodedSize, String type) {
40+
this.name = name;
41+
this.encodedSize = encodedSize;
42+
this.type = type;
43+
}
44+
45+
protected AttachmentInfo(Parcel in) {
46+
this.name = in.readString();
47+
this.encodedSize = in.readInt();
48+
this.type = in.readString();
49+
}
50+
51+
@Override
52+
public int describeContents() {
53+
return 0;
54+
}
55+
56+
@Override
57+
public void writeToParcel(Parcel dest, int flags) {
58+
dest.writeString(this.name);
59+
dest.writeInt(this.encodedSize);
60+
dest.writeString(this.type);
61+
}
62+
63+
public String getName() {
64+
return name;
65+
}
66+
67+
public int getEncodedSize() {
68+
return encodedSize;
69+
}
70+
71+
public String getType() {
72+
return type;
73+
}
74+
}

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/SyncListener.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import android.content.Context;
1010

11+
import com.flowcrypt.email.api.email.model.AttachmentInfo;
1112
import com.google.android.gms.auth.GoogleAuthException;
1213
import com.sun.mail.imap.IMAPFolder;
1314

@@ -84,12 +85,15 @@ void onMessagesMoved(IMAPFolder sourceImapFolder, IMAPFolder destinationImapFold
8485
* @param imapFolder The folder where the new messages exist.
8586
* @param uid The UID of the message.
8687
* @param rawMessageWithOutAttachments The raw message without attachments.
88+
* @param attachmentInfoList The list of attachments info.
8789
* @param ownerKey The name of the reply to {@link android.os.Messenger}.
8890
* @param requestCode The unique request code for the reply to
8991
* {@link android.os.Messenger}.
9092
*/
91-
void onMessageDetailsReceived(IMAPFolder imapFolder, long uid, String
92-
rawMessageWithOutAttachments, String ownerKey, int requestCode);
93+
void onMessageDetailsReceived(IMAPFolder imapFolder, long uid,
94+
String rawMessageWithOutAttachments,
95+
List<AttachmentInfo> attachmentInfoList,
96+
String ownerKey, int requestCode);
9397

9498
/**
9599
* This method called when a new messages received from the some folder.

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/sync/tasks/LoadMessageDetailsSyncTask.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@
88

99
import android.os.Messenger;
1010

11+
import com.flowcrypt.email.api.email.model.AttachmentInfo;
1112
import com.flowcrypt.email.api.email.sync.SyncListener;
1213
import com.sun.mail.gimap.GmailSSLStore;
1314
import com.sun.mail.iap.Argument;
1415
import com.sun.mail.iap.ProtocolException;
1516
import com.sun.mail.iap.Response;
1617
import com.sun.mail.imap.IMAPFolder;
1718
import com.sun.mail.imap.protocol.BODY;
19+
import com.sun.mail.imap.protocol.BODYSTRUCTURE;
1820
import com.sun.mail.imap.protocol.FetchResponse;
1921
import com.sun.mail.imap.protocol.IMAPProtocol;
2022
import com.sun.mail.util.ASCIIUtility;
2123

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
2227
import javax.mail.Folder;
2328
import javax.mail.Message;
29+
import javax.mail.Part;
2430

2531
/**
2632
* This task load a detail information of the some message. At now this task creates and executes
@@ -54,18 +60,21 @@ public LoadMessageDetailsSyncTask(String ownerKey, int requestCode, String folde
5460
}
5561

5662
@Override
57-
public void run(GmailSSLStore gmailSSLStore, SyncListener syncListener) throws Exception {
63+
public void run(GmailSSLStore gmailSSLStore, final SyncListener syncListener) throws Exception {
5864
IMAPFolder imapFolder = (IMAPFolder) gmailSSLStore.getFolder(folderName);
5965
imapFolder.open(Folder.READ_WRITE);
6066

6167
if (syncListener != null) {
62-
String rawMessage = (String) imapFolder.doCommand(new IMAPFolder.ProtocolCommand() {
68+
MessageDetails messageDetails = (MessageDetails) imapFolder.doCommand(new IMAPFolder
69+
.ProtocolCommand() {
6370
public Object doCommand(IMAPProtocol imapProtocol)
6471
throws ProtocolException {
6572
String rawMessage = null;
73+
List<AttachmentInfo> attachmentInfoList = new ArrayList<>();
6674

6775
Argument args = new Argument();
6876
Argument list = new Argument();
77+
list.writeString("BODYSTRUCTURE");
6978
list.writeString("RFC822.SIZE");
7079
list.writeString("BODY[]<0.204800>");
7180
args.writeArgument(list);
@@ -84,20 +93,58 @@ public Object doCommand(IMAPProtocol imapProtocol)
8493
if (body != null && body.getByteArrayInputStream() != null) {
8594
rawMessage = ASCIIUtility.toString(body.getByteArrayInputStream());
8695
}
96+
97+
getInformationAboutAttachments(attachmentInfoList,
98+
fetchResponse.getItem(BODYSTRUCTURE.class));
8799
}
88100
}
89101

90102
imapProtocol.notifyResponseHandlers(responses);
91103
imapProtocol.handleResult(serverStatusResponse);
92104

93-
return rawMessage;
105+
return new MessageDetails(rawMessage, attachmentInfoList);
94106
}
95107
});
96108

97-
syncListener.onMessageDetailsReceived(imapFolder, uid, rawMessage, ownerKey,
98-
requestCode);
109+
syncListener.onMessageDetailsReceived(imapFolder, uid, messageDetails.rawMessage,
110+
messageDetails.attachmentInfoList, ownerKey, requestCode);
99111
}
100112

101113
imapFolder.close(false);
102114
}
115+
116+
/**
117+
* Get an information about the email attachments if they exist.
118+
*
119+
* @param attachmentInfoList The attachments info list where we will add a new found attachment
120+
* info.
121+
* @param parentBodyStructure The parent {@link BODYSTRUCTURE}.
122+
*/
123+
private void getInformationAboutAttachments(List<AttachmentInfo> attachmentInfoList,
124+
BODYSTRUCTURE parentBodyStructure) {
125+
if (parentBodyStructure != null) {
126+
BODYSTRUCTURE[] bodyStructureArray = parentBodyStructure.bodies;
127+
128+
for (BODYSTRUCTURE bodystructure : bodyStructureArray) {
129+
if (bodystructure.bodies == null) {
130+
if (Part.ATTACHMENT.equalsIgnoreCase(bodystructure.disposition)) {
131+
attachmentInfoList.add(new AttachmentInfo(bodystructure.dParams.get
132+
("FILENAME"), bodystructure.size, bodystructure.type));
133+
}
134+
} else {
135+
getInformationAboutAttachments(attachmentInfoList, bodystructure);
136+
}
137+
}
138+
}
139+
}
140+
141+
private class MessageDetails {
142+
String rawMessage;
143+
List<AttachmentInfo> attachmentInfoList;
144+
145+
MessageDetails(String rawMessage, List<AttachmentInfo> attachmentInfoList) {
146+
this.rawMessage = rawMessage;
147+
this.attachmentInfoList = attachmentInfoList;
148+
}
149+
}
103150
}

FlowCrypt/src/main/java/com/flowcrypt/email/service/EmailSyncService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.flowcrypt.email.api.email.FoldersManager;
2323
import com.flowcrypt.email.api.email.JavaEmailConstants;
2424
import com.flowcrypt.email.api.email.gmail.GmailConstants;
25+
import com.flowcrypt.email.api.email.model.AttachmentInfo;
2526
import com.flowcrypt.email.api.email.sync.GmailSynsManager;
2627
import com.flowcrypt.email.api.email.sync.SyncListener;
2728
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource;
@@ -222,8 +223,10 @@ public void onMessagesMoved(IMAPFolder sourceImapFolder, IMAPFolder destinationI
222223
}
223224

224225
@Override
225-
public void onMessageDetailsReceived(IMAPFolder imapFolder, long uid, String
226-
rawMessageWithOutAttachments, String ownerKey, int requestCode) {
226+
public void onMessageDetailsReceived(IMAPFolder imapFolder, long uid,
227+
String rawMessageWithOutAttachments,
228+
List<AttachmentInfo> attachmentInfoList,
229+
String ownerKey, int requestCode) {
227230
try {
228231
MessageDaoSource messageDaoSource = new MessageDaoSource();
229232
com.flowcrypt.email.api.email.Folder folder = FoldersManager.generateFolder(imapFolder,

0 commit comments

Comments
 (0)