Store.java revision 8978aac1977408b05e386ae846c30920c7faa0a6
1
2package com.android.email.mail;
3
4import java.util.HashMap;
5
6import android.app.Application;
7
8import com.android.email.mail.store.ImapStore;
9import com.android.email.mail.store.LocalStore;
10import com.android.email.mail.store.Pop3Store;
11
12/**
13 * Store is the access point for an email message store. It's location can be
14 * local or remote and no specific protocol is defined. Store is intended to
15 * loosely model in combination the JavaMail classes javax.mail.Store and
16 * javax.mail.Folder along with some additional functionality to improve
17 * performance on mobile devices. Implementations of this class should focus on
18 * making as few network connections as possible.
19 */
20public abstract class Store {
21    /**
22     * A global suggestion to Store implementors on how much of the body
23     * should be returned on FetchProfile.Item.BODY_SANE requests.
24     */
25    public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024);
26
27    protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
28    protected static final int SOCKET_READ_TIMEOUT = 60000;
29
30    private static HashMap<String, Store> mStores = new HashMap<String, Store>();
31
32    /**
33     * Get an instance of a mail store. The URI is parsed as a standard URI and
34     * the scheme is used to determine which protocol will be used. The
35     * following schemes are currently recognized: imap - IMAP with no
36     * connection security. Ex: imap://username:password@host/ imap+tls - IMAP
37     * with TLS connection security, if the server supports it. Ex:
38     * imap+tls://username:password@host imap+tls+ - IMAP with required TLS
39     * connection security. Connection fails if TLS is not available. Ex:
40     * imap+tls+://username:password@host imap+ssl+ - IMAP with required SSL
41     * connection security. Connection fails if SSL is not available. Ex:
42     * imap+ssl+://username:password@host
43     *
44     * @param uri The URI of the store.
45     * @return
46     * @throws MessagingException
47     */
48    public synchronized static Store getInstance(String uri, Application application) throws MessagingException {
49        Store store = mStores.get(uri);
50        if (store == null) {
51            if (uri.startsWith("imap")) {
52                store = new ImapStore(uri);
53            } else if (uri.startsWith("pop3")) {
54                store = new Pop3Store(uri);
55            } else if (uri.startsWith("local")) {
56                store = new LocalStore(uri, application);
57            }
58
59            if (store != null) {
60                mStores.put(uri, store);
61            }
62        }
63
64        if (store == null) {
65            throw new MessagingException("Unable to locate an applicable Store for " + uri);
66        }
67
68        return store;
69    }
70
71    public abstract Folder getFolder(String name) throws MessagingException;
72
73    public abstract Folder[] getPersonalNamespaces() throws MessagingException;
74
75    public abstract void checkSettings() throws MessagingException;
76}
77