Build default mailboxes for POP3
Bug: 4580535 Change-Id: Ic800eed387d3e9e64e95cce691d13d98b4a48045
This commit is contained in:
parent
2126caf076
commit
e71a19a523
|
@ -222,6 +222,27 @@ public class Mailbox extends EmailContent implements SyncColumns, MailboxColumns
|
|||
Mailbox.CONTENT_URI, Mailbox.CONTENT_PROJECTION, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new mailbox with "typical" settings for a system mailbox, such as a local "Drafts"
|
||||
* mailbox. This is useful for protocols like POP3 or IMAP who don't have certain local
|
||||
* system mailboxes synced with the server.
|
||||
* Note: the mailbox is not persisted - clients must call {@link #save} themselves.
|
||||
*/
|
||||
public static Mailbox newSystemMailbox(long accountId, int mailboxType, String name) {
|
||||
if (mailboxType == Mailbox.TYPE_MAIL) {
|
||||
throw new IllegalArgumentException("Cannot specify TYPE_MAIL for a system mailbox");
|
||||
}
|
||||
Mailbox box = new Mailbox();
|
||||
box.mAccountKey = accountId;
|
||||
box.mType = mailboxType;
|
||||
box.mSyncInterval = Account.CHECK_INTERVAL_NEVER;
|
||||
box.mFlagVisible = true;
|
||||
box.mServerId = box.mDisplayName = name;
|
||||
box.mParentKey = Mailbox.NO_MAILBOX;
|
||||
box.mFlags = Mailbox.FLAG_HOLDS_MAIL;
|
||||
return box;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Mailbox from the database, given its pathname and account id. All mailbox
|
||||
* paths for a particular account must be unique. Paths are stored in the column
|
||||
|
|
|
@ -493,10 +493,9 @@ public class Controller {
|
|||
/**
|
||||
* Returns the server-side name for a specific mailbox.
|
||||
*
|
||||
* @param mailboxType the mailbox type
|
||||
* @return the resource string corresponding to the mailbox type, empty if not found.
|
||||
*/
|
||||
/* package */ String getMailboxServerName(int mailboxType) {
|
||||
public static String getMailboxServerName(Context context, int mailboxType) {
|
||||
int resId = -1;
|
||||
switch (mailboxType) {
|
||||
case Mailbox.TYPE_INBOX:
|
||||
|
@ -518,7 +517,7 @@ public class Controller {
|
|||
resId = R.string.mailbox_name_server_junk;
|
||||
break;
|
||||
}
|
||||
return resId != -1 ? mContext.getString(resId) : "";
|
||||
return resId != -1 ? context.getString(resId) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -532,17 +531,8 @@ public class Controller {
|
|||
Log.e(Logging.LOG_TAG, mes);
|
||||
throw new RuntimeException(mes);
|
||||
}
|
||||
Mailbox box = new Mailbox();
|
||||
box.mAccountKey = accountId;
|
||||
box.mType = mailboxType;
|
||||
box.mSyncInterval = Account.CHECK_INTERVAL_NEVER;
|
||||
box.mFlagVisible = true;
|
||||
box.mServerId = box.mDisplayName = getMailboxServerName(mailboxType);
|
||||
// All system mailboxes are off the top-level & can hold mail
|
||||
if (mailboxType != Mailbox.TYPE_MAIL) {
|
||||
box.mParentKey = Mailbox.NO_MAILBOX;
|
||||
box.mFlags = Mailbox.FLAG_HOLDS_MAIL;
|
||||
}
|
||||
Mailbox box = Mailbox.newSystemMailbox(
|
||||
accountId, mailboxType, getMailboxServerName(mContext, mailboxType));
|
||||
box.save(mProviderContext);
|
||||
return box.mId;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import android.content.Context;
|
|||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.email.Controller;
|
||||
import com.android.email.Email;
|
||||
import com.android.email.mail.Store;
|
||||
import com.android.email.mail.Transport;
|
||||
|
@ -150,6 +151,13 @@ 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() {
|
||||
Mailbox mailbox = Mailbox.getMailboxForPath(mContext, mAccount.mId, POP3_MAILBOX_NAME);
|
||||
|
@ -161,6 +169,15 @@ public class Pop3Store extends Store {
|
|||
} 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) {
|
||||
String name = Controller.getMailboxServerName(mContext, type);
|
||||
Mailbox.newSystemMailbox(mAccount.mId, type, name).save(mContext);
|
||||
}
|
||||
}
|
||||
|
||||
return new Folder[] { getFolder(POP3_MAILBOX_NAME) };
|
||||
}
|
||||
|
||||
|
@ -701,7 +718,8 @@ public class Pop3Store extends Store {
|
|||
for (int i = 0, count = messages.length; i < count; i++) {
|
||||
Message message = messages[i];
|
||||
if (!(message instanceof Pop3Message)) {
|
||||
throw new MessagingException("Pop3Store.fetch called with non-Pop3 Message");
|
||||
throw new MessagingException(
|
||||
"Pop3Store.fetch called with non-Pop3 Message");
|
||||
}
|
||||
Pop3Message pop3Message = (Pop3Message)message;
|
||||
String response = executeSimpleCommand(String.format("LIST %d",
|
||||
|
@ -944,8 +962,7 @@ public class Pop3Store extends Store {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Message[] getMessages(SearchParams params, MessageRetrievalListener listener)
|
||||
throws MessagingException {
|
||||
public Message[] getMessages(SearchParams params, MessageRetrievalListener listener) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,22 +85,22 @@ public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider>
|
|||
* These are strings that should not change per locale.
|
||||
*/
|
||||
public void testGetMailboxServerName() {
|
||||
assertEquals("", mTestController.getMailboxServerName(-1));
|
||||
assertEquals("", Controller.getMailboxServerName(mContext, -1));
|
||||
|
||||
assertEquals("Inbox", mTestController.getMailboxServerName(Mailbox.TYPE_INBOX));
|
||||
assertEquals("Outbox", mTestController.getMailboxServerName(Mailbox.TYPE_OUTBOX));
|
||||
assertEquals("Trash", mTestController.getMailboxServerName(Mailbox.TYPE_TRASH));
|
||||
assertEquals("Sent", mTestController.getMailboxServerName(Mailbox.TYPE_SENT));
|
||||
assertEquals("Junk", mTestController.getMailboxServerName(Mailbox.TYPE_JUNK));
|
||||
assertEquals("Inbox", Controller.getMailboxServerName(mContext, Mailbox.TYPE_INBOX));
|
||||
assertEquals("Outbox", Controller.getMailboxServerName(mContext, Mailbox.TYPE_OUTBOX));
|
||||
assertEquals("Trash", Controller.getMailboxServerName(mContext, Mailbox.TYPE_TRASH));
|
||||
assertEquals("Sent", Controller.getMailboxServerName(mContext, Mailbox.TYPE_SENT));
|
||||
assertEquals("Junk", Controller.getMailboxServerName(mContext, Mailbox.TYPE_JUNK));
|
||||
|
||||
// Now try again with translation
|
||||
Locale savedLocale = Locale.getDefault();
|
||||
Locale.setDefault(Locale.FRANCE);
|
||||
assertEquals("Inbox", mTestController.getMailboxServerName(Mailbox.TYPE_INBOX));
|
||||
assertEquals("Outbox", mTestController.getMailboxServerName(Mailbox.TYPE_OUTBOX));
|
||||
assertEquals("Trash", mTestController.getMailboxServerName(Mailbox.TYPE_TRASH));
|
||||
assertEquals("Sent", mTestController.getMailboxServerName(Mailbox.TYPE_SENT));
|
||||
assertEquals("Junk", mTestController.getMailboxServerName(Mailbox.TYPE_JUNK));
|
||||
assertEquals("Inbox", Controller.getMailboxServerName(mContext, Mailbox.TYPE_INBOX));
|
||||
assertEquals("Outbox", Controller.getMailboxServerName(mContext, Mailbox.TYPE_OUTBOX));
|
||||
assertEquals("Trash", Controller.getMailboxServerName(mContext, Mailbox.TYPE_TRASH));
|
||||
assertEquals("Sent", Controller.getMailboxServerName(mContext, Mailbox.TYPE_SENT));
|
||||
assertEquals("Junk", Controller.getMailboxServerName(mContext, Mailbox.TYPE_JUNK));
|
||||
Locale.setDefault(savedLocale);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue