2009-03-04 03:32:22 +00:00
|
|
|
/*
|
2012-08-23 05:25:42 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source P-roject
|
2009-03-04 03:32:22 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.mail;
|
|
|
|
|
2011-06-08 20:50:00 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
2012-08-23 05:25:42 +00:00
|
|
|
import com.android.email.R;
|
2012-09-20 20:34:13 +00:00
|
|
|
import com.android.email.mail.store.ImapStore;
|
2011-06-29 21:47:21 +00:00
|
|
|
import com.android.email.mail.store.Pop3Store;
|
2012-06-28 17:40:46 +00:00
|
|
|
import com.android.email.mail.store.ServiceStore;
|
2012-09-18 22:07:27 +00:00
|
|
|
import com.android.email.mail.transport.MailTransport;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
2011-02-10 02:47:43 +00:00
|
|
|
import com.android.emailcommon.mail.Folder;
|
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
2011-06-13 22:32:27 +00:00
|
|
|
import com.android.emailcommon.provider.Account;
|
2011-06-08 20:50:00 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent;
|
2011-05-19 22:18:12 +00:00
|
|
|
import com.android.emailcommon.provider.HostAuth;
|
2011-05-14 00:26:27 +00:00
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
2013-05-26 04:32:32 +00:00
|
|
|
import com.android.mail.utils.LogUtils;
|
2011-04-19 18:32:06 +00:00
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2011-06-29 21:47:21 +00:00
|
|
|
import java.lang.reflect.Method;
|
2010-05-07 21:27:44 +00:00
|
|
|
import java.util.HashMap;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-29 21:47:21 +00:00
|
|
|
* Store is the legacy equivalent of the Account class
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
|
|
|
public abstract class Store {
|
|
|
|
/**
|
|
|
|
* A global suggestion to Store implementors on how much of the body
|
2012-08-02 17:53:40 +00:00
|
|
|
* should be returned on FetchProfile.Item.BODY_SANE requests. We'll use 125k now.
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
2012-08-02 17:53:40 +00:00
|
|
|
public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (125 * 1024);
|
2011-05-17 00:47:14 +00:00
|
|
|
|
2011-04-19 18:32:06 +00:00
|
|
|
@VisibleForTesting
|
2011-06-16 15:59:19 +00:00
|
|
|
static final HashMap<HostAuth, Store> sStores = new HashMap<HostAuth, Store>();
|
2011-05-16 21:47:49 +00:00
|
|
|
protected Context mContext;
|
|
|
|
protected Account mAccount;
|
2012-09-18 22:07:27 +00:00
|
|
|
protected MailTransport mTransport;
|
2011-05-16 21:47:49 +00:00
|
|
|
protected String mUsername;
|
|
|
|
protected String mPassword;
|
|
|
|
|
2011-06-29 21:47:21 +00:00
|
|
|
static final HashMap<String, Class<? extends Store>> sStoreClasses =
|
|
|
|
new HashMap<String, Class<? extends Store>>();
|
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
/**
|
|
|
|
* Static named constructor. It should be overrode by extending class.
|
2010-05-20 23:11:26 +00:00
|
|
|
* Because this method will be called through reflection, it can not be protected.
|
2009-03-27 00:05:25 +00:00
|
|
|
*/
|
2013-06-25 22:15:02 +00:00
|
|
|
static Store newInstance(Account account) throws MessagingException {
|
2011-04-19 18:32:06 +00:00
|
|
|
throw new MessagingException("Store#newInstance: Unknown scheme in "
|
|
|
|
+ account.mDisplayName);
|
2009-03-27 00:05:25 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 18:32:06 +00:00
|
|
|
/**
|
|
|
|
* Get an instance of a mail store for the given account. The account must be valid (i.e. has
|
|
|
|
* at least an incoming server name).
|
2010-05-20 23:11:26 +00:00
|
|
|
*
|
2011-06-16 15:59:19 +00:00
|
|
|
* NOTE: The internal algorithm used to find a cached store depends upon the account's
|
|
|
|
* HostAuth row. If this ever changes (e.g. such as the user updating the
|
2011-05-17 00:47:14 +00:00
|
|
|
* host name or port), we will leak entries. This should not be typical, so, it is not
|
|
|
|
* a critical problem. However, it is something we should consider fixing.
|
2009-03-04 03:32:22 +00:00
|
|
|
*
|
2011-04-19 18:32:06 +00:00
|
|
|
* @param account The account of the store.
|
2014-06-09 22:28:11 +00:00
|
|
|
* @param context For all the usual context-y stuff
|
2009-03-04 03:32:22 +00:00
|
|
|
* @return an initialized store of the appropriate class
|
2011-04-19 18:32:06 +00:00
|
|
|
* @throws MessagingException If the store cannot be obtained or if the account is invalid.
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
2011-06-29 19:47:56 +00:00
|
|
|
public synchronized static Store getInstance(Account account, Context context)
|
|
|
|
throws MessagingException {
|
2012-08-23 05:25:42 +00:00
|
|
|
if (sStores.isEmpty()) {
|
|
|
|
sStoreClasses.put(context.getString(R.string.protocol_pop3), Pop3Store.class);
|
2012-09-20 20:34:13 +00:00
|
|
|
sStoreClasses.put(context.getString(R.string.protocol_legacy_imap), ImapStore.class);
|
2012-08-23 05:25:42 +00:00
|
|
|
}
|
2011-06-16 15:59:19 +00:00
|
|
|
HostAuth hostAuth = account.getOrCreateHostAuthRecv(context);
|
2011-10-18 20:27:30 +00:00
|
|
|
// An existing account might have been deleted
|
|
|
|
if (hostAuth == null) return null;
|
2014-06-09 22:28:11 +00:00
|
|
|
if (!account.isTemporary()) {
|
|
|
|
Store store = sStores.get(hostAuth);
|
|
|
|
if (store == null) {
|
|
|
|
store = createInstanceInternal(account, context, true);
|
|
|
|
} else {
|
|
|
|
// Make sure the account object is up to date (according to the caller, at least)
|
|
|
|
store.mAccount = account;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2014-06-09 22:28:11 +00:00
|
|
|
return store;
|
|
|
|
} else {
|
|
|
|
return createInstanceInternal(account, context, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized static Store createInstanceInternal(final Account account,
|
|
|
|
final Context context, final boolean cacheInstance)
|
|
|
|
throws MessagingException {
|
|
|
|
Context appContext = context.getApplicationContext();
|
|
|
|
final HostAuth hostAuth = account.getOrCreateHostAuthRecv(context);
|
|
|
|
Class<? extends Store> klass = sStoreClasses.get(hostAuth.mProtocol);
|
|
|
|
if (klass == null) {
|
|
|
|
klass = ServiceStore.class;
|
|
|
|
}
|
|
|
|
final Store store;
|
|
|
|
try {
|
|
|
|
// invoke "newInstance" class method
|
|
|
|
Method m = klass.getMethod("newInstance", Account.class, Context.class);
|
|
|
|
store = (Store)m.invoke(null, account, appContext);
|
|
|
|
} catch (Exception e) {
|
|
|
|
LogUtils.d(Logging.LOG_TAG, String.format(
|
|
|
|
"exception %s invoking method %s#newInstance(Account, Context) for %s",
|
|
|
|
e.toString(), klass.getName(), account.mDisplayName));
|
|
|
|
throw new MessagingException("Can't instantiate Store for " + account.mDisplayName);
|
|
|
|
}
|
|
|
|
// Don't cache this unless it's we've got a saved HostAuth
|
|
|
|
if (hostAuth.mId != EmailContent.NOT_SAVED && cacheInstance) {
|
|
|
|
sStores.put(hostAuth, store);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
return store;
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-05-07 00:42:22 +00:00
|
|
|
/**
|
2011-04-19 18:32:06 +00:00
|
|
|
* Delete the mail store associated with the given account. The account must be valid (i.e. has
|
|
|
|
* at least an incoming server name).
|
2010-05-20 23:11:26 +00:00
|
|
|
*
|
2009-05-07 00:42:22 +00:00
|
|
|
* The store should have been notified already by calling delete(), and the caller should
|
|
|
|
* also take responsibility for deleting the matching LocalStore, etc.
|
2011-04-19 18:32:06 +00:00
|
|
|
*
|
|
|
|
* @throws MessagingException If the store cannot be removed or if the account is invalid.
|
2009-05-07 00:42:22 +00:00
|
|
|
*/
|
2011-05-17 00:47:14 +00:00
|
|
|
public synchronized static Store removeInstance(Account account, Context context)
|
2011-04-19 18:32:06 +00:00
|
|
|
throws MessagingException {
|
2011-06-16 15:59:19 +00:00
|
|
|
return sStores.remove(HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
|
2009-05-07 00:42:22 +00:00
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-05-01 23:36:34 +00:00
|
|
|
/**
|
|
|
|
* Some protocols require that a sent message be copied (uploaded) into the Sent folder
|
|
|
|
* while others can take care of it automatically (ideally, on the server). This function
|
|
|
|
* allows a given store to indicate which mode(s) it supports.
|
|
|
|
* @return true if the store requires an upload into "sent", false if this happens automatically
|
|
|
|
* for any sent message.
|
|
|
|
*/
|
|
|
|
public boolean requireCopyMessageToSentFolder() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2011-06-27 19:12:41 +00:00
|
|
|
public Folder getFolder(String name) throws MessagingException {
|
|
|
|
return null;
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2011-04-20 23:22:41 +00:00
|
|
|
/**
|
|
|
|
* Updates the local list of mailboxes according to what is located on the remote server.
|
|
|
|
* <em>Note: This does not perform folder synchronization and it will not remove mailboxes
|
|
|
|
* that are stored locally but not remotely.</em>
|
|
|
|
* @return The set of remote folders
|
|
|
|
* @throws MessagingException If there was a problem connecting to the remote server
|
|
|
|
*/
|
2011-06-27 19:12:41 +00:00
|
|
|
public Folder[] updateFolders() throws MessagingException {
|
|
|
|
return null;
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2010-06-02 20:25:03 +00:00
|
|
|
public abstract Bundle checkSettings() throws MessagingException;
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-12-18 17:18:55 +00:00
|
|
|
/**
|
|
|
|
* Handle discovery of account settings using only the user's email address and password
|
|
|
|
* @param context the context of the caller
|
|
|
|
* @param emailAddress the email address of the exchange user
|
|
|
|
* @param password the password of the exchange user
|
|
|
|
* @return a Bundle containing an error code and a HostAuth (if successful)
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
|
|
|
public Bundle autoDiscover(Context context, String emailAddress, String password)
|
|
|
|
throws MessagingException {
|
|
|
|
return null;
|
|
|
|
}
|
2011-04-20 23:22:41 +00:00
|
|
|
|
|
|
|
/**
|
2011-04-22 22:45:11 +00:00
|
|
|
* Updates the fields within the given mailbox. Only the fields that are important to
|
|
|
|
* non-EAS accounts are modified.
|
2011-04-20 23:22:41 +00:00
|
|
|
*/
|
2011-04-22 22:45:11 +00:00
|
|
|
protected static void updateMailbox(Mailbox mailbox, long accountId, String mailboxPath,
|
2011-04-26 21:51:29 +00:00
|
|
|
char delimiter, boolean selectable, int type) {
|
2011-04-20 23:22:41 +00:00
|
|
|
mailbox.mAccountKey = accountId;
|
2011-04-22 22:45:11 +00:00
|
|
|
mailbox.mDelimiter = delimiter;
|
|
|
|
String displayPath = mailboxPath;
|
|
|
|
int pathIndex = mailboxPath.lastIndexOf(delimiter);
|
|
|
|
if (pathIndex > 0) {
|
|
|
|
displayPath = mailboxPath.substring(pathIndex + 1);
|
|
|
|
}
|
|
|
|
mailbox.mDisplayName = displayPath;
|
2011-04-26 21:51:29 +00:00
|
|
|
if (selectable) {
|
|
|
|
mailbox.mFlags = Mailbox.FLAG_HOLDS_MAIL | Mailbox.FLAG_ACCEPTS_MOVED_MAIL;
|
|
|
|
}
|
2011-04-20 23:22:41 +00:00
|
|
|
mailbox.mFlagVisible = true;
|
|
|
|
//mailbox.mParentKey;
|
|
|
|
//mailbox.mParentServerId;
|
|
|
|
mailbox.mServerId = mailboxPath;
|
|
|
|
//mailbox.mServerId;
|
|
|
|
//mailbox.mSyncFrequency;
|
|
|
|
//mailbox.mSyncKey;
|
|
|
|
//mailbox.mSyncLookback;
|
|
|
|
//mailbox.mSyncTime;
|
2011-04-22 22:45:11 +00:00
|
|
|
mailbox.mType = type;
|
2011-04-20 23:22:41 +00:00
|
|
|
//box.mUnreadCount;
|
|
|
|
}
|
2014-01-03 21:45:08 +00:00
|
|
|
|
|
|
|
public void closeConnections() {
|
|
|
|
// Base implementation does nothing.
|
|
|
|
}
|
2013-12-11 19:58:58 +00:00
|
|
|
|
|
|
|
public Account getAccount() {
|
|
|
|
return mAccount;
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|