backport trunk changes to MessagingController to (hopefully) improve our

unread-counter situation
This commit is contained in:
Jesse Vincent 2010-08-08 19:59:18 +00:00
parent e390fdf183
commit dfeced4879
1 changed files with 36 additions and 50 deletions

View File

@ -1352,6 +1352,18 @@ public class MessagingController implements Runnable
} }
final String folder = remoteFolder.getName(); final String folder = remoteFolder.getName();
int unreadBeforeStart = 0;
try
{
AccountStats stats = account.getStats(mApplication);
unreadBeforeStart = stats.unreadMessageCount;
}
catch (MessagingException e)
{
Log.e(K9.LOG_TAG, "Unable to getUnreadMessageCount for account: " + account, e);
}
ArrayList<Message> syncFlagMessages = new ArrayList<Message>(); ArrayList<Message> syncFlagMessages = new ArrayList<Message>();
List<Message> unsyncedMessages = new ArrayList<Message>(); List<Message> unsyncedMessages = new ArrayList<Message>();
final AtomicInteger newMessages = new AtomicInteger(0); final AtomicInteger newMessages = new AtomicInteger(0);
@ -1507,10 +1519,7 @@ public class MessagingController implements Runnable
// fp.add(FetchProfile.Item.FLAGS); // fp.add(FetchProfile.Item.FLAGS);
// fp.add(FetchProfile.Item.ENVELOPE); // fp.add(FetchProfile.Item.ENVELOPE);
downloadSmallMessages(account, remoteFolder, localFolder, smallMessages, progress, unreadBeforeStart, newMessages, todo, fp);
downloadSmallMessages(account, remoteFolder, localFolder, smallMessages, progress, newMessages, todo, fp);
smallMessages.clear(); smallMessages.clear();
@ -1523,7 +1532,7 @@ public class MessagingController implements Runnable
downloadLargeMessages(account, remoteFolder, localFolder, largeMessages, progress, newMessages, todo, fp); downloadLargeMessages(account, remoteFolder, localFolder, largeMessages, progress, unreadBeforeStart, newMessages, todo, fp);
largeMessages.clear(); largeMessages.clear();
/* /*
@ -1689,6 +1698,7 @@ public class MessagingController implements Runnable
final LocalFolder localFolder, final LocalFolder localFolder,
ArrayList<Message> smallMessages, ArrayList<Message> smallMessages,
final AtomicInteger progress, final AtomicInteger progress,
final int unreadBeforeStart,
final AtomicInteger newMessages, final AtomicInteger newMessages,
final int todo, final int todo,
FetchProfile fp) throws MessagingException FetchProfile fp) throws MessagingException
@ -1697,22 +1707,6 @@ public class MessagingController implements Runnable
final Date earliestDate = account.getEarliestPollDate(); final Date earliestDate = account.getEarliestPollDate();
int tmpRead = 0;
try
{
AccountStats stats = account.getStats(mApplication);
tmpRead = stats.unreadMessageCount;
}
catch (MessagingException e)
{
Log.e(K9.LOG_TAG, "Unable to getUnreadMessageCount for account: " + account, e);
}
final int unreadBeforeStart = tmpRead;
if (K9.DEBUG) if (K9.DEBUG)
Log.d(K9.LOG_TAG, "SYNC: Fetching small messages for folder " + folder); Log.d(K9.LOG_TAG, "SYNC: Fetching small messages for folder " + folder);
@ -1755,13 +1749,12 @@ public class MessagingController implements Runnable
} }
// Send a notification of this message // Send a notification of this message
if (shouldNotifyForMessage(account, message))
if (notifyAccount(mApplication, account, message, (unreadBeforeStart + newMessages.get())) == true)
{ {
newMessages.incrementAndGet(); newMessages.incrementAndGet();
notifyAccount(mApplication, account, message, unreadBeforeStart, newMessages);
} }
} }
catch (MessagingException me) catch (MessagingException me)
{ {
@ -1788,6 +1781,7 @@ public class MessagingController implements Runnable
final LocalFolder localFolder, final LocalFolder localFolder,
ArrayList<Message> largeMessages, ArrayList<Message> largeMessages,
final AtomicInteger progress, final AtomicInteger progress,
final int unreadBeforeStart,
final AtomicInteger newMessages, final AtomicInteger newMessages,
final int todo, final int todo,
FetchProfile fp) throws MessagingException FetchProfile fp) throws MessagingException
@ -1796,19 +1790,6 @@ public class MessagingController implements Runnable
final Date earliestDate = account.getEarliestPollDate(); final Date earliestDate = account.getEarliestPollDate();
int unreadBeforeStart = 0;
try
{
AccountStats stats = account.getStats(mApplication);
unreadBeforeStart = stats.unreadMessageCount;
}
catch (MessagingException e)
{
Log.e(K9.LOG_TAG, "Unable to getUnreadMessageCount for account: " + account, e);
}
if (K9.DEBUG) if (K9.DEBUG)
Log.d(K9.LOG_TAG, "SYNC: Fetching large messages for folder " + folder); Log.d(K9.LOG_TAG, "SYNC: Fetching large messages for folder " + folder);
@ -1914,12 +1895,12 @@ public class MessagingController implements Runnable
} }
// Send a notification of this message // Send a notification of this message
if (notifyAccount(mApplication, account, message, (unreadBeforeStart+newMessages.get())) == true) if (shouldNotifyForMessage(account, message))
{ {
newMessages.incrementAndGet(); newMessages.incrementAndGet();
notifyAccount(mApplication, account, message, unreadBeforeStart, newMessages);
} }
}//for large messsages }//for large messsages
if (K9.DEBUG) if (K9.DEBUG)
Log.d(K9.LOG_TAG, "SYNC: Done fetching large messages for folder " + folder); Log.d(K9.LOG_TAG, "SYNC: Done fetching large messages for folder " + folder);
@ -4401,9 +4382,7 @@ public class MessagingController implements Runnable
account.setRingNotified(false); account.setRingNotified(false);
try try
{ {
AccountStats stats = account.getStats(context); if (account.getStats(context).unreadMessageCount == 0)
int unreadMessageCount = stats.unreadMessageCount;
if (unreadMessageCount == 0)
{ {
notifyAccountCancel(context, account); notifyAccountCancel(context, account);
} }
@ -4550,10 +4529,8 @@ public class MessagingController implements Runnable
}); });
} }
/** Creates a notification of new email messages
* ringtone, lights, and vibration to be played private boolean shouldNotifyForMessage(Account account, Message message)
*/
private boolean notifyAccount(Context context, Account account, Message message, int unreadMessageCount)
{ {
// Do not notify if the user does not have notifications // Do not notify if the user does not have notifications
// enabled or if the message has been read // enabled or if the message has been read
@ -4578,6 +4555,17 @@ public class MessagingController implements Runnable
} }
} }
return true;
}
/** Creates a notification of new email messages
* ringtone, lights, and vibration to be played
*/
private boolean notifyAccount(Context context, Account account, Message message, int previousUnreadMessageCount, AtomicInteger newMessageCount)
{
// If we have a message, set the notification to "<From>: <Subject>" // If we have a message, set the notification to "<From>: <Subject>"
StringBuffer messageNotice = new StringBuffer(); StringBuffer messageNotice = new StringBuffer();
try try
@ -4602,7 +4590,6 @@ public class MessagingController implements Runnable
// show To: if the message was sent from me // show To: if the message was sent from me
else else
{ {
// Do not notify of mail from self if !isNotifySelfNewMail
if (!account.isNotifySelfNewMail()) if (!account.isNotifySelfNewMail())
{ {
return false; return false;
@ -4636,16 +4623,15 @@ public class MessagingController implements Runnable
messageNotice.append(context.getString(R.string.notification_new_title)); messageNotice.append(context.getString(R.string.notification_new_title));
} }
NotificationManager notifMgr = NotificationManager notifMgr =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.stat_notify_email_generic, messageNotice, System.currentTimeMillis()); Notification notif = new Notification(R.drawable.stat_notify_email_generic, messageNotice, System.currentTimeMillis());
notif.number = unreadMessageCount; notif.number = previousUnreadMessageCount + newMessageCount.get();
Intent i = FolderList.actionHandleNotification(context, account, account.getAutoExpandFolderName()); Intent i = FolderList.actionHandleNotification(context, account, account.getAutoExpandFolderName());
PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0); PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);
String accountNotice = context.getString(R.string.notification_new_one_account_fmt, (unreadMessageCount), account.getDescription()); String accountNotice = context.getString(R.string.notification_new_one_account_fmt, notif.number, account.getDescription());
notif.setLatestEventInfo(context, accountNotice, messageNotice, pi); notif.setLatestEventInfo(context, accountNotice, messageNotice, pi);
// Only ring or vibrate if we have not done so already on this // Only ring or vibrate if we have not done so already on this