1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-25 15:11:52 -05:00

Issue 394

Issue 252

394: Replace code change lost with move of DAmail MailService.java to
K-9.  Eliminate the stopSelfs.  Without the stopSelfs, the Service
remains active and the Android platform is far less likely to kill the
process, although not absolutely forbidden from doing so.  Better will
be to implement the full BroadcastReceiver methodology referred to in
Issue 394, which would allow replacing the stopSelfs, which would make
K-9 a better good citizen.

252: Fix logic for raising and canceling notifications based on number
of new unread messages and total unread messages.
This commit is contained in:
Daniel Applebaum 2009-05-02 14:49:29 +00:00
parent b603d899db
commit 0ffe1621a2

View File

@ -41,7 +41,7 @@ public class MailService extends Service {
private Listener mListener = new Listener(); private Listener mListener = new Listener();
private int mStartId; // private int mStartId;
public static void actionReschedule(Context context) { public static void actionReschedule(Context context) {
Intent i = new Intent(); Intent i = new Intent();
@ -68,7 +68,7 @@ public class MailService extends Service {
setForeground(true); // if it gets killed once, it'll never restart setForeground(true); // if it gets killed once, it'll never restart
Log.v(Email.LOG_TAG, "***** MailService *****: onStart(" + intent + ", " + startId + ")"); Log.v(Email.LOG_TAG, "***** MailService *****: onStart(" + intent + ", " + startId + ")");
super.onStart(intent, startId); super.onStart(intent, startId);
this.mStartId = startId; // this.mStartId = startId;
// MessagingController.getInstance(getApplication()).addListener(mListener); // MessagingController.getInstance(getApplication()).addListener(mListener);
if (ACTION_CHECK_MAIL.equals(intent.getAction())) { if (ACTION_CHECK_MAIL.equals(intent.getAction())) {
@ -95,7 +95,7 @@ public class MailService extends Service {
} }
reschedule(); reschedule();
stopSelf(startId); // stopSelf(startId);
} }
else if (ACTION_CANCEL.equals(intent.getAction())) { else if (ACTION_CANCEL.equals(intent.getAction())) {
if (Config.LOGV) { if (Config.LOGV) {
@ -104,7 +104,7 @@ public class MailService extends Service {
MessagingController.getInstance(getApplication()).log("***** MailService *****: cancel"); MessagingController.getInstance(getApplication()).log("***** MailService *****: cancel");
cancel(); cancel();
stopSelf(startId); // stopSelf(startId);
} }
else if (ACTION_RESCHEDULE.equals(intent.getAction())) { else if (ACTION_RESCHEDULE.equals(intent.getAction())) {
if (Config.LOGV) { if (Config.LOGV) {
@ -112,7 +112,7 @@ public class MailService extends Service {
} }
MessagingController.getInstance(getApplication()).log("***** MailService *****: reschedule"); MessagingController.getInstance(getApplication()).log("***** MailService *****: reschedule");
reschedule(); reschedule();
stopSelf(startId); // stopSelf(startId);
} }
} }
@ -179,7 +179,7 @@ public class MailService extends Service {
} }
class Listener extends MessagingListener { class Listener extends MessagingListener {
HashMap<String, Integer> accountsWithNewMail = new HashMap<String, Integer>(); HashMap<String, Integer> accountsChecked = new HashMap<String, Integer>();
private WakeLock wakeLock = null; private WakeLock wakeLock = null;
// wakelock strategy is to be very conservative. If there is any reason to release, then release // wakelock strategy is to be very conservative. If there is any reason to release, then release
@ -209,7 +209,7 @@ public class MailService extends Service {
} }
@Override @Override
public void checkMailStarted(Context context, Account account) { public void checkMailStarted(Context context, Account account) {
accountsWithNewMail.clear(); accountsChecked.clear();
} }
@Override @Override
@ -223,14 +223,19 @@ public class MailService extends Service {
String folder, String folder,
int totalMessagesInMailbox, int totalMessagesInMailbox,
int numNewMessages) { int numNewMessages) {
if (account.isNotifyNewMail() && numNewMessages > 0) { if (account.isNotifyNewMail()) {
accountsWithNewMail.put(account.getUuid(), numNewMessages); Integer existingNewMessages = accountsChecked.get(account.getUuid());
if (existingNewMessages == null)
{
existingNewMessages = 0;
}
accountsChecked.put(account.getUuid(), existingNewMessages + numNewMessages);
} }
} }
private void checkMailDone(Context context, Account doNotUseaccount) private void checkMailDone(Context context, Account doNotUseaccount)
{ {
if (accountsWithNewMail.isEmpty()) if (accountsChecked.isEmpty())
{ {
return; return;
} }
@ -240,63 +245,52 @@ public class MailService extends Service {
int index = 0; int index = 0;
for (Account thisAccount : Preferences.getPreferences(context).getAccounts()) { for (Account thisAccount : Preferences.getPreferences(context).getAccounts()) {
//No need to filter out accounts that do not require notification Integer newMailCount = accountsChecked.get(thisAccount.getUuid());
//since only the one that require so are in this map int unreadMessageCount = -1;
if (accountsWithNewMail.containsKey(thisAccount.getUuid())) if (newMailCount != null)
{ {
int unreadMessageCount = 0;
String notice = null;
try try
{ {
unreadMessageCount = thisAccount.getUnreadMessageCount(context, getApplication()); unreadMessageCount = thisAccount.getUnreadMessageCount(context, getApplication());
if (unreadMessageCount > 0) if (unreadMessageCount > 0 && newMailCount > 0)
{ {
notice = getString(R.string.notification_new_one_account_fmt, unreadMessageCount, String notice = getString(R.string.notification_new_one_account_fmt, unreadMessageCount,
thisAccount.getDescription()); thisAccount.getDescription());
Notification notif = new Notification(R.drawable.stat_notify_email_generic,
getString(R.string.notification_new_title), System.currentTimeMillis() + (index*1000));
notif.number = unreadMessageCount;
Intent i = FolderMessageList.actionHandleAccountIntent(context, thisAccount);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);
notif.setLatestEventInfo(context, getString(R.string.notification_new_title), notice, pi);
String ringtone = thisAccount.getRingtone();
notif.sound = TextUtils.isEmpty(ringtone) ? null : Uri.parse(ringtone);
if (thisAccount.isVibrate()) {
notif.defaults |= Notification.DEFAULT_VIBRATE;
}
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledARGB = Email.NOTIFICATION_LED_COLOR;
notif.ledOnMS = Email.NOTIFICATION_LED_ON_TIME;
notif.ledOffMS = Email.NOTIFICATION_LED_OFF_TIME;
notifMgr.notify(thisAccount.getAccountNumber(), notif);
} }
//Can this ever happen? else if (unreadMessageCount == 0)
else
{ {
notice = getString(R.string.notification_new_one_account_unknown_unread_count_fmt, (int)accountsWithNewMail.get(thisAccount.getUuid()), thisAccount.getDescription()); notifMgr.cancel(thisAccount.getAccountNumber());
} }
} }
catch (MessagingException me) catch (MessagingException me)
{ {
Log.e(Email.LOG_TAG, "***** MailService *****: couldn't get unread count for account " + Log.e(Email.LOG_TAG, "***** MailService *****: couldn't get unread count for account " +
thisAccount.getDescription(), me); thisAccount.getDescription(), me);
notice = getString(R.string.notification_new_one_account_unknown_unread_count_fmt, (int)accountsWithNewMail.get(thisAccount.getUuid()), thisAccount.getDescription());
} }
Notification notif = new Notification(R.drawable.stat_notify_email_generic,
getString(R.string.notification_new_title), System.currentTimeMillis() + (index*1000));
if (unreadMessageCount > 0)
{
notif.number = unreadMessageCount;
Intent i = FolderMessageList.actionHandleAccountIntent(context, thisAccount);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);
notif.setLatestEventInfo(context, getString(R.string.notification_new_title), notice, pi);
String ringtone = thisAccount.getRingtone();
notif.sound = TextUtils.isEmpty(ringtone) ? null : Uri.parse(ringtone);
if (thisAccount.isVibrate()) {
notif.defaults |= Notification.DEFAULT_VIBRATE;
}
notif.flags |= Notification.FLAG_SHOW_LIGHTS;
notif.ledARGB = Email.NOTIFICATION_LED_COLOR;
notif.ledOnMS = Email.NOTIFICATION_LED_ON_TIME;
notif.ledOffMS = Email.NOTIFICATION_LED_OFF_TIME;
notifMgr.notify(thisAccount.getAccountNumber(), notif);
}
else
{
notifMgr.cancel(thisAccount.getAccountNumber());
}
} }
}//for accounts }//for accounts
}//checkMailDone }//checkMailDone
@ -307,7 +301,7 @@ public class MailService extends Service {
controller.setCheckMailListener(null); controller.setCheckMailListener(null);
reschedule(); reschedule();
wakeLockRelease(); wakeLockRelease();
stopSelf(mStartId); // stopSelf(mStartId);
} }
@Override @Override