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 com.android.email.Email;
     20 import com.android.email.R;
     21 
     22 import org.xmlpull.v1.XmlPullParserException;
     23 
     24 import android.content.Context;
     25 import android.content.res.XmlResourceParser;
     26 import android.util.Log;
     27 
     28 import java.io.IOException;
     29 import java.util.HashMap;
     30 
     31 public abstract class Sender {
     32     protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
     33 
     34     private static final HashMap<String, Sender> sSenders = new HashMap<String, Sender>();
     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, String uri)
     41             throws MessagingException {
     42         throw new MessagingException("Sender.newInstance: Unknown scheme in " + uri);
     43     }
     44 
     45     private static Sender instantiateSender(Context context, String className, String uri)
     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", Context.class, String.class);
     53             o = m.invoke(null, context, uri);
     54         } catch (Exception e) {
     55             Log.d(Email.LOG_TAG, String.format(
     56                     "exception %s invoking %s.newInstance.(Context, String) method for %s",
     57                     e.toString(), className, uri));
     58             throw new MessagingException("can not instantiate Sender object for " + uri);
     59         }
     60         if (!(o instanceof Sender)) {
     61             throw new MessagingException(
     62                     uri + ": " + 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, String uri)
     71             throws MessagingException {
     72         Sender sender = null;
     73         try {
     74             XmlResourceParser xml = context.getResources().getXml(resourceId);
     75             int xmlEventType;
     76             // walk through senders.xml file.
     77             while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
     78                 if (xmlEventType == XmlResourceParser.START_TAG &&
     79                     "sender".equals(xml.getName())) {
     80                     String scheme = xml.getAttributeValue(null, "scheme");
     81                     if (uri.startsWith(scheme)) {
     82                         // found sender entry whose scheme is matched with uri.
     83                         // then load sender class.
     84                         String className = xml.getAttributeValue(null, "class");
     85                         sender = instantiateSender(context, className, uri);
     86                     }
     87                 }
     88             }
     89         } catch (XmlPullParserException e) {
     90             // ignore
     91         } catch (IOException e) {
     92             // ignore
     93         }
     94         return sender;
     95     }
     96 
     97     public synchronized static Sender getInstance(Context context, String uri)
     98             throws MessagingException {
     99        Sender sender = sSenders.get(uri);
    100        if (sender == null) {
    101            sender = findSender(context, R.xml.senders_product, uri);
    102            if (sender == null) {
    103                sender = findSender(context, R.xml.senders, uri);
    104            }
    105 
    106            if (sender != null) {
    107                sSenders.put(uri, sender);
    108            }
    109        }
    110 
    111        if (sender == null) {
    112             throw new MessagingException("Unable to locate an applicable Transport for " + uri);
    113        }
    114 
    115        return sender;
    116     }
    117 
    118     /**
    119      * Get class of SettingActivity for this Sender class.
    120      * @return Activity class that has class method actionEditOutgoingSettings().
    121      */
    122     public Class<? extends android.app.Activity> getSettingActivityClass() {
    123         // default SettingActivity class
    124         return com.android.email.activity.setup.AccountSetupOutgoing.class;
    125     }
    126 
    127     public abstract void open() throws MessagingException;
    128 
    129     public String validateSenderLimit(long messageId) {
    130         return null;
    131     }
    132 
    133     /**
    134      * Check message has any limitation of Sender or not.
    135      *
    136      * @param messageId the message that will be checked.
    137      * @throws LimitViolationException
    138      */
    139     public void checkSenderLimitation(long messageId) throws LimitViolationException {
    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(long messageId) throws MessagingException;
    163 
    164     public abstract void close() throws MessagingException;
    165 }
    166