From e7674272f9c9bd045591aff76e39ad1db1da36f9 Mon Sep 17 00:00:00 2001 From: Kris Wong Date: Thu, 5 Jan 2012 20:48:09 -0500 Subject: [PATCH] Miscellaneous cleanup in EAS support. --- .classpath | 2 +- project.properties | 2 +- src/com/fsck/k9/mail/store/EasStore.java | 44 +++++----- .../exchange/adapter/AbstractSyncParser.java | 86 ++++--------------- 4 files changed, 42 insertions(+), 92 deletions(-) diff --git a/.classpath b/.classpath index 779e2a6be..323d9666c 100644 --- a/.classpath +++ b/.classpath @@ -9,5 +9,5 @@ - + diff --git a/project.properties b/project.properties index 213821e1f..6b2d4bf93 100644 --- a/project.properties +++ b/project.properties @@ -11,5 +11,5 @@ split.density=false java.encoding=utf8 # Project target. -target=android-9 +target=android-10 extensible.libs.classpath=compile-only-libs diff --git a/src/com/fsck/k9/mail/store/EasStore.java b/src/com/fsck/k9/mail/store/EasStore.java index 57013f04a..86b30399e 100644 --- a/src/com/fsck/k9/mail/store/EasStore.java +++ b/src/com/fsck/k9/mail/store/EasStore.java @@ -88,7 +88,7 @@ public class EasStore extends Store { // This key is sent the first time we sync the folder hierarchy, and also the first time // we sync the items any "collection" (emails in a folder). - private static final String INITIAL_SYNC_KEY = "0"; + public static final String INITIAL_SYNC_KEY = "0"; private static final String PING_COMMAND = "Ping"; private static final String PROVISION_COMMAND = "Provision"; @@ -545,11 +545,8 @@ public class EasStore extends Store { try { init(); } - catch (IOException e) { - e.printStackTrace(); - } - catch (MessagingException e) { - e.printStackTrace(); + catch (Exception e) { + Log.e(K9.LOG_TAG, "Exception encountered while initializing EasStore", e); } } return mProtocolVersionDouble; @@ -585,7 +582,7 @@ public class EasStore extends Store { } } catch (MessagingException e) { - e.printStackTrace(); + Log.e(K9.LOG_TAG, "Exception encountered while provisioning in EasStore", e); } } return response; @@ -779,13 +776,9 @@ public class EasStore extends Store { } } else if (isProvisionError(code)) { // If the sync error is a provisioning failure (perhaps the policies changed), - // let's try the provisioning procedure - // Provisioning must only be attempted for the account mailbox - trying to - // provision any other mailbox may result in race conditions and the creation - // of multiple policy keys. + // let's try the provisioning procedure. if (!tryProvision()) { - // Set the appropriate failure status - throw new RuntimeException(); + throw new MessagingException("Unable to provision while tring to fetch the folder list"); } else { // If we succeeded, try again... return getInitialFolderList(); @@ -829,11 +822,12 @@ public class EasStore extends Store { mAccount.setSentFolderName(folder.getRemoteName()); break; case FolderSyncParser.OUTBOX_TYPE: - // Outbox folder is not synced. + // Outbox folder is not sync'd. break; } } + // We updated the account setting above, now write the settings to the DB. mAccount.saveFolderNames(Preferences.getPreferences(K9.app.getApplicationContext())); return folderList; @@ -878,7 +872,7 @@ public class EasStore extends Store { } } } catch (MessagingException e) { - e.printStackTrace(); + Log.e(K9.LOG_TAG, "Exception encountered while restoring folders from the local store", e); } } @@ -888,7 +882,7 @@ public class EasStore extends Store { try { getInitialFolderList(); } catch (MessagingException e) { - e.printStackTrace(); + Log.e(K9.LOG_TAG, "Exception encountered while fetching the initial folder list", e); } } synchronized (mFolderList) { @@ -1236,6 +1230,12 @@ public class EasStore extends Store { EasEmailSyncParser parser = new EasEmailSyncParser(is, this, mAccount); moreAvailable = parser.parse(); easMessages.addAll(parser.getMessages()); + + // If we got a new sync key from the server, make sure to update our member. + String newKey = parser.getNewSyncKey(); + if (newKey != null) { + setSyncKey(newKey); + } } else { Log.d(K9.LOG_TAG, "Empty input stream in sync command response"); } @@ -1669,7 +1669,13 @@ public class EasStore extends Store { InputStream is = resp.getEntity().getContent(); if (is != null) { EasEmailSyncParser syncParser = new EasEmailSyncParser(is, folder, folder.getAccount()); - parseResponse(syncParser, is); + syncParser.parse(); + + // If we got a new sync key from the server, make sure to update our member. + String newKey = syncParser.getNewSyncKey(); + if (newKey != null) { + folder.setSyncKey(newKey); + } } else { Log.d(K9.LOG_TAG, "Empty input stream in sync command response"); } @@ -1705,9 +1711,5 @@ public class EasStore extends Store { } abstract void prepareCommand(Serializer s) throws IOException; - - void parseResponse(EasEmailSyncParser syncParser, InputStream is) throws IOException, MessagingException { - syncParser.parse(); - } } } diff --git a/src/com/fsck/k9/mail/store/exchange/adapter/AbstractSyncParser.java b/src/com/fsck/k9/mail/store/exchange/adapter/AbstractSyncParser.java index 12e44b748..6c9bdf9ae 100644 --- a/src/com/fsck/k9/mail/store/exchange/adapter/AbstractSyncParser.java +++ b/src/com/fsck/k9/mail/store/exchange/adapter/AbstractSyncParser.java @@ -21,13 +21,12 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import android.content.ContentResolver; -import android.content.ContentValues; import android.util.Log; import com.fsck.k9.Account; import com.fsck.k9.K9; import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.store.EasStore; import com.fsck.k9.mail.store.EasStore.EasFolder; /** @@ -37,18 +36,19 @@ import com.fsck.k9.mail.store.EasStore.EasFolder; * */ public abstract class AbstractSyncParser extends Parser { - protected EasFolder mFolder; protected Account mAccount; - protected ContentResolver mContentResolver; - - private boolean mLooping; + protected String mNewSyncKey = null; public AbstractSyncParser(InputStream in, EasFolder folder, Account account) throws IOException { super(in); mFolder = folder; mAccount = account; } + + public String getNewSyncKey() { + return mNewSyncKey; + } /** * Read, parse, and act on incoming commands from the Exchange server @@ -74,10 +74,6 @@ public abstract class AbstractSyncParser extends Parser { */ public abstract void wipe(); - public boolean isLooping() { - return mLooping; - } - /** * Loop through the top-level structure coming from the Exchange server * Sync keys and the more available flag are handled here, whereas specific data parsing @@ -87,21 +83,16 @@ public abstract class AbstractSyncParser extends Parser { public boolean parse() throws IOException, MessagingException { int status; boolean moreAvailable = false; - boolean newSyncKey = false; - int interval = MailboxAdapter.mSyncInterval; - mLooping = false; - // If we're not at the top of the xml tree, throw an exception + + // If we're not at the top of the XML tree, throw an exception. if (nextTag(START_DOCUMENT) != Tags.SYNC_SYNC) { throw new EasParserException(); } - boolean mailboxUpdated = false; - ContentValues cv = new ContentValues(); - - // Loop here through the remaining xml + // Loop here through the remaining XML while (nextTag(START_DOCUMENT) != END_DOCUMENT) { if (tag == Tags.SYNC_COLLECTION || tag == Tags.SYNC_COLLECTIONS) { - // Ignore these tags, since we've only got one collection syncing in this loop + // Ignore these tags, since we've only got one collection sync'ing in this loop } else if (tag == Tags.SYNC_STATUS) { // Status = 1 is success; everything else is a failure status = getValueInt(); @@ -109,19 +100,17 @@ public abstract class AbstractSyncParser extends Parser { Log.e(K9.LOG_TAG, "Sync failed: " + status); // Status = 3 means invalid sync key if (status == 3) { - // Must delete all of the data and start over with syncKey of "0" - mFolder.setSyncKey("0"); - // Make this a push box through the first sync - // TODO Make frequency conditional on user settings! - MailboxAdapter.mSyncInterval = MailboxAdapter.CHECK_INTERVAL_PUSH; + // Must delete all of the data and start over with syncKey of "0". + mNewSyncKey = EasStore.INITIAL_SYNC_KEY; Log.e(K9.LOG_TAG, "Bad sync key; RESET and delete data"); wipe(); - // Indicate there's more so that we'll start syncing again + + // Indicate there's more so that we'll start sync'ing again. moreAvailable = true; } else if (status == 8) { // This is Bad; it means the server doesn't recognize the serverId it // sent us. What's needed is a refresh of the folder list. -// SyncManager.reloadFolderList(mContext, mAccount.mId, true); + // EASTODO: SyncManager.reloadFolderList(mContext, mAccount.mId, true); } // TODO Look at other error codes and consider what's to be done } @@ -138,57 +127,17 @@ public abstract class AbstractSyncParser extends Parser { String newKey = getValue(); userLog("Parsed key for ", mFolder.toString(), ": ", newKey); if (!newKey.equals(mFolder.getSyncKey())) { - mFolder.setSyncKey(newKey); - mailboxUpdated = true; - newSyncKey = true; - } - // If we were pushing (i.e. auto-start), now we'll become ping-triggered - if (MailboxAdapter.mSyncInterval == MailboxAdapter.CHECK_INTERVAL_PUSH) { - MailboxAdapter.mSyncInterval = MailboxAdapter.CHECK_INTERVAL_PING; + mNewSyncKey = newKey; } } else { skipTag(); } } - // If we don't have a new sync key, ignore moreAvailable (or we'll loop) - if (moreAvailable && !newSyncKey) { - mLooping = true; - } - // Commit any changes commit(); - boolean abortSyncs = false; - -// // If the sync interval has changed, we need to save it -// if (mMailbox.mSyncInterval != interval) { -// cv.put(MailboxColumns.SYNC_INTERVAL, mMailbox.mSyncInterval); -// mailboxUpdated = true; -// // If there are changes, and we were bounced from push/ping, try again -// } else if (mService.mChangeCount > 0 && -// mAccount.mSyncInterval == Account.CHECK_INTERVAL_PUSH && -// mMailbox.mSyncInterval > 0) { -// userLog("Changes found to ping loop mailbox ", mMailbox.mDisplayName, ": will ping."); -// cv.put(MailboxColumns.SYNC_INTERVAL, Mailbox.CHECK_INTERVAL_PING); -// mailboxUpdated = true; -// abortSyncs = true; -// } -// -// if (mailboxUpdated) { -// synchronized (mService.getSynchronizer()) { -// if (!mService.isStopped()) { -// mMailbox.update(mContext, cv); -// } -// } -// } -// -// if (abortSyncs) { -// userLog("Aborting account syncs due to mailbox change to ping..."); -// SyncManager.stopAccountSyncs(mAccount.mId); -// } - - // Let the caller know that there's more to do + // Let the caller know that there's more to do. userLog("Returning moreAvailable = " + moreAvailable); return moreAvailable; } @@ -200,5 +149,4 @@ public abstract class AbstractSyncParser extends Parser { void userLog(String string, int num, String string2) { Log.i(K9.LOG_TAG, string + num + string2); } - }