Skip to content

Commit 1be5ace

Browse files
committed
Added save AttachmentInfo to the local database when load the message details. | #55.
1 parent 343c768 commit 1be5ace

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

FlowCrypt/src/main/java/com/flowcrypt/email/database/provider/SecurityContentProvider.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.flowcrypt.email.database.dao.source.AccountDaoSource;
2222
import com.flowcrypt.email.database.dao.source.ContactsDaoSource;
2323
import com.flowcrypt.email.database.dao.source.KeysDaoSource;
24+
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource;
2425
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource;
2526
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource;
2627

@@ -46,6 +47,8 @@ public class SecurityContentProvider extends ContentProvider {
4647
private static final int MATCHED_CODE_IMAP_MESSAGES_SINGLE_ROW = 9;
4748
private static final int MATCHED_CODE_ACCOUNTS_TABLE = 10;
4849
private static final int MATCHED_CODE_ACCOUNTS_SINGLE_ROW = 11;
50+
private static final int MATCHED_CODE_ATTACHMENT_TABLE = 12;
51+
private static final int MATCHED_CODE_ATTACHMENT_SINGLE_ROW = 13;
4952

5053
private static final String SINGLE_APPENDED_SUFFIX = "/#";
5154
private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
@@ -75,6 +78,10 @@ public class SecurityContentProvider extends ContentProvider {
7578
MATCHED_CODE_ACCOUNTS_TABLE);
7679
URI_MATCHER.addURI(FlowcryptContract.AUTHORITY, AccountDaoSource.TABLE_NAME_ACCOUNTS +
7780
SINGLE_APPENDED_SUFFIX, MATCHED_CODE_ACCOUNTS_SINGLE_ROW);
81+
URI_MATCHER.addURI(FlowcryptContract.AUTHORITY, AttachmentDaoSource.TABLE_NAME_ATTACHMENT,
82+
MATCHED_CODE_ATTACHMENT_TABLE);
83+
URI_MATCHER.addURI(FlowcryptContract.AUTHORITY, AttachmentDaoSource.TABLE_NAME_ATTACHMENT +
84+
SINGLE_APPENDED_SUFFIX, MATCHED_CODE_ATTACHMENT_SINGLE_ROW);
7885
}
7986

8087
private FlowCryptSQLiteOpenHelper hotelDBHelper;
@@ -119,24 +126,29 @@ public Uri insert(@NonNull Uri uri, ContentValues values) {
119126
case MATCHED_CODE_IMAP_LABELS_TABLE:
120127
id = sqLiteDatabase.insert(new ImapLabelsDaoSource().getTableName(), null,
121128
values);
122-
result = Uri.parse(new ImapLabelsDaoSource().getBaseContentUri() + "/" +
123-
id);
129+
result = Uri.parse(new ImapLabelsDaoSource().getBaseContentUri()
130+
+ "/" + id);
124131
break;
125132

126133
case MATCHED_CODE_IMAP_MESSAGES_TABLE:
127134
id = sqLiteDatabase.insert(new MessageDaoSource().getTableName(),
128135
null, values);
129136
result = Uri.parse(new MessageDaoSource().getBaseContentUri()
130-
+ "/" +
131-
id);
137+
+ "/" + id);
132138
break;
133139

134140
case MATCHED_CODE_ACCOUNTS_TABLE:
135141
id = sqLiteDatabase.insert(new AccountDaoSource().getTableName(),
136142
null, values);
137143
result = Uri.parse(new AccountDaoSource().getBaseContentUri()
138-
+ "/" +
139-
id);
144+
+ "/" + id);
145+
break;
146+
147+
case MATCHED_CODE_ATTACHMENT_TABLE:
148+
id = sqLiteDatabase.insert(new AttachmentDaoSource().getTableName(),
149+
null, values);
150+
result = Uri.parse(new AttachmentDaoSource().getBaseContentUri()
151+
+ "/" + id);
140152
break;
141153

142154
default:
@@ -235,6 +247,19 @@ public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
235247
}
236248
break;
237249

250+
case MATCHED_CODE_ATTACHMENT_TABLE:
251+
for (ContentValues contentValues : values) {
252+
long id = sqLiteDatabase.insert(
253+
new AttachmentDaoSource().getTableName(), null,
254+
contentValues);
255+
if (id <= 0) {
256+
throw new SQLException("Failed to insert row into " + uri);
257+
} else {
258+
insertedRowsCount++;
259+
}
260+
}
261+
break;
262+
238263
default:
239264
throw new UnsupportedOperationException("Unknown uri: " + uri);
240265
}
@@ -305,6 +330,14 @@ public int update(@NonNull Uri uri, ContentValues values, String selection,
305330
selectionArgs);
306331
break;
307332

333+
case MATCHED_CODE_ATTACHMENT_TABLE:
334+
rowsCount = sqLiteDatabase.update(
335+
new AttachmentDaoSource().getTableName(),
336+
values,
337+
selection,
338+
selectionArgs);
339+
break;
340+
308341
default:
309342
throw new UnsupportedOperationException("Unknown uri: " + uri);
310343
}
@@ -358,6 +391,11 @@ public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
358391
selection, selectionArgs);
359392
break;
360393

