Sender.java revision 2b0c619f1edd9fd89dc06bf35d99ece91f415f1e
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; 26import android.util.Log; 27 28import java.io.IOException; 29 30public abstract class Sender { 31 protected static final int SOCKET_CONNECT_TIMEOUT = 10000; 32 33 private static java.util.HashMap<String, Sender> mSenders = 34 new java.util.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(String uri, Context context) 41 throws MessagingException { 42 throw new MessagingException("Sender.newInstance: Unknown scheme in " + uri); 43 } 44 45 private static Sender instantiateSender(String className, String uri, Context context) 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", String.class, Context.class); 53 o = m.invoke(null, uri, context); 54 } catch (Exception e) { 55 Log.d(Email.LOG_TAG, String.format( 56 "exception %s invoking %s.newInstance.(String, Context) 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(int resourceId, String uri, Context context) 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(className, uri, context); 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(String uri, Context context) 98 throws MessagingException { 99 Sender sender = mSenders.get(uri); 100 if (sender == null) { 101 sender = findSender(R.xml.senders_product, uri, context); 102 if (sender == null) { 103 sender = findSender(R.xml.senders, uri, context); 104 } 105 106 if (sender != null) { 107 mSenders.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(Message message) { 130 return null; 131 } 132 133 /** 134 * Check message has any limitation of Sender or not. 135 * 136 * @param message the message that will be checked. 137 * @throws LimitViolationException 138 */ 139 public void checkSenderLimitation(Message message) 140 throws LimitViolationException, MessagingException { 141 } 142 143 public static class LimitViolationException extends MessagingException { 144 public final int mMsgResourceId; 145 public final long mActual; 146 public final long mLimit; 147 148 private LimitViolationException(int msgResourceId, long actual, long limit) { 149 super(UNSPECIFIED_EXCEPTION); 150 mMsgResourceId = msgResourceId; 151 mActual = actual; 152 mLimit = limit; 153 } 154 155 public static void check(int msgResourceId, long actual, long limit) 156 throws LimitViolationException { 157 if (actual > limit) { 158 throw new LimitViolationException(msgResourceId, actual, limit); 159 } 160 } 161 } 162 163 public abstract void sendMessage(Message message) throws MessagingException; 164 165 public abstract void close() throws MessagingException; 166} 167