Sender.java revision f5418f1f93b02e7fab9f15eb201800b65510998e
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.R; 20import com.android.emailcommon.Logging; 21import com.android.emailcommon.mail.MessagingException; 22import com.android.emailcommon.provider.Account; 23import com.android.emailcommon.provider.HostAuth; 24 25import org.xmlpull.v1.XmlPullParserException; 26 27import android.content.Context; 28import android.content.res.XmlResourceParser; 29import android.text.TextUtils; 30import android.util.Log; 31 32import java.io.IOException; 33import java.util.HashMap; 34 35public abstract class Sender { 36 protected static final int SOCKET_CONNECT_TIMEOUT = 10000; 37 38 private static final HashMap<String, Sender> sSenders = new HashMap<String, Sender>(); 39 40 /** 41 * Static named constructor. It should be overrode by extending class. 42 * Because this method will be called through reflection, it can not be protected. 43 */ 44 public static Sender newInstance(Context context, Account account) 45 throws MessagingException { 46 throw new MessagingException("Sender.newInstance: Unknown scheme in " 47 + account.mDisplayName); 48 } 49 50 private static Sender instantiateSender(Context context, String className, Account account) 51 throws MessagingException { 52 Object o = null; 53 try { 54 Class<?> c = Class.forName(className); 55 // and invoke "newInstance" class method and instantiate sender object. 56 java.lang.reflect.Method m = 57 c.getMethod("newInstance", Account.class, Context.class); 58 o = m.invoke(null, account, context); 59 } catch (Exception e) { 60 Log.d(Logging.LOG_TAG, String.format( 61 "exception %s invoking method %s#newInstance(Account, Context) for %s", 62 e.toString(), className, account.mDisplayName)); 63 throw new MessagingException("can not instantiate Sender for " + account.mDisplayName); 64 } 65 if (!(o instanceof Sender)) { 66 throw new MessagingException( 67 account.mDisplayName + ": " + className + " create incompatible object"); 68 } 69 return (Sender) o; 70 } 71 72 /** 73 * Find Sender implementation consulting with sender.xml file. 74 */ 75 private static Sender findSender(Context context, int resourceId, Account account) 76 throws MessagingException { 77 Sender sender = null; 78 try { 79 XmlResourceParser xml = context.getResources().getXml(resourceId); 80 int xmlEventType; 81 HostAuth sendAuth = account.getOrCreateHostAuthSend(context); 82 // walk through senders.xml file. 83 while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) { 84 if (xmlEventType == XmlResourceParser.START_TAG && 85 "sender".equals(xml.getName())) { 86 String xmlScheme = xml.getAttributeValue(null, "scheme"); 87 if (sendAuth.mProtocol != null && sendAuth.mProtocol.startsWith(xmlScheme)) { 88 // found sender entry whose scheme is matched with uri. 89 // then load sender class. 90 String className = xml.getAttributeValue(null, "class"); 91 sender = instantiateSender(context, className, account); 92 } 93 } 94 } 95 } catch (XmlPullParserException e) { 96 // ignore 97 } catch (IOException e) { 98 // ignore 99 } 100 return sender; 101 } 102 103 /** 104 * Gets a unique key for the given account. 105 * @throws MessagingException If the account is not setup properly (i.e. there is no address 106 * or login) 107 */ 108 private static String getSenderKey(Context context, Account account) throws MessagingException { 109 final StringBuffer key = new StringBuffer(); 110 final HostAuth sendAuth = account.getOrCreateHostAuthSend(context); 111 if (sendAuth.mAddress == null) { 112 throw new MessagingException("Cannot find sender for account " + account.mDisplayName); 113 } 114 final String address = sendAuth.mAddress.trim(); 115 if (TextUtils.isEmpty(address)) { 116 throw new MessagingException("Cannot find sender for account " + account.mDisplayName); 117 } 118 key.append(address); 119 if (sendAuth.mLogin != null) { 120 key.append(sendAuth.mLogin.trim()); 121 } 122 return key.toString(); 123 } 124 125 /** 126 * Get an instance of a mail sender for the given account. The account must be valid (i.e. has 127 * at least an outgoing server name). 128 * 129 * @param account The account of the sender. 130 * @return an initialized sender of the appropriate class 131 * @throws MessagingException If the sender cannot be obtained or if the account is invalid. 132 */ 133 public synchronized static Sender getInstance(Context context, Account account) 134 throws MessagingException { 135 String senderKey = getSenderKey(context, account); 136 Sender sender = sSenders.get(senderKey); 137 if (sender == null) { 138 Context appContext = context.getApplicationContext(); 139 sender = findSender(appContext, R.xml.senders_product, account); 140 if (sender == null) { 141 sender = findSender(appContext, R.xml.senders, account); 142 } 143 144 if (sender != null) { 145 sSenders.put(senderKey, sender); 146 } 147 } 148 149 if (sender == null) { 150 throw new MessagingException("Cannot find sender for account " + account.mDisplayName); 151 } 152 153 return sender; 154 } 155 156 /** 157 * Get class of SettingActivity for this Sender class. 158 * @return Activity class that has class method actionEditOutgoingSettings(). 159 */ 160 public Class<? extends android.app.Activity> getSettingActivityClass() { 161 // default SettingActivity class 162 return com.android.email.activity.setup.AccountSetupOutgoing.class; 163 } 164 165 public abstract void open() throws MessagingException; 166 167 public String validateSenderLimit(long messageId) { 168 return null; 169 } 170 171 /** 172 * Check message has any limitation of Sender or not. 173 * 174 * @param messageId the message that will be checked. 175 * @throws LimitViolationException 176 */ 177 public void checkSenderLimitation(long messageId) throws LimitViolationException { 178 } 179 180 public static class LimitViolationException extends MessagingException { 181 public final int mMsgResourceId; 182 public final long mActual; 183 public final long mLimit; 184 185 private LimitViolationException(int msgResourceId, long actual, long limit) { 186 super(UNSPECIFIED_EXCEPTION); 187 mMsgResourceId = msgResourceId; 188 mActual = actual; 189 mLimit = limit; 190 } 191 192 public static void check(int msgResourceId, long actual, long limit) 193 throws LimitViolationException { 194 if (actual > limit) { 195 throw new LimitViolationException(msgResourceId, actual, limit); 196 } 197 } 198 } 199 200 public abstract void sendMessage(long messageId) throws MessagingException; 201 202 public abstract void close() throws MessagingException; 203} 204