394+
case MATCHED_CODE_ATTACHMENT_TABLE:
395+
rowsCount = sqLiteDatabase.delete(new AttachmentDaoSource().getTableName(),
396+
selection, selectionArgs);
397+
break;
398+
361399
default:
362400
throw new UnsupportedOperationException("Unknown uri: " + uri);
363401
}
@@ -403,6 +441,10 @@ public Cursor query(@NonNull Uri uri, String[] projection, String selection,
403441
table = AccountDaoSource.TABLE_NAME_ACCOUNTS;
404442
break;
405443

444+
case MATCHED_CODE_ATTACHMENT_TABLE:
445+
table = AttachmentDaoSource.TABLE_NAME_ATTACHMENT;
446+
break;
447+
406448
default:
407449
throw new UnsupportedOperationException("Unknown uri: " + uri);
408450
}
@@ -451,6 +493,12 @@ public String getType(@NonNull Uri uri) {
451493
case MATCHED_CODE_ACCOUNTS_SINGLE_ROW:
452494
return new AccountDaoSource().getSingleRowContentType();
453495

496+
case MATCHED_CODE_ATTACHMENT_TABLE:
497+
return new AttachmentDaoSource().getRowsContentType();
498+
499+
case MATCHED_CODE_ATTACHMENT_SINGLE_ROW:
500+
return new AttachmentDaoSource().getSingleRowContentType();
501+
454502
default:
455503
throw new IllegalArgumentException("Unknown uri: " + uri);
456504
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.flowcrypt.email.api.email.model.AttachmentInfo;
2626
import com.flowcrypt.email.api.email.sync.GmailSynsManager;
2727
import com.flowcrypt.email.api.email.sync.SyncListener;
28+
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource;
2829
import com.flowcrypt.email.database.dao.source.imap.ImapLabelsDaoSource;
2930
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource;
3031
import com.flowcrypt.email.model.EmailAndNamePair;
@@ -238,6 +239,12 @@ public void onMessageDetailsReceived(IMAPFolder imapFolder, long uid,
238239
uid,
239240
rawMessageWithOutAttachments);
240241

242+
new AttachmentDaoSource().addRows(getApplicationContext(),
243+
account.name,
244+
folder.getFolderAlias(),
245+
uid,
246+
attachmentInfoList);
247+
241248
if (TextUtils.isEmpty(rawMessageWithOutAttachments)) {
242249
sendReply(ownerKey, requestCode, REPLY_RESULT_CODE_ACTION_ERROR);
243250
} else {

FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/EmailListFragment.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.flowcrypt.email.R;
2828
import com.flowcrypt.email.api.email.Folder;
29+
import com.flowcrypt.email.database.dao.source.imap.AttachmentDaoSource;
2930
import com.flowcrypt.email.database.dao.source.imap.MessageDaoSource;
3031
import com.flowcrypt.email.ui.activity.MessageDetailsActivity;
3132
import com.flowcrypt.email.ui.activity.base.BaseSyncActivity;
@@ -316,10 +317,7 @@ public void onErrorOccurred(int requestCode, int errorType) {
316317
emptyView.setVisibility(View.GONE);
317318

318319
getLoaderManager().destroyLoader(R.id.loader_id_load_gmail_messages);
319-
new MessageDaoSource().deleteCachedMessagesOfFolder(
320-
getContext(),
321-
onManageEmailsListener.getCurrentAccount().name,
322-
onManageEmailsListener.getCurrentFolder().getFolderAlias());
320+
cleanCache();
323321
}
324322

325323
/**
@@ -340,10 +338,7 @@ public void updateList(boolean isFolderChanged) {
340338
}
341339

342340
getLoaderManager().destroyLoader(R.id.loader_id_load_gmail_messages);
343-
new MessageDaoSource().deleteCachedMessagesOfFolder(
344-
getContext(),
345-
onManageEmailsListener.getCurrentAccount().name,
346-
onManageEmailsListener.getCurrentFolder().getFolderAlias());
341+
cleanCache();
347342
}
348343

349344
getLoaderManager().restartLoader(R.id.loader_id_load_gmail_messages, null,
@@ -368,6 +363,19 @@ public void onNextMessagesLoaded(boolean isNeedToUpdateList) {
368363
}
369364
}
370365

366+
/**
367+
* Clean the cache information about some folder.
368+
*/
369+
private void cleanCache() {
370+
new MessageDaoSource().deleteCachedMessagesOfFolder(
371+
getContext(),
372+
onManageEmailsListener.getCurrentAccount().name,
373+
onManageEmailsListener.getCurrentFolder().getFolderAlias());
374+
new AttachmentDaoSource().deleteCachedAttachmentInfoOfFolder(getContext(),
375+
onManageEmailsListener.getCurrentAccount().name,
376+
onManageEmailsListener.getCurrentFolder().getFolderAlias());
377+
}
378+
371379
/**
372380
* Try to load a new messages from IMAP server.
373381
*/

0 commit comments

Comments
 (0)