2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.mail;
|
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
import com.android.email.R;
|
2011-02-11 23:05:17 +00:00
|
|
|
import com.android.emailcommon.Logging;
|
2011-02-10 02:47:43 +00:00
|
|
|
import com.android.emailcommon.mail.MessagingException;
|
2011-04-20 15:04:46 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.Account;
|
|
|
|
import com.android.emailcommon.provider.EmailContent.HostAuth;
|
2009-03-27 23:59:36 +00:00
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.XmlResourceParser;
|
2011-04-20 15:04:46 +00:00
|
|
|
import android.text.TextUtils;
|
2009-04-03 22:42:31 +00:00
|
|
|
import android.util.Log;
|
2009-03-27 23:59:36 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2010-05-07 21:27:44 +00:00
|
|
|
import java.util.HashMap;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
public abstract class Sender {
|
|
|
|
protected static final int SOCKET_CONNECT_TIMEOUT = 10000;
|
|
|
|
|
2010-05-10 21:20:16 +00:00
|
|
|
private static final HashMap<String, Sender> sSenders = new HashMap<String, Sender>();
|
2009-03-27 23:59:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static named constructor. It should be overrode by extending class.
|
|
|
|
* Because this method will be called through reflection, it can not be protected.
|
|
|
|
*/
|
2011-04-20 15:04:46 +00:00
|
|
|
public static Sender newInstance(Context context, Account account)
|
2009-03-27 23:59:36 +00:00
|
|
|
throws MessagingException {
|
2011-04-20 15:04:46 +00:00
|
|
|
throw new MessagingException("Sender.newInstance: Unknown scheme in "
|
|
|
|
+ account.mDisplayName);
|
2009-03-27 23:59:36 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 15:04:46 +00:00
|
|
|
private static Sender instantiateSender(Context context, String className, Account account)
|
2009-03-27 23:59:36 +00:00
|
|
|
throws MessagingException {
|
|
|
|
Object o = null;
|
|
|
|
try {
|
|
|
|
Class<?> c = Class.forName(className);
|
|
|
|
// and invoke "newInstance" class method and instantiate sender object.
|
|
|
|
java.lang.reflect.Method m =
|
2011-04-20 15:04:46 +00:00
|
|
|
c.getMethod("newInstance", Account.class, Context.class);
|
|
|
|
o = m.invoke(null, account, context);
|
2009-03-27 23:59:36 +00:00
|
|
|
} catch (Exception e) {
|
2011-02-11 23:05:17 +00:00
|
|
|
Log.d(Logging.LOG_TAG, String.format(
|
2011-04-20 15:04:46 +00:00
|
|
|
"exception %s invoking method %s#newInstance(Account, Context) for %s",
|
|
|
|
e.toString(), className, account.mDisplayName));
|
|
|
|
throw new MessagingException("can not instantiate Sender for " + account.mDisplayName);
|
2009-03-27 23:59:36 +00:00
|
|
|
}
|
|
|
|
if (!(o instanceof Sender)) {
|
|
|
|
throw new MessagingException(
|
2011-04-20 15:04:46 +00:00
|
|
|
account.mDisplayName + ": " + className + " create incompatible object");
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2009-03-27 23:59:36 +00:00
|
|
|
return (Sender) o;
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
/**
|
|
|
|
* Find Sender implementation consulting with sender.xml file.
|
|
|
|
*/
|
2011-04-20 15:04:46 +00:00
|
|
|
private static Sender findSender(Context context, int resourceId, Account account)
|
2009-03-27 23:59:36 +00:00
|
|
|
throws MessagingException {
|
|
|
|
Sender sender = null;
|
|
|
|
try {
|
|
|
|
XmlResourceParser xml = context.getResources().getXml(resourceId);
|
|
|
|
int xmlEventType;
|
2011-04-20 15:04:46 +00:00
|
|
|
HostAuth sendAuth = account.getOrCreateHostAuthSend(context);
|
2009-03-27 23:59:36 +00:00
|
|
|
// walk through senders.xml file.
|
|
|
|
while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
|
|
|
|
if (xmlEventType == XmlResourceParser.START_TAG &&
|
|
|
|
"sender".equals(xml.getName())) {
|
2011-04-20 15:04:46 +00:00
|
|
|
String xmlScheme = xml.getAttributeValue(null, "scheme");
|
|
|
|
if (sendAuth.mProtocol != null && sendAuth.mProtocol.startsWith(xmlScheme)) {
|
2009-03-27 23:59:36 +00:00
|
|
|
// found sender entry whose scheme is matched with uri.
|
|
|
|
// then load sender class.
|
|
|
|
String className = xml.getAttributeValue(null, "class");
|
2011-04-20 15:04:46 +00:00
|
|
|
sender = instantiateSender(context, className, account);
|
2009-03-27 23:59:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (XmlPullParserException e) {
|
|
|
|
// ignore
|
|
|
|
} catch (IOException e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
return sender;
|
|
|
|
}
|
|
|
|
|
2011-04-20 15:04:46 +00:00
|
|
|
/**
|
|
|
|
* Gets a unique key for the given account.
|
|
|
|
* @throws MessagingException If the account is not setup properly (i.e. there is no address
|
|
|
|
* or login)
|
|
|
|
*/
|
|
|
|
private static String getSenderKey(Context context, Account account) throws MessagingException {
|
|
|
|
final StringBuffer key = new StringBuffer();
|
|
|
|
final HostAuth sendAuth = account.getOrCreateHostAuthSend(context);
|
|
|
|
if (sendAuth.mAddress == null) {
|
|
|
|
throw new MessagingException("Cannot find sender for account " + account.mDisplayName);
|
|
|
|
}
|
|
|
|
final String address = sendAuth.mAddress.trim();
|
|
|
|
if (TextUtils.isEmpty(address)) {
|
|
|
|
throw new MessagingException("Cannot find sender for account " + account.mDisplayName);
|
|
|
|
}
|
|
|
|
key.append(address);
|
|
|
|
if (sendAuth.mLogin != null) {
|
|
|
|
key.append(sendAuth.mLogin.trim());
|
|
|
|
}
|
|
|
|
return key.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of a mail sender for the given account. The account must be valid (i.e. has
|
|
|
|
* at least an outgoing server name).
|
|
|
|
*
|
|
|
|
* @param account The account of the sender.
|
|
|
|
* @return an initialized sender of the appropriate class
|
|
|
|
* @throws MessagingException If the sender cannot be obtained or if the account is invalid.
|
|
|
|
*/
|
|
|
|
public synchronized static Sender getInstance(Context context, Account account)
|
2009-03-27 23:59:36 +00:00
|
|
|
throws MessagingException {
|
2011-04-20 15:04:46 +00:00
|
|
|
String senderKey = getSenderKey(context, account);
|
|
|
|
Sender sender = sSenders.get(senderKey);
|
|
|
|
if (sender == null) {
|
|
|
|
Context appContext = context.getApplicationContext();
|
|
|
|
sender = findSender(appContext, R.xml.senders_product, account);
|
|
|
|
if (sender == null) {
|
|
|
|
sender = findSender(appContext, R.xml.senders, account);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sender != null) {
|
|
|
|
sSenders.put(senderKey, sender);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sender == null) {
|
|
|
|
throw new MessagingException("Cannot find sender for account " + account.mDisplayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sender;
|
2009-03-27 23:59:36 +00:00
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
/**
|
|
|
|
* Get class of SettingActivity for this Sender class.
|
2010-05-20 23:11:26 +00:00
|
|
|
* @return Activity class that has class method actionEditOutgoingSettings().
|
2009-03-27 23:59:36 +00:00
|
|
|
*/
|
|
|
|
public Class<? extends android.app.Activity> getSettingActivityClass() {
|
|
|
|
// default SettingActivity class
|
|
|
|
return com.android.email.activity.setup.AccountSetupOutgoing.class;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void open() throws MessagingException;
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-08-13 16:31:57 +00:00
|
|
|
public String validateSenderLimit(long messageId) {
|
2009-03-27 23:59:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
/**
|
|
|
|
* Check message has any limitation of Sender or not.
|
2010-05-20 23:11:26 +00:00
|
|
|
*
|
2009-08-13 16:31:57 +00:00
|
|
|
* @param messageId the message that will be checked.
|
2009-03-27 23:59:36 +00:00
|
|
|
* @throws LimitViolationException
|
|
|
|
*/
|
2009-08-13 16:31:57 +00:00
|
|
|
public void checkSenderLimitation(long messageId) throws LimitViolationException {
|
2009-03-27 23:59:36 +00:00
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
public static class LimitViolationException extends MessagingException {
|
|
|
|
public final int mMsgResourceId;
|
|
|
|
public final long mActual;
|
|
|
|
public final long mLimit;
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
private LimitViolationException(int msgResourceId, long actual, long limit) {
|
|
|
|
super(UNSPECIFIED_EXCEPTION);
|
|
|
|
mMsgResourceId = msgResourceId;
|
|
|
|
mActual = actual;
|
|
|
|
mLimit = limit;
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-03-27 23:59:36 +00:00
|
|
|
public static void check(int msgResourceId, long actual, long limit)
|
|
|
|
throws LimitViolationException {
|
|
|
|
if (actual > limit) {
|
|
|
|
throw new LimitViolationException(msgResourceId, actual, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-20 23:11:26 +00:00
|
|
|
|
2009-08-13 16:31:57 +00:00
|
|
|
public abstract void sendMessage(long messageId) throws MessagingException;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
public abstract void close() throws MessagingException;
|
|
|
|
}
|