2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
import com.android.email.Email;
|
|
|
|
import com.android.email.R;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.XmlResourceParser;
|
2009-04-03 22:40:49 +00:00
|
|
|
import android.util.Log;
|
2009-03-27 00:05:25 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store is the access point for an email message store. It's location can be
|
|
|
|
* local or remote and no specific protocol is defined. Store is intended to
|
|
|
|
* loosely model in combination the JavaMail classes javax.mail.Store and
|
|
|
|
* javax.mail.Folder along with some additional functionality to improve
|
|
|
|
* performance on mobile devices. Implementations of this class should focus on
|
|
|
|
* making as few network connections as possible.
|
|
|
|
*/
|
|
|
|
public abstract class Store {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String constants for known store schemes.
|
|
|
|
*/
|
|
|
|
public static final String STORE_SCHEME_IMAP = "imap";
|
|
|
|
public static final String STORE_SCHEME_POP3 = "pop3";
|
|
|
|
public static final String STORE_SCHEME_LOCAL = "local";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A global suggestion to Store implementors on how much of the body
|
|
|
|
* should be returned on FetchProfile.Item.BODY_SANE requests.
|
|
|
|
*/
|
|
|
|
public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024);
|
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
private static java.util.HashMap<String, Store> mStores =
|
|
|
|
new java.util.HashMap<String, Store>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static named constructor. It should be overrode by extending class.
|
|
|
|
* Because this method will be called through reflection, it can not be protected.
|
|
|
|
*/
|
2009-04-14 03:07:56 +00:00
|
|
|
public static Store newInstance(String uri, Context context, PersistentDataCallbacks callbacks)
|
2009-03-27 00:05:25 +00:00
|
|
|
throws MessagingException {
|
|
|
|
throw new MessagingException("Store.newInstance: Unknown scheme in " + uri);
|
|
|
|
}
|
|
|
|
|
2009-04-14 03:07:56 +00:00
|
|
|
private static Store instantiateStore(String className, String uri, Context context,
|
|
|
|
PersistentDataCallbacks callbacks)
|
2009-03-27 00:05:25 +00:00
|
|
|
throws MessagingException {
|
|
|
|
Object o = null;
|
|
|
|
try {
|
|
|
|
Class<?> c = Class.forName(className);
|
|
|
|
// and invoke "newInstance" class method and instantiate store object.
|
|
|
|
java.lang.reflect.Method m =
|
2009-04-14 03:07:56 +00:00
|
|
|
c.getMethod("newInstance", String.class, Context.class,
|
|
|
|
PersistentDataCallbacks.class);
|
|
|
|
o = m.invoke(null, uri, context, callbacks);
|
2009-03-27 00:05:25 +00:00
|
|
|
} catch (Exception e) {
|
2009-04-03 22:40:49 +00:00
|
|
|
Log.d(Email.LOG_TAG, String.format(
|
|
|
|
"exception %s invoking %s.newInstance.(String, Context) method for %s",
|
|
|
|
e.toString(), className, uri));
|
|
|
|
throw new MessagingException("can not instantiate Store object for " + uri);
|
2009-03-27 00:05:25 +00:00
|
|
|
}
|
|
|
|
if (!(o instanceof Store)) {
|
|
|
|
throw new MessagingException(
|
|
|
|
uri + ": " + className + " create incompatible object");
|
|
|
|
}
|
|
|
|
return (Store) o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-08 00:35:13 +00:00
|
|
|
* Look up descriptive information about a particular type of store.
|
2009-03-27 00:05:25 +00:00
|
|
|
*/
|
2009-04-08 00:35:13 +00:00
|
|
|
public static class StoreInfo {
|
|
|
|
public String mScheme;
|
|
|
|
public String mClassName;
|
|
|
|
public boolean mPushSupported = false;
|
2009-04-15 17:58:59 +00:00
|
|
|
public int mVisibleLimitDefault;
|
|
|
|
public int mVisibleLimitIncrement;
|
2009-04-08 00:35:13 +00:00
|
|
|
|
2009-04-15 17:58:59 +00:00
|
|
|
// TODO cache result for performance - silly to keep reading the XML
|
2009-04-08 00:35:13 +00:00
|
|
|
public static StoreInfo getStoreInfo(String scheme, Context context) {
|
|
|
|
StoreInfo result = getStoreInfo(R.xml.stores_product, scheme, context);
|
|
|
|
if (result == null) {
|
|
|
|
result = getStoreInfo(R.xml.stores, scheme, context);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static StoreInfo getStoreInfo(int resourceId, String scheme, Context context) {
|
|
|
|
try {
|
|
|
|
XmlResourceParser xml = context.getResources().getXml(resourceId);
|
|
|
|
int xmlEventType;
|
|
|
|
// walk through stores.xml file.
|
|
|
|
while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
|
|
|
|
if (xmlEventType == XmlResourceParser.START_TAG &&
|
|
|
|
"store".equals(xml.getName())) {
|
|
|
|
String xmlScheme = xml.getAttributeValue(null, "scheme");
|
|
|
|
if (scheme.startsWith(xmlScheme)) {
|
|
|
|
StoreInfo result = new StoreInfo();
|
|
|
|
result.mScheme = scheme;
|
|
|
|
result.mClassName = xml.getAttributeValue(null, "class");
|
|
|
|
result.mPushSupported = xml.getAttributeBooleanValue(
|
|
|
|
null, "push", false);
|
2009-04-15 17:58:59 +00:00
|
|
|
result.mVisibleLimitDefault = xml.getAttributeIntValue(
|
|
|
|
null, "visibleLimitDefault", Email.VISIBLE_LIMIT_DEFAULT);
|
|
|
|
result.mVisibleLimitIncrement = xml.getAttributeIntValue(
|
|
|
|
null, "visibleLimitIncrement", Email.VISIBLE_LIMIT_INCREMENT);
|
2009-04-08 00:35:13 +00:00
|
|
|
return result;
|
|
|
|
}
|
2009-03-27 00:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-08 00:35:13 +00:00
|
|
|
} catch (XmlPullParserException e) {
|
|
|
|
// ignore
|
|
|
|
} catch (IOException e) {
|
|
|
|
// ignore
|
2009-03-27 00:05:25 +00:00
|
|
|
}
|
2009-04-08 00:35:13 +00:00
|
|
|
return null;
|
2009-03-27 00:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of a mail store. The URI is parsed as a standard URI and
|
2009-04-03 22:40:49 +00:00
|
|
|
* the scheme is used to determine which protocol will be used.
|
|
|
|
*
|
|
|
|
* Although the URI format is somewhat protocol-specific, we use the following
|
|
|
|
* guidelines wherever possible:
|
|
|
|
*
|
|
|
|
* scheme [+ security [+]] :// username : password @ host [ / resource ]
|
|
|
|
*
|
|
|
|
* Typical schemes include imap, pop3, local, eas.
|
|
|
|
* Typical security models include SSL or TLS.
|
|
|
|
* A + after the security identifier indicates "required".
|
|
|
|
*
|
|
|
|
* Username, password, and host are as expected.
|
|
|
|
* Resource is protocol specific. For example, IMAP uses it as the path prefix. EAS uses it
|
|
|
|
* as the domain.
|
2009-03-04 03:32:22 +00:00
|
|
|
*
|
|
|
|
* @param uri The URI of the store.
|
|
|
|
* @return an initialized store of the appropriate class
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
2009-04-14 03:07:56 +00:00
|
|
|
public synchronized static Store getInstance(String uri, Context context,
|
|
|
|
PersistentDataCallbacks callbacks)
|
2009-03-27 00:05:25 +00:00
|
|
|
throws MessagingException {
|
2009-03-04 03:32:22 +00:00
|
|
|
Store store = mStores.get(uri);
|
|
|
|
if (store == null) {
|
2009-04-08 00:35:13 +00:00
|
|
|
StoreInfo info = StoreInfo.getStoreInfo(uri, context);
|
|
|
|
if (info != null) {
|
2009-04-14 03:07:56 +00:00
|
|
|
store = instantiateStore(info.mClassName, uri, context, callbacks);
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (store != null) {
|
|
|
|
mStores.put(uri, store);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (store == null) {
|
|
|
|
throw new MessagingException("Unable to locate an applicable Store for " + uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
/**
|
|
|
|
* Get class of SettingActivity for this Store class.
|
|
|
|
* @return Activity class that has class method actionEditIncomingSettings().
|
|
|
|
*/
|
|
|
|
public Class<? extends android.app.Activity> getSettingActivityClass() {
|
|
|
|
// default SettingActivity class
|
|
|
|
return com.android.email.activity.setup.AccountSetupIncoming.class;
|
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
public abstract Folder getFolder(String name) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract Folder[] getPersonalNamespaces() throws MessagingException;
|
2009-04-23 01:45:22 +00:00
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
public abstract void checkSettings() throws MessagingException;
|
2009-03-27 00:05:25 +00:00
|
|
|
|
2009-04-09 01:42:42 +00:00
|
|
|
/**
|
|
|
|
* Enable or disable push mode delivery for a given Store.
|
|
|
|
*
|
|
|
|
* <p>For protocols that do not support push mode, be sure that push="true" is not
|
|
|
|
* set by the stores.xml definition file(s). This function need not be implemented.
|
|
|
|
*
|
|
|
|
* <p>For protocols that do support push mode, this will be called at startup (boot) time
|
|
|
|
* so that the Store can launch its own underlying connection service. It will also be called
|
|
|
|
* any time the user changes the settings for the account (because the user may switch back
|
|
|
|
* to polling mode (or disable checking completely).
|
|
|
|
*
|
|
|
|
* <p>This API will be called repeatedly, even after push mode has already been started or
|
|
|
|
* stopped. Stores that support push mode should return quickly if the configuration has not
|
|
|
|
* changed.
|
|
|
|
*
|
|
|
|
* @param enablePushMode start or stop push mode delivery
|
|
|
|
*/
|
|
|
|
public void enablePushModeDelivery(boolean enablePushMode) {
|
|
|
|
// does nothing for non-push protocols
|
|
|
|
}
|
|
|
|
|
2009-03-27 00:05:25 +00:00
|
|
|
/**
|
|
|
|
* Delete Store and its corresponding resources.
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
|
|
|
public void delete() throws MessagingException {
|
|
|
|
}
|
2009-04-14 03:07:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback interface by which a Store can read and write persistent data.
|
|
|
|
* TODO This needs to be made more generic & flexible
|
|
|
|
*/
|
|
|
|
public interface PersistentDataCallbacks {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a small place for Stores to store persistent data.
|
|
|
|
* @param storeData Data to persist. All data must be encoded into a string,
|
|
|
|
* so use base64 or some other encoding if necessary.
|
|
|
|
*/
|
2009-04-23 23:42:23 +00:00
|
|
|
public void setPersistentString(Context context, String storeData);
|
2009-04-14 03:07:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the data saved by the Store, or null if never set.
|
|
|
|
*/
|
2009-04-23 23:42:23 +00:00
|
|
|
public String getPersistentString(Context context);
|
2009-04-14 03:07:56 +00:00
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|