Skip to content

Commit f6a3f89

Browse files
committed
Added show is a message has attachments when display the emails list. | #55.
1 parent 1be5ace commit f6a3f89

File tree

17 files changed

+220
-120
lines changed

17 files changed

+220
-120
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/GeneralMessageDetails.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public GeneralMessageDetails[] newArray(int size) {
3535
return new GeneralMessageDetails[size];
3636
}
3737
};
38+
3839
private String email;
3940
private String label;
4041
private long uid;
@@ -45,6 +46,7 @@ public GeneralMessageDetails[] newArray(int size) {
4546
private String subject;
4647
private String[] flags;
4748
private String rawMessageWithoutAttachments;
49+
private boolean isMessageHasAttachment;
4850

4951
public GeneralMessageDetails() {
5052
}
@@ -60,6 +62,24 @@ protected GeneralMessageDetails(Parcel in) {
6062
this.subject = in.readString();
6163
this.flags = in.createStringArray();
6264
this.rawMessageWithoutAttachments = in.readString();
65+
this.isMessageHasAttachment = in.readByte() != 0;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "GeneralMessageDetails{" +
71+
"email='" + email + '\'' +
72+
", label='" + label + '\'' +
73+
", uid=" + uid +
74+
", receivedDateInMillisecond=" + receivedDateInMillisecond +
75+
", sentDateInMillisecond=" + sentDateInMillisecond +
76+
", from=" + Arrays.toString(from) +
77+
", to=" + Arrays.toString(to) +
78+
", subject='" + subject + '\'' +
79+
", flags=" + Arrays.toString(flags) +
80+
", rawMessageWithoutAttachments =" + TextUtils.isEmpty
81+
(rawMessageWithoutAttachments) +
82+
'}';
6383
}
6484

6585
@Override
@@ -79,23 +99,7 @@ public void writeToParcel(Parcel dest, int flags) {
7999
dest.writeString(this.subject);
80100
dest.writeStringArray(this.flags);
81101
dest.writeString(this.rawMessageWithoutAttachments);
82-
}
83-
84-
@Override
85-
public String toString() {
86-
return "GeneralMessageDetails{" +
87-
"email='" + email + '\'' +
88-
", label='" + label + '\'' +
89-
", uid=" + uid +
90-
", receivedDateInMillisecond=" + receivedDateInMillisecond +
91-
", sentDateInMillisecond=" + sentDateInMillisecond +
92-
", from=" + Arrays.toString(from) +
93-
", to=" + Arrays.toString(to) +
94-
", subject='" + subject + '\'' +
95-
", flags=" + Arrays.toString(flags) +
96-
", rawMessageWithoutAttachments =" + TextUtils.isEmpty
97-
(rawMessageWithoutAttachments) +
98-
'}';
102+
dest.writeByte(this.isMessageHasAttachment ? (byte) 1 : (byte) 0);
99103
}
100104

101105
public String getEmail() {
@@ -181,4 +185,12 @@ public String getRawMessageWithoutAttachments() {
181185
public void setRawMessageWithoutAttachments(String rawMessageWithoutAttachments) {
182186
this.rawMessageWithoutAttachments = rawMessageWithoutAttachments;
183187
}
188+
189+
public boolean isMessageHasAttachment() {
190+
return isMessageHasAttachment;
191+
}
192+
193+
public void setMessageHasAttachment(boolean messageHasAttachment) {
194+
isMessageHasAttachment = messageHasAttachment;
195+
}
184196
}

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

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

99
import android.content.Context;
1010

11-
import com.flowcrypt.email.api.email.model.AttachmentInfo;
1211
import com.google.android.gms.auth.GoogleAuthException;
1312
import com.sun.mail.imap.IMAPFolder;
1413

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

9894
/**
9995
* 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: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@
88

99
import android.os.Messenger;
1010

11-
import com.flowcrypt.email.api.email.model.AttachmentInfo;
1211
import com.flowcrypt.email.api.email.sync.SyncListener;
1312
import com.sun.mail.gimap.GmailSSLStore;
1413
import com.sun.mail.iap.Argument;
1514
import com.sun.mail.iap.ProtocolException;
1615
import com.sun.mail.iap.Response;
1716
import com.sun.mail.imap.IMAPFolder;
1817
import com.sun.mail.imap.protocol.BODY;
19-
import com.sun.mail.imap.protocol.BODYSTRUCTURE;
2018
import com.sun.mail.imap.protocol.FetchResponse;
2119
import com.sun.mail.imap.protocol.IMAPProtocol;
2220
import com.sun.mail.util.ASCIIUtility;
2321

24-
import java.util.ArrayList;
25-
import java.util.List;
26-
2722
import javax.mail.Folder;
2823
import javax.mail.Message;
29-
import javax.mail.Part;
3024

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

6256
@Override
63-
public void run(GmailSSLStore gmailSSLStore, final SyncListener syncListener) throws Exception {
57+
public void run(GmailSSLStore gmailSSLStore, SyncListener syncListener) throws Exception {
6458
IMAPFolder imapFolder = (IMAPFolder) gmailSSLStore.getFolder(folderName);
6559
imapFolder.open(Folder.READ_WRITE);
6660

6761
if (syncListener != null) {
68-
MessageDetails messageDetails = (MessageDetails) imapFolder.doCommand(new IMAPFolder
69-
.ProtocolCommand() {
62+
String rawMessage = (String) imapFolder.doCommand(new IMAPFolder.ProtocolCommand() {
7063
public Object doCommand(IMAPProtocol imapProtocol)
7164
throws ProtocolException {
7265
String rawMessage = null;
73-
List<AttachmentInfo> attachmentInfoList = new ArrayList<>();
7466

7567
Argument args = new Argument();
7668
Argument list = new Argument();
77-
list.writeString("BODYSTRUCTURE");
7869
list.writeString("RFC822.SIZE");
7970
list.writeString("BODY[]<0.204800>");
8071
args.writeArgument(list);
@@ -93,58 +84,20 @@ public Object doCommand(IMAPProtocol imapProtocol)
9384
if (body != null && body.getByteArrayInputStream() != null) {
9485
rawMessage = ASCIIUtility.toString(body.getByteArrayInputStream());
9586
}
96-
97-
getInformationAboutAttachments(attachmentInfoList,
98-
fetchResponse.getItem(BODYSTRUCTURE.class));
9987
}
10088
}
10189

10290
imapProtocol.notifyResponseHandlers(responses);
10391
imapProtocol.handleResult(serverStatusResponse);
10492

105-
return new MessageDetails(rawMessage, attachmentInfoList);
93+
return rawMessage;
10694
}
10795
});
10896

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

113101
imapFolder.close(false);
114102
}
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-
}
150103
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void run(GmailSSLStore gmailSSLStore, SyncListener syncListener) throws E
5959
FetchProfile fetchProfile = new FetchProfile();
6060
fetchProfile.add(FetchProfile.Item.ENVELOPE);
6161
fetchProfile.add(FetchProfile.Item.FLAGS);
62+
fetchProfile.add(FetchProfile.Item.CONTENT_INFO);
6263
fetchProfile.add(UIDFolder.FetchProfileItem.UID);
6364
imapFolder.fetch(messages, fetchProfile);
6465

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void run(GmailSSLStore gmailSSLStore, SyncListener syncListener) throws E
7272
FetchProfile fetchProfile = new FetchProfile();
7373
fetchProfile.add(FetchProfile.Item.ENVELOPE);
7474
fetchProfile.add(FetchProfile.Item.FLAGS);
75+
fetchProfile.add(FetchProfile.Item.CONTENT_INFO);
7576
fetchProfile.add(UIDFolder.FetchProfileItem.UID);
7677
imapFolder.fetch(messages, fetchProfile);
7778

FlowCrypt/src/main/java/com/flowcrypt/email/database/FlowCryptSQLiteOpenHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,9 @@ public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVers
8383
private void upgradeDatabaseFrom1To2Version(SQLiteDatabase sqLiteDatabase) {
8484
sqLiteDatabase.execSQL(AttachmentDaoSource.ATTACHMENT_TABLE_SQL_CREATE);
8585
sqLiteDatabase.execSQL(AttachmentDaoSource.CREATE_INDEX_EMAIL_UID_FOLDER_IN_MESSAGES);
86+
87+
sqLiteDatabase.execSQL("ALTER TABLE " + MessageDaoSource.TABLE_NAME_MESSAGES +
88+
" ADD COLUMN " + MessageDaoSource.COL_IS_MESSAGE_HAS_ATTACHMENTS
89+
+ " INTEGER DEFAULT 0;");
8690
}
8791
}

FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/source/imap/AttachmentDaoSource.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ public class AttachmentDaoSource extends BaseDaoSource {
5252
+ "_in_" + TABLE_NAME_ATTACHMENT + " ON " + TABLE_NAME_ATTACHMENT +
5353
" (" + COL_EMAIL + ", " + COL_UID + ", " + COL_FOLDER + ")";
5454

55+
/**
56+
* Prepare the content values for insert to the database.
57+
*
58+
* @param email The email that the message linked.
59+
* @param label The folder label.
60+
* @param uid The message UID.
61+
* @param attachmentInfo The attachment info which will be added to the database.
62+
* @return generated {@link ContentValues}
63+
*/
64+
@NonNull
65+
public static ContentValues prepareContentValues(String email, String label, long uid,
66+
AttachmentInfo attachmentInfo) {
67+
ContentValues contentValues = new ContentValues();
68+
contentValues.put(COL_EMAIL, email);
69+
contentValues.put(COL_FOLDER, label);
70+
contentValues.put(COL_UID, uid);
71+
contentValues.put(COL_NAME, attachmentInfo.getName());
72+
contentValues.put(COL_ENCODED_SIZE_IN_BYTES, attachmentInfo.getEncodedSize());
73+
contentValues.put(COL_TYPE, attachmentInfo.getType());
74+
return contentValues;
75+
}
76+
5577
@Override
5678
public String getTableName() {
5779
return TABLE_NAME_ATTACHMENT;
@@ -106,6 +128,20 @@ public int addRows(Context context, String email, String label, long uid,
106128
} else return 0;
107129
}
108130

131+
/**
132+
* This method add rows per single transaction.
133+
*
134+
* @param context Interface to global information about an application environment.
135+
* @param contentValues The array of prepared {@link ContentValues}.
136+
* @return the number of newly created rows.
137+
*/
138+
public int addRows(Context context, ContentValues[] contentValues) {
139+
if (contentValues != null) {
140+
ContentResolver contentResolver = context.getContentResolver();
141+
return contentResolver.bulkInsert(getBaseContentUri(), contentValues);
142+
} else return 0;
143+
}
144+
109145
/**
110146
* Generate an {@link AttachmentInfo} object from the current cursor position.
111147
*
@@ -162,26 +198,4 @@ public int deleteCachedAttachmentInfoOfFolder(Context context, String email, Str
162198
+ COL_FOLDER + " = ?", new String[]{email, label});
163199
} else return -1;
164200
}
165-
166-
/**
167-
* Prepare the content values for insert to the database.
168-
*
169-
* @param email The email that the message linked.
170-
* @param label The folder label.
171-
* @param uid The message UID.
172-
* @param attachmentInfo The attachment info which will be added to the database.
173-
* @return generated {@link ContentValues}
174-
*/
175-
@NonNull
176-
private ContentValues prepareContentValues(String email, String label, long uid,
177-
AttachmentInfo attachmentInfo) {
178-
ContentValues contentValues = new ContentValues();
179-
contentValues.put(COL_EMAIL, email);
180-
contentValues.put(COL_FOLDER, label);
181-
contentValues.put(COL_UID, uid);
182-
contentValues.put(COL_NAME, attachmentInfo.getName());
183-
contentValues.put(COL_ENCODED_SIZE_IN_BYTES, attachmentInfo.getEncodedSize());
184-
contentValues.put(COL_TYPE, attachmentInfo.getType());
185-
return contentValues;
186-
}
187201
}

0 commit comments

Comments
 (0)