From c59aeb2c7894ab3033ea4940f701863fbc17725b Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Thu, 21 Jan 2010 18:49:11 +0000 Subject: [PATCH] Patch from Scott Kister to give us better new-message notifications, somewhat worked over by me --- res/values/strings.xml | 1 + src/com/fsck/k9/MessagingController.java | 158 ++++++++++++++++------- src/com/fsck/k9/service/PollService.java | 27 +--- 3 files changed, 110 insertions(+), 76 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 0bfa397c8..1efd082e2 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -103,6 +103,7 @@ (No subject) + No sender Poll%s (Poll %s%s) Loading messages\u2026 diff --git a/src/com/fsck/k9/MessagingController.java b/src/com/fsck/k9/MessagingController.java index 0bd5ca74d..1d6001e24 100644 --- a/src/com/fsck/k9/MessagingController.java +++ b/src/com/fsck/k9/MessagingController.java @@ -1330,6 +1330,13 @@ public class MessagingController implements Runnable l.synchronizeMailboxAddOrUpdateMessage(account, folder, localMessage); } } + + // Send a notification of this message + if (!message.isSet(Flag.SEEN)) + { + notifyAccount(mApplication, account, message); + } + } } @@ -4118,66 +4125,114 @@ public class MessagingController implements Runnable }); } - public void notifyAccount(Context context, Account thisAccount, int newMailCount) + /** Creates a notification of new email messages + * ringtone, lights, and vibration to be played + */ + private void notifyAccount(Context context, Account account, Message message) { + if (!account.isNotifyNewMail()) + return; + + // If we have a message, set the notification to ": " + StringBuffer messageNotice = new StringBuffer(); + try + { + if (message != null && message.getFrom() != null) + { + Address[] addrs = message.getFrom(); + String from = addrs.length > 0 ? addrs[0].toFriendly() : null; + String subject = message.getSubject(); + if (subject == null) + { + subject = context.getString(R.string.general_no_subject); + } + + if (from != null) + { + // Show From: address, except show To: if sent from me + if (account.isAnIdentity(message.getFrom()) == false) + { + messageNotice.append(from + ": " + subject); + } + else + { + Address[] rcpts = message.getRecipients(Message.RecipientType.TO); + String to = rcpts.length > 0 ? rcpts[0].toFriendly() : null; + if (to != null) + { + messageNotice.append(to + ": "+subject); + } + else + { + messageNotice.append(context.getString(R.string.general_no_sender) + ": "+subject); + + } + + } + } + } + } + catch (MessagingException e) + { + Log.e(K9.LOG_TAG, "Unable to get message information for notification.", e); + } + // If we could not set a per-message notification, revert to a default message + if (messageNotice.length() == 0) + { + messageNotice.append(context.getString(R.string.notification_new_title)); + } + int unreadMessageCount = 0; try { - unreadMessageCount = thisAccount.getUnreadMessageCount(context, mApplication); + unreadMessageCount = account.getUnreadMessageCount(context, mApplication); } - catch (Exception e) + catch (MessagingException e) { - Log.e(K9.LOG_TAG, "Unable to get unread message count", e); + Log.e(K9.LOG_TAG, "Unable to getUnreadMessageCount for account: " + account, e); } - if (K9.DEBUG) - Log.i(K9.LOG_TAG, "notifyAccount Account " + thisAccount.getDescription() + ", newMailCount = " + newMailCount); - boolean isNotifyAccount = thisAccount.isNotifyNewMail(); - if (isNotifyAccount) + NotificationManager notifMgr = + (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); + Notification notif = new Notification(R.drawable.stat_notify_email_generic, messageNotice, System.currentTimeMillis()); + notif.number = unreadMessageCount; + + Intent i = FolderList.actionHandleAccountIntent(context, account); + PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0); + + // 279 Unread (someone@gmail.com) + String accountNotice = context.getString(R.string.notification_new_one_account_fmt, unreadMessageCount, account.getDescription()); + notif.setLatestEventInfo(context, accountNotice, messageNotice, pi); + + // If we've already annoyed the user with buzzing, flashing and beeping for this account, don't do it again + notif.defaults |= Notification.FLAG_ONLY_ALERT_ONCE; + + if (account.isRing()) { - - NotificationManager notifMgr = - (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); - if (newMailCount > 0 && unreadMessageCount > 0) - { - String notice = context.getString(R.string.notification_new_one_account_fmt, unreadMessageCount, - thisAccount.getDescription()); - Notification notif = new Notification(R.drawable.stat_notify_email_generic, - context.getString(R.string.notification_new_title), System.currentTimeMillis()); - - notif.number = unreadMessageCount; - - Intent i = FolderList.actionHandleAccountIntent(context, thisAccount); - - PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0); - - notif.setLatestEventInfo(context, context.getString(R.string.notification_new_title), notice, pi); - - String ringtone = thisAccount.getRingtone(); - notif.sound = TextUtils.isEmpty(ringtone) || thisAccount.isRing() == false ? null : Uri.parse(ringtone); - - if (thisAccount.isVibrate()) - { - notif.defaults |= Notification.DEFAULT_VIBRATE; - } - - notif.flags |= Notification.FLAG_SHOW_LIGHTS; - notif.ledARGB = K9.NOTIFICATION_LED_COLOR; - notif.ledOnMS = K9.NOTIFICATION_LED_ON_TIME; - notif.ledOffMS = K9.NOTIFICATION_LED_OFF_TIME; - - - notifMgr.notify(thisAccount.getAccountNumber(), notif); - } - else if (unreadMessageCount == 0) - { - notifMgr.cancel(thisAccount.getAccountNumber()); - } + String ringtone = account.getRingtone(); + notif.sound = TextUtils.isEmpty(ringtone) ? null : Uri.parse(ringtone); } - for (MessagingListener l : getListeners()) + + if (account.isVibrate()) { - l.accountStatusChanged(thisAccount, unreadMessageCount); + notif.defaults |= Notification.DEFAULT_VIBRATE; } + + notif.flags |= Notification.FLAG_SHOW_LIGHTS; + notif.ledARGB = K9.NOTIFICATION_LED_COLOR; + notif.ledOnMS = K9.NOTIFICATION_LED_ON_TIME; + notif.ledOffMS = K9.NOTIFICATION_LED_OFF_TIME; + + notif.number = unreadMessageCount; + notifMgr.notify(account.getAccountNumber(), notif); + } + + /** Cancel a notification of new email messages */ + private void notifyAccountCancel(Context context, Account account) + { + NotificationManager notifMgr = + (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); + notifMgr.cancel(account.getAccountNumber()); } @@ -4467,9 +4522,12 @@ public class MessagingController implements Runnable localFolder.setStatus(null); if (K9.DEBUG) - Log.i(K9.LOG_TAG, "messagesArrived newCount = " + newCount); + Log.i(K9.LOG_TAG, "messagesArrived newCount = " + newCount + ", unread count = " + unreadMessageCount); - notifyAccount(mApplication, account, newCount); + if (unreadMessageCount == 0) + { + notifyAccountCancel(mApplication, account); + } for (MessagingListener l : getListeners()) { diff --git a/src/com/fsck/k9/service/PollService.java b/src/com/fsck/k9/service/PollService.java index 0d7430a54..26d4e0d4a 100644 --- a/src/com/fsck/k9/service/PollService.java +++ b/src/com/fsck/k9/service/PollService.java @@ -138,24 +138,6 @@ public class PollService extends CoreService } } - private void checkMailDone(Context context, Account doNotUseaccount) - { - if (accountsChecked.isEmpty()) - { - return; - } - - for (Account thisAccount : Preferences.getPreferences(context).getAccounts()) - { - Integer newMailCount = accountsChecked.get(thisAccount.getUuid()); - if (newMailCount != null) - { - MessagingController.getInstance(getApplication()).notifyAccount(context, thisAccount, newMailCount); - } - }//for accounts - }//checkMailDone - - private void release() { MessagingController controller = MessagingController.getInstance(getApplication()); @@ -174,14 +156,7 @@ public class PollService extends CoreService if (K9.DEBUG) Log.v(K9.LOG_TAG, "***** PollService *****: checkMailFinished"); - try - { - checkMailDone(context, account); - } - finally - { - release(); - } + release(); } public int getStartId() {