From 39d595d2da681c9e75267ff99f78e156c47a272c Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 17 Feb 2015 09:23:25 +0100 Subject: [PATCH] Fix crash when sorting chat list --- src/org/linphone/ChatStorage.java | 33 ++++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/org/linphone/ChatStorage.java b/src/org/linphone/ChatStorage.java index c415b3059..189678463 100644 --- a/src/org/linphone/ChatStorage.java +++ b/src/org/linphone/ChatStorage.java @@ -389,20 +389,25 @@ public class ChatStorage { } } - Collections.sort(rooms, new Comparator() { - @Override - public int compare(LinphoneChatRoom a, LinphoneChatRoom b) { - // /!\ Warning: Have to take the second element because it returns two even when asking for only one... - long atime = a.getHistory(1)[1].getTime(); - long btime = b.getHistory(1)[1].getTime(); - if (atime > btime) - return -1; - else if (btime > atime) - return 1; - else - return 0; - } - }); + if (rooms.size() > 1) { + Collections.sort(rooms, new Comparator() { + @Override + public int compare(LinphoneChatRoom a, LinphoneChatRoom b) { + LinphoneChatMessage[] messagesA = a.getHistory(1); + LinphoneChatMessage[] messagesB = b.getHistory(1); + long atime, btime; + // /!\ Warning: Have to take the second element because it returns two even when asking for only one... + atime = messagesA.length > 1 ? messagesA[1].getTime() : messagesA[0].getTime(); + btime = messagesA.length > 1 ? messagesB[1].getTime() : messagesB[0].getTime(); + if (atime > btime) + return -1; + else if (btime > atime) + return 1; + else + return 0; + } + }); + } for (LinphoneChatRoom chatroom : rooms) { chatList.add(chatroom.getPeerAddress().asStringUriOnly());