From 43c455eb266125a8e09a98e38d7639a865f52d42 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 8 Mar 2011 10:34:18 -0800 Subject: [PATCH] Fix NPE in NotificationController This would happen when a message has no from addresses. Bug 4027170 Change-Id: I1da091a6a55b274805ee6b3e109cbeb55a21fae1 --- .../android/email/NotificationController.java | 5 ++++- .../email/NotificationControllerTest.java | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/com/android/email/NotificationController.java b/src/com/android/email/NotificationController.java index ea6c061fd..c9e7f2fd7 100644 --- a/src/com/android/email/NotificationController.java +++ b/src/com/android/email/NotificationController.java @@ -215,7 +215,10 @@ public class NotificationController { return null; // no message found??? } - final String senderName = Address.toFriendly(Address.unpack(message.mFrom)); + String senderName = Address.toFriendly(Address.unpack(message.mFrom)); + if (senderName == null) { + senderName = ""; // Happens when a message has no from. + } final String subject = message.mSubject; final Bitmap senderPhoto = getSenderPhoto(message); diff --git a/tests/src/com/android/email/NotificationControllerTest.java b/tests/src/com/android/email/NotificationControllerTest.java index 84e9bbfe8..fd3c19b42 100644 --- a/tests/src/com/android/email/NotificationControllerTest.java +++ b/tests/src/com/android/email/NotificationControllerTest.java @@ -224,6 +224,24 @@ public class NotificationControllerTest extends AndroidTestCase { // TODO Add 2 account test, if we find a way to check content } + public void testCreateNewMessageNotificationWithEmptyFrom() { + final Context c = mProviderContext; + Notification n; + + // Message with no from fields. + Account a1 = ProviderTestUtils.setupAccount("a1", true, c); + Mailbox b1 = ProviderTestUtils.setupMailbox("inbox", a1.mId, true, c, Mailbox.TYPE_INBOX); + Message m1 = ProviderTestUtils.setupMessage("message", a1.mId, b1.mId, true, false, c); + m1.mFrom = null; + m1.save(c); + + // This shouldn't crash. + n = mTarget.createNewMessageNotification(a1.mId, 1); + + // Minimum test for the result + assertEquals(R.drawable.stat_notify_email_generic, n.icon); + } + public void testGetNotificationTitle() { final Context c = mProviderContext;