diff --git a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroid.java b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroid.java index 7d8cb94984..a582812559 100644 --- a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroid.java +++ b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroid.java @@ -427,7 +427,7 @@ private List extractMessages(Connection conn, WAContact remote, boolean } } - Collections.sort(messages); + Message.sort(messages); } //Find quote messages diff --git a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroidNew.java b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroidNew.java index a37f2609c6..c028d08d04 100644 --- a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroidNew.java +++ b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorAndroidNew.java @@ -147,7 +147,7 @@ protected List extractChatList() throws WAExtractorException { extractCalls(conn, idToChat); for (Chat c : list) { - Collections.sort(c.getMessages()); + Message.sort(c.getMessages()); if (c.isGroupChat()) { setGroupMembers(c, conn, SELECT_GROUP_MEMBERS); } diff --git a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorIOS.java b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorIOS.java index a5f8e6215d..a11da7d6b5 100644 --- a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorIOS.java +++ b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/ExtractorIOS.java @@ -383,7 +383,7 @@ private void mergeUndeletedMessages(Chat chat, Map messagesMap, } } - Collections.sort(chat.getMessages()); + Message.sort(chat.getMessages()); } } diff --git a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/Message.java b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/Message.java index e50b6b3c02..c7af95678b 100644 --- a/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/Message.java +++ b/iped-parsers/iped-parsers-impl/src/main/java/iped/parsers/whatsapp/Message.java @@ -691,4 +691,57 @@ public static enum MessageQuotedType { public int compareTo(Message o) { return Long.compare(getSortId(), o.getSortId()); } + + public static void sort(List messages) { + Collections.sort(messages); + + // Check if there are messages with sortId != 0 AND sortId == 0 + boolean zero = false; + boolean notZero = false; + for (Message m : messages) { + if (m.sortId == 0) { + zero = true; + } else { + notZero = true; + } + if (zero && notZero) { + break; + } + } + + // If both are present, merge these two groups + if (zero && notZero) { + List l0 = new ArrayList(); + List l1 = new ArrayList(); + for (Message m : messages) { + if (m.sortId == 0) { + l0.add(m); + } else { + l1.add(m); + } + } + messages.clear(); + int idx0 = 0; + int idx1 = 0; + while (idx0 < l0.size() || idx1 < l1.size()) { + if (idx0 == l0.size()) { + messages.add(l1.get(idx1++)); + } else if (idx1 == l1.size()) { + messages.add(l0.get(idx0++)); + } else { + Message m0 = l0.get(idx0); + Message m1 = l1.get(idx1); + long t0 = m0.timeStamp == null ? m0.id : m0.timeStamp.getTime(); + long t1 = m1.timeStamp == null ? m1.id : m1.timeStamp.getTime(); + if (t0 < t1) { + messages.add(m0); + idx0++; + } else { + messages.add(m1); + idx1++; + } + } + } + } + } }