Sender.java revision 1b8e0fa23f6e9957f0b8753dd3f5b95d3f5d98ea
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 android.content.Context;
20import android.content.res.XmlResourceParser;
21
22import com.android.email.R;
23import com.android.emailcommon.Logging;
24import com.android.emailcommon.mail.MessagingException;
25import com.android.emailcommon.provider.Account;
26import com.android.emailcommon.provider.HostAuth;
27import com.android.mail.utils.LogUtils;
28
29import org.xmlpull.v1.XmlPullParserException;
30
31import java.io.IOException;
32
33public abstract class Sender {
34    protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
35
36    /**
37     * Static named constructor.  It should be overrode by extending class.
38     * Because this method will be called through reflection, it can not be protected.
39     */
40    public static Sender newInstance(Account account) throws MessagingException {
41        throw new MessagingException("Sender.newInstance: Unknown scheme in "
42                + account.mDisplayName);
43    }
44
45    private static Sender instantiateSender(Context context, String className, Account account)
46        throws MessagingException {
47        Object o = null;
48        try {
49            Class<?> c = Class.forName(className);
50            // and invoke "newInstance" class method and instantiate sender object.
51            java.lang.reflect.Method m =
52                c.getMethod("newInstance", Account.class, Context.class);
53            o = m.invoke(null, account, context);
54        } catch (Exception e) {
55            LogUtils.d(Logging.LOG_TAG, String.format(
56                    "exception %s invoking method %s#newInstance(Account, Context) for %s",
57                    e.toString(), className, account.mDisplayName));
58            throw new MessagingException("can not instantiate Sender for " + account.mDisplayName);
59        }
60        if (!(o instanceof Sender)) {
61            throw new MessagingException(
62                    account.mDisplayName + ": " + className + " create incompatible object");
63        }
64        return (Sender) o;
65    }
66
67    /**
68     * Find Sender implementation consulting with sender.xml file.
69     */
70    private static Sender findSender(Context context, int resourceId, Account account)
71            throws MessagingException {
72        Sender sender = null;
73        try {
74            XmlResourceParser xml = context.getResources().getXml(resourceId);
75            int xmlEventType;
76            HostAuth sendAuth = account.getOrCreateHostAuthSend(context);
77            // walk through senders.xml file.
78            while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
79                if (xmlEventType == XmlResourceParser.START_TAG &&
80                    "sender".equals(xml.getName())) {
81                    String xmlScheme = xml.getAttributeValue(null, "scheme");
82                    if (sendAuth.mProtocol != null && sendAuth.mProtocol.startsWith(xmlScheme)) {
83                        // found sender entry whose scheme is matched with uri.
84                        // then load sender class.
85                        String className = xml.getAttributeValue(null, "class");
86                        sender = instantiateSender(context, className, account);
87                    }
88                }
89            }
90        } catch (XmlPullParserException e) {
91            // ignore
92        } catch (IOException e) {
93            // ignore
94        }
95        return sender;
96    }
97
98    /**
99     * Get an instance of a mail sender for the given account. The account must be valid (i.e. has
100     * at least an outgoing server name).
101     *
102     * @param context the caller's context
103     * @param account the account of the sender.
104     * @return an initialized sender of the appropriate class
105     * @throws MessagingException If the sender cannot be obtained or if the account is invalid.
106     */
107    public synchronized static Sender getInstance(Context context, Account account)
108            throws MessagingException {
109        Context appContext = context.getApplicationContext();
110        Sender sender = findSender(appContext, R.xml.senders_product, account);
111        if (sender == null) {
112            sender = findSender(appContext, R.xml.senders, account);
113        }
114        if (sender == null) {
115            throw new MessagingException("Cannot find sender for account " + account.mDisplayName);
116        }
117        return sender;
118    }
119
120    /**
121     * Get class of SettingActivity for this Sender class.
122     * @return Activity class that has class method actionEditOutgoingSettings().
123     */
124    public Class<? extends android.app.Activity> getSettingActivityClass() {
125        // default SettingActivity class
126        return com.android.email.activity.setup.AccountSetupOutgoing.class;
127    }
128
129    public abstract void open() throws MessagingException;
130
131    public abstract void sendMessage(long messageId) throws MessagingException;
132
133    public abstract void close() throws MessagingException;
134}
135