• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.email.mail;
18 
19 import android.content.Context;
20 import android.content.res.XmlResourceParser;
21 
22 import com.android.email.R;
23 import com.android.emailcommon.Logging;
24 import com.android.emailcommon.mail.MessagingException;
25 import com.android.emailcommon.provider.Account;
26 import com.android.emailcommon.provider.HostAuth;
27 import com.android.mail.utils.LogUtils;
28 
29 import org.xmlpull.v1.XmlPullParserException;
30 
31 import java.io.IOException;
32 
33 public 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      */
newInstance(Account account)40     public static Sender newInstance(Account account) throws MessagingException {
41         throw new MessagingException("Sender.newInstance: Unknown scheme in "
42                 + account.mDisplayName);
43     }
44 
instantiateSender(Context context, String className, Account account)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      */
findSender(Context context, int resourceId, Account account)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      */
getInstance(Context context, Account account)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 
open()120     public abstract void open() throws MessagingException;
121 
sendMessage(long messageId)122     public abstract void sendMessage(long messageId) throws MessagingException;
123 
close()124     public abstract void close() throws MessagingException;
125 }
126