88
99import android .os .Messenger ;
1010
11+ import com .flowcrypt .email .api .email .model .AttachmentInfo ;
1112import com .flowcrypt .email .api .email .sync .SyncListener ;
1213import com .sun .mail .gimap .GmailSSLStore ;
1314import com .sun .mail .iap .Argument ;
1415import com .sun .mail .iap .ProtocolException ;
1516import com .sun .mail .iap .Response ;
1617import com .sun .mail .imap .IMAPFolder ;
1718import com .sun .mail .imap .protocol .BODY ;
19+ import com .sun .mail .imap .protocol .BODYSTRUCTURE ;
1820import com .sun .mail .imap .protocol .FetchResponse ;
1921import com .sun .mail .imap .protocol .IMAPProtocol ;
2022import com .sun .mail .util .ASCIIUtility ;
2123
24+ import java .util .ArrayList ;
25+ import java .util .List ;
26+
2227import javax .mail .Folder ;
2328import 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}
0 commit comments