Fix bugs re: sending via EAS

* Make sure each call to sendMessage returns a proper result code
* Exit outbox sync on irrecoverable errors
This commit is contained in:
Marc Blank 2009-08-20 12:04:25 -07:00
parent c0c9c33322
commit 9178dcc9e9
2 changed files with 19 additions and 4 deletions

View File

@ -67,7 +67,8 @@ public class EasOutboxService extends EasSyncService {
* @param msgId the _id of the message to send * @param msgId the _id of the message to send
* @throws IOException * @throws IOException
*/ */
void sendMessage(File cacheDir, long msgId) throws IOException, MessagingException { int sendMessage(File cacheDir, long msgId) throws IOException, MessagingException {
int result;
sendCallback(msgId, null, EmailServiceStatus.IN_PROGRESS); sendCallback(msgId, null, EmailServiceStatus.IN_PROGRESS);
File tmpFile = File.createTempFile("eas_", "tmp", cacheDir); File tmpFile = File.createTempFile("eas_", "tmp", cacheDir);
// Write the output to a temporary file // Write the output to a temporary file
@ -93,17 +94,19 @@ public class EasOutboxService extends EasSyncService {
Message msg = Message.restoreMessageWithId(mContext, msgId); Message msg = Message.restoreMessageWithId(mContext, msgId);
mContext.getContentResolver().delete(ContentUris.withAppendedId( mContext.getContentResolver().delete(ContentUris.withAppendedId(
Message.CONTENT_URI, msgId), null, null); Message.CONTENT_URI, msgId), null, null);
result = EmailServiceStatus.SUCCESS;
sendCallback(-1, msg.mSubject, EmailServiceStatus.SUCCESS); sendCallback(-1, msg.mSubject, EmailServiceStatus.SUCCESS);
} else { } else {
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.put(SyncColumns.SERVER_ID, SEND_FAILED); cv.put(SyncColumns.SERVER_ID, SEND_FAILED);
Message.update(mContext, Message.CONTENT_URI, msgId, cv); Message.update(mContext, Message.CONTENT_URI, msgId, cv);
// TODO REMOTE_EXCEPTION is temporary; add better error codes // TODO REMOTE_EXCEPTION is temporary; add better error codes
int result = EmailServiceStatus.REMOTE_EXCEPTION; result = EmailServiceStatus.REMOTE_EXCEPTION;
if (isAuthError(code)) { if (isAuthError(code)) {
result = EmailServiceStatus.LOGIN_FAILED; result = EmailServiceStatus.LOGIN_FAILED;
} }
sendCallback(msgId, null, result); sendCallback(msgId, null, result);
} }
} catch (IOException e) { } catch (IOException e) {
// We catch this just to send the callback // We catch this just to send the callback
@ -115,6 +118,7 @@ public class EasOutboxService extends EasSyncService {
tmpFile.delete(); tmpFile.delete();
} }
} }
return result;
} }
@Override @Override
@ -130,16 +134,27 @@ public class EasOutboxService extends EasSyncService {
while (c.moveToNext()) { while (c.moveToNext()) {
long msgId = c.getLong(0); long msgId = c.getLong(0);
if (msgId != 0) { if (msgId != 0) {
sendMessage(cacheDir, msgId); 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;
}
} }
} }
} finally { } finally {
c.close(); c.close();
} }
mExitStatus = EXIT_DONE;
} catch (Exception e) { } catch (Exception e) {
mExitStatus = EXIT_EXCEPTION; mExitStatus = EXIT_EXCEPTION;
} finally { } finally {
userLog(mMailbox.mDisplayName, ": sync finished"); userLog(mMailbox.mDisplayName, ": sync finished");
userLog("Outbox exited with status ", mExitStatus);
SyncManager.done(this); SyncManager.done(this);
} }
} }

View File

@ -1047,7 +1047,7 @@ public class SyncManager extends Service implements Runnable {
// We also observe synced messages to trigger upsyncs at the appropriate time // We also observe synced messages to trigger upsyncs at the appropriate time
mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver); mResolver.registerContentObserver(Mailbox.CONTENT_URI, false, mMailboxObserver);
mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver); mResolver.registerContentObserver(Message.SYNCED_CONTENT_URI, true, mSyncedMessageObserver);
mResolver.registerContentObserver(Message.CONTENT_URI, false, mMessageObserver); mResolver.registerContentObserver(Message.CONTENT_URI, true, mMessageObserver);
mConnectivityReceiver = new ConnectivityReceiver(); mConnectivityReceiver = new ConnectivityReceiver();
registerReceiver(mConnectivityReceiver, registerReceiver(mConnectivityReceiver,