Sender.java revision 2193962ca2b3157e79f731736afa2a0c972e778a
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; 21import com.android.emailcommon.mail.MessagingException; 22 23import org.xmlpull.v1.XmlPullParserException; 24 25import android.content.Context; 26import android.content.res.XmlResourceParser; 27import android.util.Log; 28 29import java.io.IOException; 30import java.util.HashMap; 31 32public abstract class Sender { 33 protected static final int SOCKET_CONNECT_TIMEOUT = 10000; 34 35 private static final HashMap<String, Sender> sSenders = new HashMap<String, Sender>(); 36 37 /** 38 * Static named constructor. It should be overrode by extending class. 39 * Because this method will be called through reflection, it can not be protected. 40 */ 41 public static Sender newInstance(Context context, String uri) 42 throws MessagingException { 43 throw new MessagingException("Sender.newInstance: Unknown scheme in " + uri); 44 } 45 46 private static Sender instantiateSender(Context context, String className, String uri) 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", Context.class, String.class); 54 o = m.invoke(null, context, uri); 55 } catch (Exception e) { 56 Log.d(Email.LOG_TAG, String.format( 57 "exception %s invoking %s.newInstance.(Context, String) method for %s", 58 e.toString(), className, uri)); 59 throw new MessagingException("can not instantiate Sender object for " + uri); 60 } 61 if (!(o instanceof Sender)) { 62 throw new MessagingException( 63 uri + ": " + 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, String uri) 72 throws MessagingException { 73 Sender sender = null; 74 try { 75 XmlResourceParser xml = context.getResources().getXml(resourceId); 76 int xmlEventType; 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 scheme = xml.getAttributeValue(null, "scheme"); 82 if (uri.startsWith(scheme)) { 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, uri); 87 } 88 } 89 } 90 } catch (XmlPullParserException e) { 91 // ignore 92 } catch (IOException e) { 93 // ignore 94 } 95 return sender; 96 } 97 98 public synchronized static Sender getInstance(Context context, String uri) 99 throws MessagingException { 100 Sender sender = sSenders.get(uri); 101 if (sender == null) { 102 context = context.getApplicationContext(); 103 sender = findSender(context, R.xml.senders_product, uri); 104 if (sender == null) { 105 sender = findSender(context, R.xml.senders, uri); 106 } 107 108 if (sender != null) { 109 sSenders.put(uri, sender); 110 } 111 } 112 113 if (sender == null) { 114 throw new MessagingException("Unable to locate an applicable Transport for " + uri); 115 } 116 117 return sender; 118 } 119 120 /** 121 * Get class of SettingActivity for this Sender class. 122 * @return Activity class that has class method actionEditOutgoingSettings(). 123 */ 124 public Class<? extends android.app.Activity> getSettingActivityClass() { 125 // default SettingActivity class 126 return com.android.email.activity.setup.AccountSetupOutgoing.class; 127 } 128 129 public abstract void open() throws MessagingException; 130 131 public String validateSenderLimit(long messageId) { 132 return null; 133 } 134 135 /** 136 * Check message has any limitation of Sender or not. 137 * 138 * @param messageId the message that will be checked. 139 * @throws LimitViolationException 140 */ 141 public void checkSenderLimitation(long messageId) throws LimitViolationException { 142 } 143 144 public static class LimitViolationException extends MessagingException { 145 public final int mMsgResourceId; 146 public final long mActual; 147 public final long mLimit; 148 149 private LimitViolationException(int msgResourceId, long actual, long limit) { 150 super(UNSPECIFIED_EXCEPTION); 151 mMsgResourceId = msgResourceId; 152 mActual = actual; 153 mLimit = limit; 154 } 155 156 public static void check(int msgResourceId, long actual, long limit) 157 throws LimitViolationException { 158 if (actual > limit) { 159 throw new LimitViolationException(msgResourceId, actual, limit); 160 } 161 } 162 } 163 164 public abstract void sendMessage(long messageId) throws MessagingException; 165 166 public abstract void close() throws MessagingException; 167} 168