From 22f422ea29e34e6cab0a8e59e67957461c711478 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Mon, 24 Jan 2011 03:27:14 +0000 Subject: [PATCH] Use a heuristic to try to avoid notifying POP3 users about older mail messages --- src/com/fsck/k9/Account.java | 26 +++++++++----- .../k9/controller/MessagingController.java | 33 +++++++++++++++++ src/com/fsck/k9/mail/store/LocalStore.java | 35 +++++++++++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/src/com/fsck/k9/Account.java b/src/com/fsck/k9/Account.java index a405597b5..0478376d7 100644 --- a/src/com/fsck/k9/Account.java +++ b/src/com/fsck/k9/Account.java @@ -78,6 +78,7 @@ public class Account implements BaseAccount private int mDisplayCount; private int mChipColor; private long mLastAutomaticCheckTime; + private long mLatestOldMessageSeenTime; private boolean mNotifyNewMail; private boolean mNotifySelfNewMail; private String mDraftsFolderName; @@ -257,14 +258,11 @@ public class Account implements BaseAccount { mDisplayCount = K9.DEFAULT_VISIBLE_LIMIT; } - mLastAutomaticCheckTime = prefs.getLong(mUuid - + ".lastAutomaticCheckTime", 0); - mNotifyNewMail = prefs.getBoolean(mUuid + ".notifyNewMail", - false); - mNotifySelfNewMail = prefs.getBoolean(mUuid + ".notifySelfNewMail", - true); - mNotifySync = prefs.getBoolean(mUuid + ".notifyMailCheck", - false); + mLastAutomaticCheckTime = prefs.getLong(mUuid + ".lastAutomaticCheckTime", 0); + mLatestOldMessageSeenTime = prefs.getLong(mUuid +".latestOldMessageSeenTime",0); + mNotifyNewMail = prefs.getBoolean(mUuid + ".notifyNewMail", false); + mNotifySelfNewMail = prefs.getBoolean(mUuid + ".notifySelfNewMail", true); + mNotifySync = prefs.getBoolean(mUuid + ".notifyMailCheck", false); mDeletePolicy = prefs.getInt(mUuid + ".deletePolicy", 0); mDraftsFolderName = prefs.getString(mUuid + ".draftsFolderName", "Drafts"); @@ -445,6 +443,7 @@ public class Account implements BaseAccount editor.remove(mUuid + ".saveAllHeaders"); editor.remove(mUuid + ".idleRefreshMinutes"); editor.remove(mUuid + ".lastAutomaticCheckTime"); + editor.remove(mUuid + ".latestOldMessageSeenTime"); editor.remove(mUuid + ".notifyNewMail"); editor.remove(mUuid + ".notifySelfNewMail"); editor.remove(mUuid + ".deletePolicy"); @@ -546,6 +545,7 @@ public class Account implements BaseAccount editor.putBoolean(mUuid + ".pushPollOnConnect", mPushPollOnConnect); editor.putInt(mUuid + ".displayCount", mDisplayCount); editor.putLong(mUuid + ".lastAutomaticCheckTime", mLastAutomaticCheckTime); + editor.putLong(mUuid + ".latestOldMessageSeenTime", mLatestOldMessageSeenTime); editor.putBoolean(mUuid + ".notifyNewMail", mNotifyNewMail); editor.putBoolean(mUuid + ".notifySelfNewMail", mNotifySelfNewMail); editor.putBoolean(mUuid + ".notifyMailCheck", mNotifySync); @@ -850,6 +850,16 @@ public class Account implements BaseAccount this.mLastAutomaticCheckTime = lastAutomaticCheckTime; } + public synchronized long getLatestOldMessageSeenTime() + { + return mLatestOldMessageSeenTime; + } + + public synchronized void setLatestOldMessageSeenTime(long latestOldMessageSeenTime) + { + this.mLatestOldMessageSeenTime = latestOldMessageSeenTime; + } + public synchronized boolean isNotifyNewMail() { return mNotifyNewMail; diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index 8812f1a10..ed496fd1a 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -1396,6 +1396,9 @@ public class MessagingController implements Runnable final LocalFolder localFolder, List inputMessages, boolean flagSyncOnly) throws MessagingException { final Date earliestDate = account.getEarliestPollDate(); + Date downloadStarted = new Date(); // now + + if (earliestDate != null) { if (K9.DEBUG) @@ -1540,6 +1543,25 @@ public class MessagingController implements Runnable }); + // If the oldest message seen on this sync is newer than + // the oldest message seen on the previous sync, then + // we want to move our high-water mark forward + // this is all here just for pop which only syncs inbox + // this would be a little wrong for IMAP (we'd want a folder-level pref, not an account level pref.) + // fortunately, we just don't care. + Long oldestMessageTime = localFolder.getOldestMessageDate(); + + if (oldestMessageTime != null) + { + Date oldestExtantMessage = new Date(oldestMessageTime); + if (oldestExtantMessage.before(downloadStarted) && + oldestExtantMessage.after(new Date(account.getLatestOldMessageSeenTime()))) + { + account.setLatestOldMessageSeenTime(oldestExtantMessage.getTime()); + account.save(Preferences.getPreferences(mApplication.getApplicationContext())); + } + + } return newMessages.get(); } private void evaluateMessageForDownload(final Message message, final String folder, @@ -4693,6 +4715,17 @@ public class MessagingController implements Runnable return false; } + // If the account us a POP3 account and the message is older than + // the oldest message we've previously seen then don't notify about it + if (account.getStoreUri().startsWith("pop3") ) + { + if ( message.olderThan(new Date(account.getLatestOldMessageSeenTime()))) + { + return false; + } + } + + // If we have a message, set the notification to ": " StringBuilder messageNotice = new StringBuilder(); final KeyguardManager keyguardService = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 337661593..26ba82715 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -3281,6 +3281,41 @@ public class LocalStore extends Store implements Serializable Log.d(K9.LOG_TAG, "Updated last UID for folder " + mName + " to " + lastUid); mLastUid = lastUid; } + + public long getOldestMessageDate() throws MessagingException + { + return database.execute(false, new DbCallback() + { + @Override + public Long doDbWork(final SQLiteDatabase db) + { + Cursor cursor = null; + try + { + open(OpenMode.READ_ONLY); + cursor = db.rawQuery("SELECT MIN(date) FROM messages WHERE folder_id=?", new String[] { Long.toString(mFolderId) }); + if (cursor.getCount() > 0) + { + cursor.moveToFirst(); + return cursor.getLong(0); + } + } + catch (Exception e) + { + Log.e(K9.LOG_TAG, "Unable to fetch oldest message date: ", e); + } + finally + { + if (cursor != null) + { + cursor.close(); + } + } + return null; + } + }); + } + } public static class LocalTextBody extends TextBody