@@ -69,11 +69,11 @@ public class PrepareOutgoingMessagesJobIntentService extends JobIntentService {
6969 ("EXTRA_KEY_OUTGOING_MESSAGE_INFO" , PrepareOutgoingMessagesJobIntentService .class );
7070 private static final String TAG = PrepareOutgoingMessagesJobIntentService .class .getSimpleName ();
7171
72- private MessageDaoSource messageDaoSource ;
72+ private MessageDaoSource msgDaoSource ;
7373 private Js js ;
7474 private Session session ;
7575 private AccountDao accountDao ;
76- private File attachmentsCacheDirectory ;
76+ private File attsCacheDir ;
7777
7878 /**
7979 * Enqueue a new task for {@link PrepareOutgoingMessagesJobIntentService}.
@@ -95,7 +95,7 @@ public static void enqueueWork(Context context, OutgoingMessageInfo outgoingMess
9595 public void onCreate () {
9696 super .onCreate ();
9797 Log .d (TAG , "onCreate" );
98- messageDaoSource = new MessageDaoSource ();
98+ msgDaoSource = new MessageDaoSource ();
9999 accountDao = new AccountDaoSource ().getActiveAccountInformation (getApplicationContext ());
100100 session = OpenStoreHelper .getSessionForAccountDao (getApplicationContext (), accountDao );
101101 }
@@ -116,62 +116,62 @@ public boolean onStopCurrentWork() {
116116 protected void onHandleWork (@ NonNull Intent intent ) {
117117 Log .d (TAG , "onHandleWork" );
118118 if (intent .hasExtra (EXTRA_KEY_OUTGOING_MESSAGE_INFO )) {
119- OutgoingMessageInfo outgoingMessageInfo = intent .getParcelableExtra (EXTRA_KEY_OUTGOING_MESSAGE_INFO );
119+ OutgoingMessageInfo outgoingMsgInfo = intent .getParcelableExtra (EXTRA_KEY_OUTGOING_MESSAGE_INFO );
120+ long uid = outgoingMsgInfo .getUid ();
121+
122+ if (msgDaoSource .getMessage (getApplicationContext (),
123+ accountDao .getEmail (), JavaEmailConstants .FOLDER_OUTBOX , uid ) != null ) {
124+ //todo-DenBond7 need to think about resolving a situation, when a message was created but the
125+ // attachments were not added.
126+ return ;
127+ }
120128
121- Log .d (TAG , "Received a new job: " + outgoingMessageInfo );
129+ Log .d (TAG , "Received a new job: " + outgoingMsgInfo );
122130
123131 setupIfNeed ();
124132
125- updateContactsLastUseDateTime (outgoingMessageInfo );
133+ updateContactsLastUseDateTime (outgoingMsgInfo );
126134
127135 Uri newMessageUri = null ;
128- long generatedUID = -1 ;
129136
130137 try {
131- String [] pubKeys = outgoingMessageInfo .getMessageEncryptionType () == MessageEncryptionType .ENCRYPTED ?
138+ String [] pubKeys = outgoingMsgInfo .getMessageEncryptionType () == MessageEncryptionType .ENCRYPTED ?
132139 SecurityUtils .getRecipientsPubKeys (getApplicationContext (), js , EmailUtil .getAllRecipients
133- (outgoingMessageInfo ), accountDao , outgoingMessageInfo .getFromPgpContact ().getEmail ())
134- : null ;
135-
136- String rawMessage = EmailUtil .generateRawMessageWithoutAttachments (outgoingMessageInfo , js , pubKeys );
137- generatedUID = EmailUtil .generateOutboxUID (getApplicationContext ());
140+ (outgoingMsgInfo ), accountDao , outgoingMsgInfo .getFromPgpContact ().getEmail ()) : null ;
138141
139- MimeMessage mimeMessage = new MimeMessage (session , IOUtils .toInputStream (rawMessage ,
140- StandardCharsets .UTF_8 ));
142+ String rawMsg = EmailUtil .generateRawMessageWithoutAttachments (outgoingMsgInfo , js , pubKeys );
143+ MimeMessage mimeMessage = new MimeMessage (session ,
144+ IOUtils .toInputStream (rawMsg , StandardCharsets .UTF_8 ));
141145
142- File messageAttachmentCacheDirectory = new File (attachmentsCacheDirectory ,
143- UUID .randomUUID ().toString ());
146+ File msgAttsCacheDir = new File (attsCacheDir , UUID .randomUUID ().toString ());
144147
145- ContentValues contentValues = prepareContentValues (outgoingMessageInfo , generatedUID , mimeMessage ,
146- rawMessage , messageAttachmentCacheDirectory );
148+ ContentValues contentValues = prepareContentValues (outgoingMsgInfo , uid , mimeMessage ,
149+ rawMsg , msgAttsCacheDir );
147150
148- newMessageUri = messageDaoSource .addRow (getApplicationContext (), contentValues );
151+ newMessageUri = msgDaoSource .addRow (getApplicationContext (), contentValues );
149152
150153 if (newMessageUri != null ) {
151154 new ImapLabelsDaoSource ().updateLabelMessageCount (getApplicationContext (), accountDao .getEmail (),
152- JavaEmailConstants .FOLDER_OUTBOX , messageDaoSource .getOutboxMessages
153- (getApplicationContext (),
154- accountDao .getEmail ()).size ());
155-
156- if (!CollectionUtils .isEmpty (outgoingMessageInfo .getAttachmentInfoArrayList ())
157- || !CollectionUtils .isEmpty (outgoingMessageInfo .getForwardedAttachmentInfoList ())) {
158- if (!messageAttachmentCacheDirectory .exists ()) {
159- if (!messageAttachmentCacheDirectory .mkdir ()) {
160- Log .e (TAG , "Create cache directory " + attachmentsCacheDirectory .getName () + " filed!" );
161- messageDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
162- JavaEmailConstants .FOLDER_OUTBOX , generatedUID , MessageState
163- .ERROR_CACHE_PROBLEM );
155+ JavaEmailConstants .FOLDER_OUTBOX , msgDaoSource .getOutboxMessages (getApplicationContext (),
156+ accountDao .getEmail ()).size ());
157+
158+ if (!CollectionUtils .isEmpty (outgoingMsgInfo .getAttachmentInfoArrayList ())
159+ || !CollectionUtils .isEmpty (outgoingMsgInfo .getForwardedAttachmentInfoList ())) {
160+ if (!msgAttsCacheDir .exists ()) {
161+ if (!msgAttsCacheDir .mkdir ()) {
162+ Log .e (TAG , "Create cache directory " + attsCacheDir .getName () + " filed!" );
163+ msgDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
164+ JavaEmailConstants .FOLDER_OUTBOX , uid , MessageState .ERROR_CACHE_PROBLEM );
164165 return ;
165166 }
166167 }
167168
168- addAttachmentsToCache (outgoingMessageInfo , generatedUID , pubKeys ,
169- messageAttachmentCacheDirectory );
169+ addAttachmentsToCache (outgoingMsgInfo , uid , pubKeys , msgAttsCacheDir );
170170 }
171171
172- if (CollectionUtils .isEmpty (outgoingMessageInfo .getForwardedAttachmentInfoList ())) {
173- messageDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
174- JavaEmailConstants .FOLDER_OUTBOX , generatedUID , MessageState .QUEUED );
172+ if (CollectionUtils .isEmpty (outgoingMsgInfo .getForwardedAttachmentInfoList ())) {
173+ msgDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
174+ JavaEmailConstants .FOLDER_OUTBOX , uid , MessageState .QUEUED );
175175 MessagesSenderJobService .schedule (getApplicationContext ());
176176 } else {
177177 ForwardedAttachmentsDownloaderJobService .schedule (getApplicationContext ());
@@ -182,14 +182,14 @@ protected void onHandleWork(@NonNull Intent intent) {
182182 ExceptionUtil .handleError (e );
183183
184184 if (newMessageUri != null ) {
185- messageDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
186- JavaEmailConstants .FOLDER_OUTBOX , generatedUID , MessageState .ERROR_DURING_CREATION );
185+ msgDaoSource .updateMessageState (getApplicationContext (), accountDao .getEmail (),
186+ JavaEmailConstants .FOLDER_OUTBOX , uid , MessageState .ERROR_DURING_CREATION );
187187 }
188188 }
189189
190190 if (newMessageUri != null ) {
191191 new ImapLabelsDaoSource ().updateLabelMessageCount (this , accountDao .getEmail (),
192- JavaEmailConstants .FOLDER_OUTBOX , new MessageDaoSource () .getOutboxMessages (this ,
192+ JavaEmailConstants .FOLDER_OUTBOX , msgDaoSource .getOutboxMessages (this ,
193193 accountDao .getEmail ()).size ());
194194 }
195195 }
@@ -301,12 +301,11 @@ private void setupIfNeed() {
301301 }
302302 }
303303
304- if (attachmentsCacheDirectory == null ) {
305- attachmentsCacheDirectory = new File (getCacheDir (), Constants .ATTACHMENTS_CACHE_DIR );
306- if (!attachmentsCacheDirectory .exists ()) {
307- if (!attachmentsCacheDirectory .mkdirs ()) {
308- throw new IllegalStateException ("Create cache directory " + attachmentsCacheDirectory .getName () +
309- " filed!" );
304+ if (attsCacheDir == null ) {
305+ attsCacheDir = new File (getCacheDir (), Constants .ATTACHMENTS_CACHE_DIR );
306+ if (!attsCacheDir .exists ()) {
307+ if (!attsCacheDir .mkdirs ()) {
308+ throw new IllegalStateException ("Create cache directory " + attsCacheDir .getName () + " filed!" );
310309 }
311310 }
312311 }
0 commit comments