1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Remove all code using the old unread/flagged count database columns

This commit is contained in:
cketti 2012-12-07 15:45:36 +01:00
parent 04ec2abd2c
commit bc4fb530d0
3 changed files with 34 additions and 213 deletions

View File

@ -971,10 +971,7 @@ public class MessagingController implements Runnable {
*/ */
int newMessages = downloadMessages(account, remoteFolder, localFolder, remoteMessages, false); int newMessages = downloadMessages(account, remoteFolder, localFolder, remoteMessages, false);
int unreadMessageCount = setLocalUnreadCountToRemote(localFolder, remoteFolder, newMessages); int unreadMessageCount = localFolder.getUnreadMessageCount();
setLocalFlaggedCountToRemote(localFolder, remoteFolder);
for (MessagingListener l : getListeners()) { for (MessagingListener l : getListeners()) {
l.folderStatusChanged(account, folder, unreadMessageCount); l.folderStatusChanged(account, folder, unreadMessageCount);
} }
@ -1070,38 +1067,6 @@ public class MessagingController implements Runnable {
} }
return true; return true;
} }
private int setLocalUnreadCountToRemote(LocalFolder localFolder, Folder remoteFolder, int newMessageCount) throws MessagingException {
int remoteUnreadMessageCount = remoteFolder.getUnreadMessageCount();
if (remoteUnreadMessageCount != -1) {
localFolder.setUnreadMessageCount(remoteUnreadMessageCount);
} else {
int unreadCount = 0;
Message[] messages = localFolder.getMessages(null, false);
for (Message message : messages) {
if (!message.isSet(Flag.SEEN) && !message.isSet(Flag.DELETED)) {
unreadCount++;
}
}
localFolder.setUnreadMessageCount(unreadCount);
}
return localFolder.getUnreadMessageCount();
}
private void setLocalFlaggedCountToRemote(LocalFolder localFolder, Folder remoteFolder) throws MessagingException {
int remoteFlaggedMessageCount = remoteFolder.getFlaggedMessageCount();
if (remoteFlaggedMessageCount != -1) {
localFolder.setFlaggedMessageCount(remoteFlaggedMessageCount);
} else {
int flaggedCount = 0;
Message[] messages = localFolder.getMessages(null, false);
for (Message message : messages) {
if (message.isSet(Flag.FLAGGED) && !message.isSet(Flag.DELETED)) {
flaggedCount++;
}
}
localFolder.setFlaggedMessageCount(flaggedCount);
}
}
/** /**
* Fetches the messages described by inputMessages from the remote store and writes them to * Fetches the messages described by inputMessages from the remote store and writes them to
@ -2453,7 +2418,7 @@ public class MessagingController implements Runnable {
} }
} }
} }
localFolder.setUnreadMessageCount(0);
for (MessagingListener l : getListeners()) { for (MessagingListener l : getListeners()) {
l.folderStatusChanged(account, folder, 0); l.folderStatusChanged(account, folder, 0);
} }
@ -3947,8 +3912,6 @@ public class MessagingController implements Runnable {
localFolder = (LocalFolder) localStore.getFolder(account.getTrashFolderName()); localFolder = (LocalFolder) localStore.getFolder(account.getTrashFolderName());
localFolder.open(OpenMode.READ_WRITE); localFolder.open(OpenMode.READ_WRITE);
localFolder.setFlags(new Flag[] { Flag.DELETED }, true); localFolder.setFlags(new Flag[] { Flag.DELETED }, true);
localFolder.setUnreadMessageCount(0);
localFolder.setFlaggedMessageCount(0);
for (MessagingListener l : getListeners()) { for (MessagingListener l : getListeners()) {
l.emptyTrashCompleted(account); l.emptyTrashCompleted(account);
@ -4797,9 +4760,8 @@ public class MessagingController implements Runnable {
account.setRingNotified(false); account.setRingNotified(false);
int newCount = downloadMessages(account, remoteFolder, localFolder, messages, flagSyncOnly); int newCount = downloadMessages(account, remoteFolder, localFolder, messages, flagSyncOnly);
int unreadMessageCount = setLocalUnreadCountToRemote(localFolder, remoteFolder, messages.size());
setLocalFlaggedCountToRemote(localFolder, remoteFolder); int unreadMessageCount = localFolder.getUnreadMessageCount();
localFolder.setLastPush(System.currentTimeMillis()); localFolder.setLastPush(System.currentTimeMillis());
localFolder.setStatus(null); localFolder.setStatus(null);

View File

@ -38,7 +38,6 @@ import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.fsck.k9.Account; import com.fsck.k9.Account;
import com.fsck.k9.AccountStats;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.Preferences; import com.fsck.k9.Preferences;
import com.fsck.k9.R; import com.fsck.k9.R;
@ -703,81 +702,6 @@ public class LocalStore extends Store implements Serializable {
}); });
} }
public void getMessageCounts(final AccountStats stats) throws MessagingException {
final Account.FolderMode displayMode = mAccount.getFolderDisplayMode();
database.execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) {
Cursor cursor = null;
try {
// Always count messages in the INBOX but exclude special folders and possibly
// more (depending on the folder display mode)
String baseQuery = "SELECT SUM(unread_count), SUM(flagged_count) " +
"FROM folders " +
"WHERE (name = ?)" + /* INBOX */
" OR (" +
"name NOT IN (?, ?, ?, ?, ?)" + /* special folders */
"%s)"; /* placeholder for additional constraints */
List<String> queryParam = new ArrayList<String>();
queryParam.add(mAccount.getInboxFolderName());
queryParam.add((mAccount.getTrashFolderName() != null) ?
mAccount.getTrashFolderName() : "");
queryParam.add((mAccount.getDraftsFolderName() != null) ?
mAccount.getDraftsFolderName() : "");
queryParam.add((mAccount.getSpamFolderName() != null) ?
mAccount.getSpamFolderName() : "");
queryParam.add((mAccount.getOutboxFolderName() != null) ?
mAccount.getOutboxFolderName() : "");
queryParam.add((mAccount.getSentFolderName() != null) ?
mAccount.getSentFolderName() : "");
final String extraWhere;
switch (displayMode) {
case FIRST_CLASS:
// Count messages in the INBOX and non-special first class folders
extraWhere = " AND (display_class = ?)";
queryParam.add(Folder.FolderClass.FIRST_CLASS.name());
break;
case FIRST_AND_SECOND_CLASS:
// Count messages in the INBOX and non-special first and second class folders
extraWhere = " AND (display_class IN (?, ?))";
queryParam.add(Folder.FolderClass.FIRST_CLASS.name());
queryParam.add(Folder.FolderClass.SECOND_CLASS.name());
break;
case NOT_SECOND_CLASS:
// Count messages in the INBOX and non-special non-second-class folders
extraWhere = " AND (display_class != ?)";
queryParam.add(Folder.FolderClass.SECOND_CLASS.name());
break;
case ALL:
// Count messages in the INBOX and non-special folders
extraWhere = "";
break;
default:
Log.e(K9.LOG_TAG, "asked to compute account statistics for an impossible folder mode " + displayMode);
stats.unreadMessageCount = 0;
stats.flaggedMessageCount = 0;
return null;
}
String query = String.format(Locale.US, baseQuery, extraWhere);
cursor = db.rawQuery(query, queryParam.toArray(EMPTY_STRING_ARRAY));
cursor.moveToFirst();
stats.unreadMessageCount = cursor.getInt(0);
stats.flaggedMessageCount = cursor.getInt(1);
return null;
} finally {
Utility.closeQuietly(cursor);
}
}
});
}
public int getFolderCount() throws MessagingException { public int getFolderCount() throws MessagingException {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
@ -1493,8 +1417,36 @@ public class LocalStore extends Store implements Serializable {
@Override @Override
public int getUnreadMessageCount() throws MessagingException { public int getUnreadMessageCount() throws MessagingException {
open(OpenMode.READ_WRITE); if (!isOpen()) {
return mUnreadMessageCount; // open() sums up the number of unread messages in the database
open(OpenMode.READ_WRITE);
return mUnreadMessageCount;
}
// Folder was already opened. Unread count might be outdated so query the database now.
try {
return database.execute(false, new DbCallback<Integer>() {
@Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException {
int unreadMessageCount = 0;
Cursor cursor = db.query("messages", new String[] { "SUM(read=0)" },
"folder_id = ? AND (empty IS NULL OR empty != 1) AND deleted = 0",
new String[] { Long.toString(mFolderId) }, null, null, null);
try {
if (cursor.moveToFirst()) {
unreadMessageCount = cursor.getInt(0);
}
} finally {
cursor.close();
}
return unreadMessageCount;
}
});
} catch (WrappedException e) {
throw(MessagingException) e.getCause();
}
} }
@Override @Override
@ -1503,16 +1455,6 @@ public class LocalStore extends Store implements Serializable {
return mFlaggedMessageCount; return mFlaggedMessageCount;
} }
public void setUnreadMessageCount(final int unreadMessageCount) throws MessagingException {
mUnreadMessageCount = Math.max(0, unreadMessageCount);
updateFolderColumn("unread_count", mUnreadMessageCount);
}
public void setFlaggedMessageCount(final int flaggedMessageCount) throws MessagingException {
mFlaggedMessageCount = Math.max(0, flaggedMessageCount);
updateFolderColumn("flagged_count", mFlaggedMessageCount);
}
@Override @Override
public void setLastChecked(final long lastChecked) throws MessagingException { public void setLastChecked(final long lastChecked) throws MessagingException {
try { try {
@ -2152,16 +2094,6 @@ public class LocalStore extends Store implements Serializable {
for (Message message : msgs) { for (Message message : msgs) {
LocalMessage lMessage = (LocalMessage)message; LocalMessage lMessage = (LocalMessage)message;
if (!message.isSet(Flag.SEEN)) {
setUnreadMessageCount(getUnreadMessageCount() - 1);
lDestFolder.setUnreadMessageCount(lDestFolder.getUnreadMessageCount() + 1);
}
if (message.isSet(Flag.FLAGGED)) {
setFlaggedMessageCount(getFlaggedMessageCount() - 1);
lDestFolder.setFlaggedMessageCount(lDestFolder.getFlaggedMessageCount() + 1);
}
String oldUID = message.getUid(); String oldUID = message.getUid();
if (K9.DEBUG) { if (K9.DEBUG) {
@ -2476,13 +2408,6 @@ public class LocalStore extends Store implements Serializable {
if (oldMessage != null) { if (oldMessage != null) {
oldMessageId = oldMessage.getId(); oldMessageId = oldMessage.getId();
if (!oldMessage.isSet(Flag.SEEN)) {
setUnreadMessageCount(getUnreadMessageCount() - 1);
}
if (oldMessage.isSet(Flag.FLAGGED)) {
setFlaggedMessageCount(getFlaggedMessageCount() - 1);
}
} }
deleteAttachments(message.getUid()); deleteAttachments(message.getUid());
@ -2576,12 +2501,6 @@ public class LocalStore extends Store implements Serializable {
saveAttachment(messageUid, attachment, copy); saveAttachment(messageUid, attachment, copy);
} }
saveHeaders(messageUid, (MimeMessage)message); saveHeaders(messageUid, (MimeMessage)message);
if (!message.isSet(Flag.SEEN)) {
setUnreadMessageCount(getUnreadMessageCount() + 1);
}
if (message.isSet(Flag.FLAGGED)) {
setFlaggedMessageCount(getFlaggedMessageCount() + 1);
}
} catch (Exception e) { } catch (Exception e) {
throw new MessagingException("Error appending message", e); throw new MessagingException("Error appending message", e);
} }
@ -2996,7 +2915,6 @@ public class LocalStore extends Store implements Serializable {
return null; return null;
} }
}); });
resetUnreadAndFlaggedCounts();
notifyChange(); notifyChange();
} }
@ -3026,27 +2944,6 @@ public class LocalStore extends Store implements Serializable {
setVisibleLimit(mAccount.getDisplayCount()); setVisibleLimit(mAccount.getDisplayCount());
} }
public void resetUnreadAndFlaggedCounts() {
try {
int newUnread = 0;
int newFlagged = 0;
Message[] messages = getMessages(null);
for (Message message : messages) {
if (!message.isSet(Flag.SEEN)) {
newUnread++;
}
if (message.isSet(Flag.FLAGGED)) {
newFlagged++;
}
}
setUnreadMessageCount(newUnread);
setFlaggedMessageCount(newFlagged);
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Unable to fetch all messages from LocalStore", e);
}
}
@Override @Override
public void delete(final boolean recurse) throws MessagingException { public void delete(final boolean recurse) throws MessagingException {
try { try {
@ -3711,9 +3608,6 @@ public class LocalStore extends Store implements Serializable {
delete(); delete();
} }
updateFolderCountsOnFlag(flag, set);
LocalMessage.super.setFlag(flag, set); LocalMessage.super.setFlag(flag, set);
} catch (MessagingException e) { } catch (MessagingException e) {
throw new WrappedException(e); throw new WrappedException(e);
@ -3812,7 +3706,6 @@ public class LocalStore extends Store implements Serializable {
try { try {
LocalFolder localFolder = (LocalFolder) mFolder; LocalFolder localFolder = (LocalFolder) mFolder;
updateFolderCountsOnFlag(Flag.X_DESTROYED, true);
localFolder.deleteAttachments(mId); localFolder.deleteAttachments(mId);
String id = Long.toString(mId); String id = Long.toString(mId);
@ -3919,39 +3812,6 @@ public class LocalStore extends Store implements Serializable {
notifyChange(); notifyChange();
} }
private void updateFolderCountsOnFlag(Flag flag, boolean set) {
/*
* Update the unread count on the folder.
*/
try {
LocalFolder folder = (LocalFolder)mFolder;
if (flag == Flag.DELETED || flag == Flag.X_DESTROYED) {
if (!isSet(Flag.SEEN)) {
folder.setUnreadMessageCount(folder.getUnreadMessageCount() + (set ? -1 : 1));
}
if (isSet(Flag.FLAGGED)) {
folder.setFlaggedMessageCount(folder.getFlaggedMessageCount() + (set ? -1 : 1));
}
}
if (!isSet(Flag.DELETED)) {
if (flag == Flag.SEEN && set != isSet(Flag.SEEN)) {
folder.setUnreadMessageCount(folder.getUnreadMessageCount() + (set ? -1 : 1));
}
if (flag == Flag.FLAGGED) {
folder.setFlaggedMessageCount(folder.getFlaggedMessageCount() + (set ? 1 : -1));
}
}
} catch (MessagingException me) {
Log.e(K9.LOG_TAG, "Unable to update LocalStore unread message count",
me);
throw new RuntimeException(me);
}
}
private void loadHeaders() throws UnavailableStorageException { private void loadHeaders() throws UnavailableStorageException {
ArrayList<LocalMessage> messages = new ArrayList<LocalMessage>(); ArrayList<LocalMessage> messages = new ArrayList<LocalMessage>();
messages.add(this); messages.add(this);

View File

@ -55,8 +55,7 @@ public class UnreadWidgetProvider extends AppWidgetProvider {
try { try {
Account account = Preferences.getPreferences(context).getAccount(accountUuid); Account account = Preferences.getPreferences(context).getAccount(accountUuid);
if (account != null) { if (account != null) {
AccountStats stats = new AccountStats(); AccountStats stats = account.getStats(context);
account.getLocalStore().getMessageCounts(stats);
unreadCount = stats.unreadMessageCount; unreadCount = stats.unreadMessageCount;
accountName = account.getDescription(); accountName = account.getDescription();
if (K9.FOLDER_NONE.equals(account.getAutoExpandFolderName())) { if (K9.FOLDER_NONE.equals(account.getAutoExpandFolderName())) {