Always create system folders during sync.

Change-Id: I8ff6eb4f65d4207d7a2768c5fd837fad8cd47269
This commit is contained in:
Yu Ping Hu 2013-03-20 15:23:51 -07:00
parent b06369ea00
commit 2192bf01e0
3 changed files with 37 additions and 30 deletions

View File

@ -384,6 +384,7 @@ public class ImapStore extends Store {
if (encodedFolder.isEmpty()) continue;
String folderName = decodeFolderName(encodedFolder.getString(), mPathPrefix);
// TODO: Can this special case be eliminated?
if (ImapConstants.INBOX.equalsIgnoreCase(folderName)) continue;
// Parse attributes.
@ -399,10 +400,13 @@ public class ImapStore extends Store {
mailboxes.put(folderName, folder);
}
}
String inboxName = mContext.getString(R.string.mailbox_name_server_inbox);
// In order to properly map INBOX -> Inbox, handle it as a special case.
String inboxName = Mailbox.getSystemMailboxName(mContext, Mailbox.TYPE_INBOX);
Folder newFolder =
addMailbox(mContext, mAccount.mId, inboxName, '\0', true /*selectable*/);
mailboxes.put(ImapConstants.INBOX, (ImapFolder)newFolder);
createHierarchy(mailboxes);
saveMailboxList(mContext, mailboxes);
return mailboxes.values().toArray(new Folder[] {});

View File

@ -103,35 +103,21 @@ public class Pop3Store extends Store {
return folder;
}
private final int[] DEFAULT_FOLDERS = {
Mailbox.TYPE_DRAFTS,
Mailbox.TYPE_OUTBOX,
Mailbox.TYPE_SENT,
Mailbox.TYPE_TRASH
};
@Override
public Folder[] updateFolders() {
String inboxName = mContext.getString(R.string.mailbox_name_server_inbox);
Mailbox mailbox = Mailbox.getMailboxForPath(mContext, mAccount.mId, inboxName);
updateMailbox(mailbox, mAccount.mId, inboxName, '\0', true, Mailbox.TYPE_INBOX);
// Force the parent key to be "no mailbox" for the mail POP3 mailbox
mailbox.mParentKey = Mailbox.NO_MAILBOX;
Mailbox mailbox = Mailbox.restoreMailboxOfType(mContext, mAccount.mId, Mailbox.TYPE_INBOX);
if (mailbox == null) {
mailbox = Mailbox.newSystemMailbox(mContext, mAccount.mId, Mailbox.TYPE_INBOX);
}
// TODO: Mailbox.newSystemMailbox should be aware of these default values for inbox?
mailbox.mFlags = Mailbox.FLAG_HOLDS_MAIL | Mailbox.FLAG_ACCEPTS_MOVED_MAIL;
mailbox.mVisibleLimit = MailActivityEmail.VISIBLE_LIMIT_DEFAULT;
if (mailbox.isSaved()) {
mailbox.update(mContext, mailbox.toContentValues());
} else {
mailbox.save(mContext);
}
// Build default mailboxes as well, in case they're not already made.
for (int type : DEFAULT_FOLDERS) {
if (Mailbox.findMailboxOfType(mContext, mAccount.mId, type) == Mailbox.NO_MAILBOX) {
mailbox = Mailbox.newSystemMailbox(mContext, mAccount.mId, type);
mailbox.save(mContext);
}
}
return new Folder[] { getFolder(inboxName) };
return new Folder[] { getFolder(mailbox.mServerId) };
}
/**

View File

@ -80,6 +80,15 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
public static final String SYNC_EXTRA_MAILBOX_ID = "__mailboxId__";
/** System folders that should always exist. */
private final int[] DEFAULT_FOLDERS = {
Mailbox.TYPE_INBOX,
Mailbox.TYPE_DRAFTS,
Mailbox.TYPE_OUTBOX,
Mailbox.TYPE_SENT,
Mailbox.TYPE_TRASH
};
/** Small projection for just the columns required for a sync. */
private static final String[] MAILBOX_PROJECTION = new String[] {
MailboxColumns.ID,
@ -341,10 +350,21 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
public void updateFolderList(long accountId) throws RemoteException {
Account account = Account.restoreAccountWithId(mContext, accountId);
if (account == null) return;
Mailbox inbox = Mailbox.restoreMailboxOfType(mContext, accountId, Mailbox.TYPE_INBOX);
long inboxId = -1;
TrafficStats.setThreadStatsTag(TrafficFlags.getSyncFlags(mContext, account));
Cursor localFolderCursor = null;
try {
// Step 0: Make sure the default system mailboxes exist.
for (int type : DEFAULT_FOLDERS) {
if (Mailbox.findMailboxOfType(mContext, accountId, type) == Mailbox.NO_MAILBOX) {
Mailbox mailbox = Mailbox.newSystemMailbox(mContext, accountId, type);
mailbox.save(mContext);
if (type == Mailbox.TYPE_INBOX) {
inboxId = mailbox.mId;
}
}
}
// Step 1: Get remote mailboxes
Store store = Store.getInstance(account, mContext);
Folder[] remoteFolders = store.updateFolders();
@ -398,12 +418,9 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
if (localFolderCursor != null) {
localFolderCursor.close();
}
// If this is a first sync, find the inbox and sync it
if (inbox == null) {
inbox = Mailbox.restoreMailboxOfType(mContext, accountId, Mailbox.TYPE_INBOX);
if (inbox != null) {
startSync(inbox.mId, true);
}
// If we just created the inbox, sync it
if (inboxId != -1) {
startSync(inboxId, true);
}
}
}