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