Sender.java revision bd681454608d518e938e2e7b3ce801f560b14131
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;
26
27import java.io.IOException;
28
29public abstract class Sender {
30    protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
31
32    private static java.util.HashMap<String, Sender> mSenders =
33        new java.util.HashMap<String, Sender>();
34
35    /**
36     * Static named constructor.  It should be overrode by extending class.
37     * Because this method will be called through reflection, it can not be protected.
38     */
39    public static Sender newInstance(String uri, Context context)
40            throws MessagingException {
41        throw new MessagingException("Sender.newInscance: Unknown scheme in " + uri);
42    }
43
44    private static Sender instanciateSender(String className, String uri, Context context)
45        throws MessagingException {
46        Object o = null;
47        try {
48            Class<?> c = Class.forName(className);
49            // and invoke "newInstance" class method and instantiate sender object.
50            java.lang.reflect.Method m =
51                c.getMethod("newInstance", String.class, Context.class);
52            o = m.invoke(null, uri, context);
53        } catch (Exception e) {
54            android.util.Log.e(Email.LOG_TAG,
55                    String.format("can not invoke %s.newInstance.(String, Context) method for %s",
56                            className, uri));
57            throw new MessagingException("can not instanciate Sender object for " + uri);
58        }
59        if (!(o instanceof Sender)) {
60            throw new MessagingException(
61                    uri + ": " + className + " create incompatible object");
62        }
63        return (Sender) o;
64    }
65
66    /**
67     * Find Sender implementation consulting with sender.xml file.
68     */
69    private static Sender findSender(int resourceId, String uri, Context context)
70            throws MessagingException {
71        Sender sender = null;
72        try {
73            XmlResourceParser xml = context.getResources().getXml(resourceId);
74            int xmlEventType;
75            // walk through senders.xml file.
76            while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
77                if (xmlEventType == XmlResourceParser.START_TAG &&
78                    "sender".equals(xml.getName())) {
79                    String scheme = xml.getAttributeValue(null, "scheme");
80                    if (uri.startsWith(scheme)) {
81                        // found sender entry whose scheme is matched with uri.
82                        // then load sender class.
83                        String className = xml.getAttributeValue(null, "class");
84                        sender = instanciateSender(className, uri, context);
85                    }
86                }
87            }
88        } catch (XmlPullParserException e) {
89            // ignore
90        } catch (IOException e) {
91            // ignore
92        }
93        return sender;
94    }
95
96    public synchronized static Sender getInstance(String uri, Context context)
97            throws MessagingException {
98       Sender sender = mSenders.get(uri);
99       if (sender == null) {
100           sender = findSender(R.xml.senders_product, uri, context);
101           if (sender == null) {
102               sender = findSender(R.xml.senders, uri, context);
103           }
104
105           if (sender != null) {
106               mSenders.put(uri, sender);
107           }
108       }
109
110       if (sender == null) {
111            throw new MessagingException("Unable to locate an applicable Transport for " + uri);
112       }
113
114       return sender;
115    }
116
117    /**
118     * Get class of SettingActivity for this Sender class.
119     * @return Activity class that has class method actionEditOutgoingSettings().
120     */
121    public Class<? extends android.app.Activity> getSettingActivityClass() {
122        // default SettingActivity class
123        return com.android.email.activity.setup.AccountSetupOutgoing.class;
124    }
125
126    public abstract void open() throws MessagingException;
127
128    public String validateSenderLimit(Message message) {
129        return null;
130    }
131
132    /**
133     * Check message has any limitation of Sender or not.
134     *
135     * @param message the message that will be checked.
136     * @throws LimitViolationException
137     */
138    public void checkSenderLimitation(Message message)
139        throws LimitViolationException, MessagingException {
140    }
141
142    public static class LimitViolationException extends MessagingException {
143        public final int mMsgResourceId;
144        public final long mActual;
145        public final long mLimit;
146
147        private LimitViolationException(int msgResourceId, long actual, long limit) {
148            super(UNSPECIFIED_EXCEPTION);
149            mMsgResourceId = msgResourceId;
150            mActual = actual;
151            mLimit = limit;
152        }
153
154        public static void check(int msgResourceId, long actual, long limit)
155            throws LimitViolationException {
156            if (actual > limit) {
157                throw new LimitViolationException(msgResourceId, actual, limit);
158            }
159        }
160    }
161
162    public abstract void sendMessage(Message message) throws MessagingException;
163
164    public abstract void close() throws MessagingException;
165}
166