Store.java revision 8664ecf1817e9965f9910dd12f6115ef4aaa8f2a
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.mail;
18
19import com.android.email.Email;
20import com.android.email.R;
21
22import org.xmlpull.v1.XmlPullParserException;
23
24import android.content.Context;
25import android.content.res.XmlResourceParser;
26import android.util.Log;
27
28import java.io.IOException;
29
30/**
31 * Store is the access point for an email message store. It's location can be
32 * local or remote and no specific protocol is defined. Store is intended to
33 * loosely model in combination the JavaMail classes javax.mail.Store and
34 * javax.mail.Folder along with some additional functionality to improve
35 * performance on mobile devices. Implementations of this class should focus on
36 * making as few network connections as possible.
37 */
38public abstract class Store {
39
40    /**
41     * String constants for known store schemes.
42     */
43    public static final String STORE_SCHEME_IMAP = "imap";
44    public static final String STORE_SCHEME_POP3 = "pop3";
45    public static final String STORE_SCHEME_LOCAL = "local";
46
47    /**
48     * A global suggestion to Store implementors on how much of the body
49     * should be returned on FetchProfile.Item.BODY_SANE requests.
50     */
51    public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024);
52
53    private static java.util.HashMap<String, Store> mStores =
54        new java.util.HashMap<String, Store>();
55
56    /**
57     * Static named constructor.  It should be overrode by extending class.
58     * Because this method will be called through reflection, it can not be protected.
59     */
60    public static Store newInstance(String uri, Context context)
61            throws MessagingException {
62        throw new MessagingException("Store.newInstance: Unknown scheme in " + uri);
63    }
64
65    private static Store instantiateStore(String className, String uri, Context context)
66        throws MessagingException {
67        Object o = null;
68        try {
69            Class<?> c = Class.forName(className);
70            // and invoke "newInstance" class method and instantiate store object.
71            java.lang.reflect.Method m =
72                c.getMethod("newInstance", String.class, Context.class);
73            o = m.invoke(null, uri, context);
74        } catch (Exception e) {
75            Log.d(Email.LOG_TAG, String.format(
76                    "exception %s invoking %s.newInstance.(String, Context) method for %s",
77                    e.toString(), className, uri));
78            throw new MessagingException("can not instantiate Store object for " + uri);
79        }
80        if (!(o instanceof Store)) {
81            throw new MessagingException(
82                    uri + ": " + className + " create incompatible object");
83        }
84        return (Store) o;
85    }
86
87    /**
88     * Look up descriptive information about a particular type of store.
89     */
90    public static class StoreInfo {
91        public String mScheme;
92        public String mClassName;
93        public boolean mPushSupported = false;
94
95        public static StoreInfo getStoreInfo(String scheme, Context context) {
96            StoreInfo result = getStoreInfo(R.xml.stores_product, scheme, context);
97            if (result == null) {
98                result = getStoreInfo(R.xml.stores, scheme, context);
99            }
100            return result;
101        }
102
103        public static StoreInfo getStoreInfo(int resourceId, String scheme, Context context) {
104            try {
105                XmlResourceParser xml = context.getResources().getXml(resourceId);
106                int xmlEventType;
107                // walk through stores.xml file.
108                while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
109                    if (xmlEventType == XmlResourceParser.START_TAG &&
110                            "store".equals(xml.getName())) {
111                        String xmlScheme = xml.getAttributeValue(null, "scheme");
112                        if (scheme.startsWith(xmlScheme)) {
113                            StoreInfo result = new StoreInfo();
114                            result.mScheme = scheme;
115                            result.mClassName = xml.getAttributeValue(null, "class");
116                            result.mPushSupported = xml.getAttributeBooleanValue(
117                                    null, "push", false);
118                            return result;
119                        }
120                    }
121                }
122            } catch (XmlPullParserException e) {
123                // ignore
124            } catch (IOException e) {
125                // ignore
126            }
127            return null;
128        }
129    }
130
131    /**
132     * Get an instance of a mail store. The URI is parsed as a standard URI and
133     * the scheme is used to determine which protocol will be used.
134     *
135     * Although the URI format is somewhat protocol-specific, we use the following
136     * guidelines wherever possible:
137     *
138     * scheme [+ security [+]] :// username : password @ host [ / resource ]
139     *
140     * Typical schemes include imap, pop3, local, eas.
141     * Typical security models include SSL or TLS.
142     * A + after the security identifier indicates "required".
143     *
144     * Username, password, and host are as expected.
145     * Resource is protocol specific.  For example, IMAP uses it as the path prefix.  EAS uses it
146     * as the domain.
147     *
148     * @param uri The URI of the store.
149     * @return an initialized store of the appropriate class
150     * @throws MessagingException
151     */
152    public synchronized static Store getInstance(String uri, Context context)
153        throws MessagingException {
154        Store store = mStores.get(uri);
155        if (store == null) {
156            StoreInfo info = StoreInfo.getStoreInfo(uri, context);
157            if (info != null) {
158                store = instantiateStore(info.mClassName, uri, context);
159            }
160
161            if (store != null) {
162                mStores.put(uri, store);
163            }
164        }
165
166        if (store == null) {
167            throw new MessagingException("Unable to locate an applicable Store for " + uri);
168        }
169
170        return store;
171    }
172
173    /**
174     * Get class of SettingActivity for this Store class.
175     * @return Activity class that has class method actionEditIncomingSettings().
176     */
177    public Class<? extends android.app.Activity> getSettingActivityClass() {
178        // default SettingActivity class
179        return com.android.email.activity.setup.AccountSetupIncoming.class;
180    }
181
182    public abstract Folder getFolder(String name) throws MessagingException;
183
184    public abstract Folder[] getPersonalNamespaces() throws MessagingException;
185
186    public abstract void checkSettings() throws MessagingException;
187
188    /**
189     * Enable or disable push mode delivery for a given Store.
190     *
191     * <p>For protocols that do not support push mode, be sure that push="true" is not
192     * set by the stores.xml definition file(s).  This function need not be implemented.
193     *
194     * <p>For protocols that do support push mode, this will be called at startup (boot) time
195     * so that the Store can launch its own underlying connection service.  It will also be called
196     * any time the user changes the settings for the account (because the user may switch back
197     * to polling mode (or disable checking completely).
198     *
199     * <p>This API will be called repeatedly, even after push mode has already been started or
200     * stopped.  Stores that support push mode should return quickly if the configuration has not
201     * changed.
202     *
203     * @param enablePushMode start or stop push mode delivery
204     */
205    public void enablePushModeDelivery(boolean enablePushMode) {
206        // does nothing for non-push protocols
207    }
208
209    /**
210     * Delete Store and its corresponding resources.
211     * @throws MessagingException
212     */
213    public void delete() throws MessagingException {
214    }
215}
216