2009-06-16 19:03:45 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008-2009 Marc Blank
|
|
|
|
* Licensed to 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.
|
|
|
|
*/
|
|
|
|
|
2009-07-31 01:17:52 +00:00
|
|
|
package com.android.exchange;
|
2009-06-16 19:03:45 +00:00
|
|
|
|
2009-08-13 16:31:57 +00:00
|
|
|
import com.android.email.mail.MessagingException;
|
2009-08-11 22:15:04 +00:00
|
|
|
import com.android.email.mail.transport.Rfc822Output;
|
2009-09-08 23:44:00 +00:00
|
|
|
import com.android.email.provider.EmailContent.Body;
|
|
|
|
import com.android.email.provider.EmailContent.BodyColumns;
|
2009-07-23 22:31:28 +00:00
|
|
|
import com.android.email.provider.EmailContent.Mailbox;
|
2009-09-08 23:44:00 +00:00
|
|
|
import com.android.email.provider.EmailContent.MailboxColumns;
|
2009-07-23 22:31:28 +00:00
|
|
|
import com.android.email.provider.EmailContent.Message;
|
|
|
|
import com.android.email.provider.EmailContent.MessageColumns;
|
2009-08-11 22:15:04 +00:00
|
|
|
import com.android.email.provider.EmailContent.SyncColumns;
|
2009-06-16 19:03:45 +00:00
|
|
|
|
2009-08-08 20:26:03 +00:00
|
|
|
import org.apache.http.HttpResponse;
|
2009-08-14 16:42:24 +00:00
|
|
|
import org.apache.http.HttpStatus;
|
2009-08-12 17:35:40 +00:00
|
|
|
import org.apache.http.entity.InputStreamEntity;
|
2009-08-08 20:26:03 +00:00
|
|
|
|
2009-06-16 19:03:45 +00:00
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2009-08-20 05:51:42 +00:00
|
|
|
import android.os.RemoteException;
|
2009-06-16 19:03:45 +00:00
|
|
|
|
2009-08-12 17:35:40 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2009-07-23 22:31:28 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2009-07-05 19:54:49 +00:00
|
|
|
public class EasOutboxService extends EasSyncService {
|
2009-06-16 19:03:45 +00:00
|
|
|
|
2009-08-12 17:35:40 +00:00
|
|
|
public static final int SEND_FAILED = 1;
|
|
|
|
public static final String MAILBOX_KEY_AND_NOT_SEND_FAILED =
|
2009-09-23 17:52:00 +00:00
|
|
|
MessageColumns.MAILBOX_KEY + "=? and (" + SyncColumns.SERVER_ID + " is null or " +
|
|
|
|
SyncColumns.SERVER_ID + "!=" + SEND_FAILED + ')';
|
2009-09-08 23:44:00 +00:00
|
|
|
public static final String[] BODY_SOURCE_PROJECTION =
|
|
|
|
new String[] {BodyColumns.SOURCE_MESSAGE_KEY};
|
|
|
|
public static final String WHERE_MESSAGE_KEY = Body.MESSAGE_KEY + "=?";
|
2009-08-12 17:35:40 +00:00
|
|
|
|
2009-10-09 17:05:41 +00:00
|
|
|
// This needs to be long enough to send the longest reasonable message, without being so long
|
|
|
|
// as to effectively "hang" sending of mail. The standard 30 second timeout isn't long enough
|
|
|
|
// for pictures and the like. For now, we'll use 15 minutes, in the knowledge that any socket
|
|
|
|
// failure would probably generate an Exception before timing out anyway
|
|
|
|
public static final int SEND_MAIL_TIMEOUT = 15*MINUTES;
|
|
|
|
|
2009-06-24 00:50:42 +00:00
|
|
|
public EasOutboxService(Context _context, Mailbox _mailbox) {
|
2009-06-16 19:03:45 +00:00
|
|
|
super(_context, _mailbox);
|
2009-08-12 17:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-08-20 05:51:42 +00:00
|
|
|
private void sendCallback(long msgId, String subject, int status) {
|
|
|
|
try {
|
|
|
|
SyncManager.callback().sendMessageStatus(msgId, subject, status, 0);
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
// It's all good
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:35:40 +00:00
|
|
|
/**
|
|
|
|
* Send a single message via EAS
|
|
|
|
* Note that we mark messages SEND_FAILED when there is a permanent failure, rather than an
|
|
|
|
* IOException, which is handled by SyncManager with retries, backoffs, etc.
|
|
|
|
*
|
|
|
|
* @param cacheDir the cache directory for this context
|
|
|
|
* @param msgId the _id of the message to send
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2009-08-20 19:04:25 +00:00
|
|
|
int sendMessage(File cacheDir, long msgId) throws IOException, MessagingException {
|
|
|
|
int result;
|
2009-08-20 05:51:42 +00:00
|
|
|
sendCallback(msgId, null, EmailServiceStatus.IN_PROGRESS);
|
2009-08-12 17:35:40 +00:00
|
|
|
File tmpFile = File.createTempFile("eas_", "tmp", cacheDir);
|
|
|
|
// Write the output to a temporary file
|
|
|
|
try {
|
2009-09-08 23:44:00 +00:00
|
|
|
String[] cols = getRowColumns(Message.CONTENT_URI, msgId, MessageColumns.FLAGS,
|
|
|
|
MessageColumns.SUBJECT);
|
|
|
|
int flags = Integer.parseInt(cols[0]);
|
|
|
|
String subject = cols[1];
|
|
|
|
|
|
|
|
boolean reply = (flags & Message.FLAG_TYPE_REPLY) != 0;
|
|
|
|
boolean forward = (flags & Message.FLAG_TYPE_FORWARD) != 0;
|
|
|
|
// The reference message and mailbox are called item and collection in EAS
|
|
|
|
String itemId = null;
|
|
|
|
String collectionId = null;
|
|
|
|
if (reply || forward) {
|
|
|
|
// First, we need to get the id of the reply/forward message
|
|
|
|
cols = getRowColumns(Body.CONTENT_URI, BODY_SOURCE_PROJECTION,
|
|
|
|
WHERE_MESSAGE_KEY, new String[] {Long.toString(msgId)});
|
|
|
|
if (cols != null) {
|
|
|
|
long refId = Long.parseLong(cols[0]);
|
|
|
|
// Then, we need the serverId and mailboxKey of the message
|
|
|
|
cols = getRowColumns(Message.CONTENT_URI, refId, SyncColumns.SERVER_ID,
|
|
|
|
MessageColumns.MAILBOX_KEY);
|
|
|
|
if (cols != null) {
|
|
|
|
itemId = cols[0];
|
|
|
|
long boxId = Long.parseLong(cols[1]);
|
|
|
|
// Then, we need the serverId of the mailbox
|
|
|
|
cols = getRowColumns(Mailbox.CONTENT_URI, boxId, MailboxColumns.SERVER_ID);
|
|
|
|
if (cols != null) {
|
|
|
|
collectionId = cols[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean smartSend = itemId != null && collectionId != null;
|
|
|
|
|
|
|
|
// Write the message in rfc822 format to the temporary file
|
2009-08-12 17:35:40 +00:00
|
|
|
FileOutputStream fileStream = new FileOutputStream(tmpFile);
|
2009-09-15 21:03:14 +00:00
|
|
|
Rfc822Output.writeTo(mContext, msgId, fileStream, !smartSend, true);
|
2009-08-12 17:35:40 +00:00
|
|
|
fileStream.close();
|
2009-09-08 23:44:00 +00:00
|
|
|
|
|
|
|
// Now, get an input stream to our temporary file and create an entity with it
|
2009-08-12 17:35:40 +00:00
|
|
|
FileInputStream inputStream = new FileInputStream(tmpFile);
|
|
|
|
InputStreamEntity inputEntity =
|
|
|
|
new InputStreamEntity(inputStream, tmpFile.length());
|
2009-09-08 23:44:00 +00:00
|
|
|
|
|
|
|
// Create the appropriate command and POST it to the server
|
|
|
|
String cmd = "SendMail&SaveInSent=T";
|
|
|
|
if (smartSend) {
|
|
|
|
cmd = reply ? "SmartReply" : "SmartForward";
|
2009-09-17 22:47:51 +00:00
|
|
|
cmd += "&ItemId=" + itemId + "&CollectionId=" + collectionId + "&SaveInSent=T";
|
2009-09-08 23:44:00 +00:00
|
|
|
}
|
2009-09-17 22:20:00 +00:00
|
|
|
userLog("Send cmd: " + cmd);
|
2009-10-09 17:05:41 +00:00
|
|
|
HttpResponse resp = sendHttpClientPost(cmd, inputEntity, SEND_MAIL_TIMEOUT);
|
2009-09-08 23:44:00 +00:00
|
|
|
|
2009-08-12 17:35:40 +00:00
|
|
|
inputStream.close();
|
|
|
|
int code = resp.getStatusLine().getStatusCode();
|
2009-08-14 16:42:24 +00:00
|
|
|
if (code == HttpStatus.SC_OK) {
|
2009-08-12 17:35:40 +00:00
|
|
|
userLog("Deleting message...");
|
2009-09-08 23:44:00 +00:00
|
|
|
mContentResolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, msgId),
|
|
|
|
null, null);
|
2009-08-20 19:04:25 +00:00
|
|
|
result = EmailServiceStatus.SUCCESS;
|
2009-09-08 23:44:00 +00:00
|
|
|
sendCallback(-1, subject, EmailServiceStatus.SUCCESS);
|
2009-08-12 17:35:40 +00:00
|
|
|
} else {
|
2009-09-17 22:20:00 +00:00
|
|
|
userLog("Message sending failed, code: " + code);
|
2009-08-12 17:35:40 +00:00
|
|
|
ContentValues cv = new ContentValues();
|
|
|
|
cv.put(SyncColumns.SERVER_ID, SEND_FAILED);
|
|
|
|
Message.update(mContext, Message.CONTENT_URI, msgId, cv);
|
2009-08-20 19:04:25 +00:00
|
|
|
result = EmailServiceStatus.REMOTE_EXCEPTION;
|
2009-08-20 05:51:42 +00:00
|
|
|
if (isAuthError(code)) {
|
|
|
|
result = EmailServiceStatus.LOGIN_FAILED;
|
|
|
|
}
|
|
|
|
sendCallback(msgId, null, result);
|
2009-08-20 19:04:25 +00:00
|
|
|
|
2009-08-12 17:35:40 +00:00
|
|
|
}
|
2009-08-20 05:51:42 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
// We catch this just to send the callback
|
|
|
|
sendCallback(msgId, null, EmailServiceStatus.CONNECTION_ERROR);
|
|
|
|
throw e;
|
2009-08-12 17:35:40 +00:00
|
|
|
} finally {
|
|
|
|
// Clean up the temporary file
|
|
|
|
if (tmpFile.exists()) {
|
|
|
|
tmpFile.delete();
|
|
|
|
}
|
|
|
|
}
|
2009-08-20 19:04:25 +00:00
|
|
|
return result;
|
2009-06-16 19:03:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 20:26:03 +00:00
|
|
|
@Override
|
2009-07-05 19:54:49 +00:00
|
|
|
public void run() {
|
2009-08-19 21:23:21 +00:00
|
|
|
setupService();
|
2009-08-12 17:35:40 +00:00
|
|
|
File cacheDir = mContext.getCacheDir();
|
2009-06-16 19:03:45 +00:00
|
|
|
try {
|
2009-09-08 23:44:00 +00:00
|
|
|
mDeviceId = SyncManager.getDeviceId();
|
2009-08-08 20:26:03 +00:00
|
|
|
Cursor c = mContext.getContentResolver().query(Message.CONTENT_URI,
|
2009-08-12 17:35:40 +00:00
|
|
|
Message.ID_COLUMN_PROJECTION, MAILBOX_KEY_AND_NOT_SEND_FAILED,
|
|
|
|
new String[] {Long.toString(mMailbox.mId)}, null);
|
|
|
|
try {
|
2009-07-05 19:54:49 +00:00
|
|
|
while (c.moveToNext()) {
|
2009-08-12 17:35:40 +00:00
|
|
|
long msgId = c.getLong(0);
|
|
|
|
if (msgId != 0) {
|
2009-08-20 19:04:25 +00:00
|
|
|
int result = sendMessage(cacheDir, msgId);
|
|
|
|
// If there's an error, it should stop the service; we will distinguish
|
|
|
|
// at least between login failures and everything else
|
|
|
|
if (result == EmailServiceStatus.LOGIN_FAILED) {
|
|
|
|
mExitStatus = EXIT_LOGIN_FAILURE;
|
|
|
|
return;
|
|
|
|
} else if (result == EmailServiceStatus.REMOTE_EXCEPTION) {
|
|
|
|
mExitStatus = EXIT_EXCEPTION;
|
|
|
|
return;
|
|
|
|
}
|
2009-06-16 19:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
2009-08-12 17:35:40 +00:00
|
|
|
c.close();
|
2009-06-16 19:03:45 +00:00
|
|
|
}
|
2009-08-20 19:04:25 +00:00
|
|
|
mExitStatus = EXIT_DONE;
|
2009-09-08 23:44:00 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
mExitStatus = EXIT_IO_ERROR;
|
2009-07-05 19:54:49 +00:00
|
|
|
} catch (Exception e) {
|
2009-09-17 22:20:00 +00:00
|
|
|
userLog("Exception caught in EasOutboxService", e);
|
2009-07-05 19:54:49 +00:00
|
|
|
mExitStatus = EXIT_EXCEPTION;
|
|
|
|
} finally {
|
2009-08-13 16:32:58 +00:00
|
|
|
userLog(mMailbox.mDisplayName, ": sync finished");
|
2009-08-20 19:04:25 +00:00
|
|
|
userLog("Outbox exited with status ", mExitStatus);
|
2009-07-05 19:54:49 +00:00
|
|
|
SyncManager.done(this);
|
2009-06-16 19:03:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|